最近寫了一個,Excel 的 寫入和導出. 需求是這樣的. 在新建合同的時候,會有導出合同的數據, 導出的模板是固定的,,需要在模板里面寫入合同的信息.
first : 下載模板 > 寫入數據 > 輸出
下載模板 :
StringBuilder path = new StringBuilder(""); path.append(request.getSession().getServletContext().getRealPath("")); path.append(File.separator); path.append("WEB-INF"); path.append(File.separator); path.append("classes"); path.append(File.separator); path.append("template"); path.append(File.separator); String filePath=path.toString()+"\\"+"contractDemo.xlsx"; //這是獲取jboss服務器上的模板路徑
FileInputStream fis = new FileInputStream(filePath); XSSFWorkbook workBook=new XSSFWorkbook(fis); // 新建一個workBook 用來新建Excel 的sheet
// 這個是下載和輸出excel excel寫入數據 是在另一個方法里面寫着的 ,方法分開來寫比較清晰.
try{
StringBuilder path = new StringBuilder(""); path.append(request.getSession().getServletContext().getRealPath("")); path.append(File.separator); path.append("WEB-INF"); path.append(File.separator); path.append("classes"); path.append(File.separator); path.append("template"); path.append(File.separator); String filePath=path.toString()+"\\"+"contractDemo.xlsx"; // 服務器上的模板路徑 FileInputStream fis = new FileInputStream(filePath); // 輸入流 XSSFWorkbook workBook=new XSSFWorkbook(fis); String fileName="test_"+System.currentTimeMillis()+".xlsx"; OutputStream out=new FileOutputStream("d:/"+fileName); contractExportTemplate.createSheet(workBook,vo,conf); workBook.setForceFormulaRecalculation(true); workBook.write(out); fis.close(); out.flush(); out.close(); return "success"; }catch(Exception e){ e.printStackTrace(); return "error"; }
// 下面的代碼其實就是在excel 里面寫入數據,我是根據模板來寫的,所以sheet 表里面的格式都是固定的.我只要獲取具體的單元格然后寫入數據就可以了.
//給excel表添加數據 public void excelContractWriteData(XSSFWorkbook workBook,XSSFSheet sheet, List<ContractExcelGroupByStoreVo> conList1, List<ContractExcelGroupByAreaVo> conList2,List<ContractExcelGroupByStoreVo> conList3) throws Exception{ if(conList1.size()!=0){ XSSFRow row0=sheet.getRow(0); row0.getCell(2).setCellValue(conList1.get(0).getTaskId()+""+ (conList1.get(0).getPrintSeqNo()==null?"1":conList1.get(0).getPrintSeqNo()));//寫入打印編號 XSSFRow row=sheet.getRow(2); //獲取sheet表的單元格,寫入數據 row.getCell(2).setCellValue(conList1.get(0).getYear()); row.getCell(4).setCellValue(conList1.get(0).getCatlgId()); row.getCell(6).setCellValue(conList1.get(0).getSupNo()); } if(conList2.size()!=0){ for( ContractExcelGroupByAreaVo vo :conList2){ if(vo.getAreaName()!="" && "華東".equals(vo.getAreaName().toString())) { sheet.getRow(6).getCell(2).setCellValue(vo.getStoreNum()); sheet.getRow(6).getCell(3).setCellValue(vo.getTargetNum()); } if(vo.getAreaName()!="" && "西南".equals(vo.getAreaName().toString())) { sheet.getRow(7).getCell(2).setCellValue(vo.getStoreNum()); sheet.getRow(7).getCell(3).setCellValue(vo.getTargetNum()); } if(vo.getAreaName()!="" && "華北".equals(vo.getAreaName().toString())) { sheet.getRow(8).getCell(2).setCellValue(vo.getStoreNum()); sheet.getRow(8).getCell(3).setCellValue(vo.getTargetNum()); } if(vo.getAreaName()!="" && "華南".equals(vo.getAreaName().toString())) { sheet.getRow(9).getCell(2).setCellValue(vo.getStoreNum()); sheet.getRow(9).getCell(3).setCellValue(vo.getTargetNum()); } if(vo.getAreaName()!="" && "華中".equals(vo.getAreaName().toString())) { sheet.getRow(10).getCell(2).setCellValue(vo.getStoreNum()); sheet.getRow(10).getCell(3).setCellValue(vo.getTargetNum()); } } } if(conList3.size()!=0){ int rowIndex=14; //這個數字是根據excel模板定的 for(ContractExcelGroupByStoreVo conExcel : conList3){ sheet.getRow(rowIndex).getCell(1).setCellValue(conExcel.getAreaName()); sheet.getRow(rowIndex).getCell(2).setCellValue(conExcel.getProvinceName()); sheet.getRow(rowIndex).getCell(3).setCellValue(conExcel.getCityName()); sheet.getRow(rowIndex).getCell(4).setCellValue(conExcel.getStoreNum()+"-"+conExcel.getStoreName()); //門店的編號 sheet.getRow(rowIndex).getCell(5).setCellValue(conExcel.getStoreAmount()); sheet.getRow(rowIndex).getCell(6).setCellValue(conExcel.getMemo()); if(conExcel.getServReqd()!=null){ String month= conExcel.getServReqd().toString(); sheet.getRow(rowIndex).getCell(7).setCellValue(this.stringtoIntArray(month)); }else{ sheet.getRow(rowIndex).getCell(7).setCellValue(conExcel.getDateStr()); } rowIndex++; } } }
// 這里是一個排序, 月份有 1 2 3 4 5 6 7 8 9 10 11 12 但是每次出現的月份是不固定的 可能中間會有斷開的月份. 所以需要先排序然后 判斷 第二個數和第一個數相差多少, 如果相差大於2的話就說明 月份中間是有斷開的.那就把連續的幾個數的頭尾 用字符串拼接起來,顯示
//把 月份數組轉換為字符串 public String stringtoIntArray(String str) { StringBuffer dateStage= new StringBuffer(""); String strs[] = str.split(","); int array[] = new int[strs.length]; for(int i=0;i<strs.length;i++){ array[i]=Integer.parseInt(strs[i]); } for (int i = 0; i < array.length; i++) { for(int j = 0; j<array.length-i-1; j++){ if(array[j]>array[j+1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } int a=array[0]; for (int j = 1; j < array.length; j++) { if(array[j] - array[j-1]>=2){ dateStage.append(""+a+"-"+array[j-1]+","); a=array[j]; } if(j==(array.length-1)){ dateStage.append(""+a+"-"+array[j]); } } return dateStage.toString(); }