jxl 生成excel檔案 採用模板 動態插入行
阿新 • • 發佈:2019-02-18
/* * tempPath: excel模板檔案路徑(包含檔名) * dataForm: 資料物件 * saveFilePath: 生成完excel檔案後儲存的路徑(包含檔名) */ private static void createExcelByTemp(String tempPath, DataForm dataForm, String saveFilePath){ WritableWorkbook wwb = null; FileOutputStream out = null; try { int rows = 8, cols = 4; out = new FileOutputStream(saveFilePath); Workbook wb = Workbook.getWorkbook(new File(tempPath)); wwb = Workbook.createWorkbook(out, wb); WritableSheet sheet = wwb.getSheet(0); for (int i = 1; i < rows; i++){ for (int j = 0; j < cols; j++){ Cell cell = sheet.getCell(j,i); //得到每個單元格物件 String cen = cell.getContents(); //得到單元格中的內容 cen = cen.replace("${yearCode}", dataForm.getYearCode()) //替換內容 .replace("${year}", String.valueOf(dataForm.getYear())) .replace("${yearNumber}", String.valueOf(dataForm.getYearNumber())) //.replace("${visitor_tr}", trs) .replace("${field}", dataForm.getField()) .replace("${printTo}", dataForm.getPrintTo()) .replace("${digest}", dataForm.getDigest()) .replace("${memo}", dataForm.getMemo()) .replace("${cosignTo}", dataForm.getCosignTo()); Label lbl = new Label(j, i, cen);//將第一個單元格的值改為“修改後的值” CellFormat cf = cell.getCellFormat();//獲取第一個單元格的格式 lbl.setCellFormat(cf);//將修改後的單元格的格式設定成跟原來一樣 sheet.addCell(lbl);
/* 通過設定自動換行來解決內容中的換行符“\n”不生效的方式,*/
/* CellFormat cf = cell.getCellFormat();
WritableCellFormat wcf = new WritableCellFormat(cf); wcf.setBorder(Border.RIGHT, BorderLineStyle.THIN);
wcf.setWrap(true);
lbl.setCellFormat(wcf);*/
}}String string1 = dataForm.getString1();String string3 = dataForm.getString3();String[] visitors = string1.split("@@@");String[] companyDutys = string3.split("@@@");String trs = "";Cell cell = null;int height = 25;CellFormat[] cf = new CellFormat[4];for (int i = 0; i < visitors.length; i++) { //動態插入行String visitor = visitors[i];String companyDuty = companyDutys[i];int r = i +3, c = 0;if (i == 0) { //給動態行設定一個樣式行,便於給以後動態插入的行新增相同的樣式。//sheet.insertRow(r);height = sheet.getRowHeight(r); //取得行高cell = sheet.getCell(c, r);cf[c] = cell.getCellFormat();Label lbl = new Label(c, r, visitor);lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);cell = sheet.getCell(c, r);cf[c] = cell.getCellFormat();lbl = new Label(c, r, "團長");lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);cell = sheet.getCell(c, r);cf[c] = cell.getCellFormat();lbl = new Label(c, r, companyDuty);lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);cell = sheet.getCell(c, r);cf[c] = cell.getCellFormat();} else {sheet.insertRow(r);sheet.setRowView(r, height);sheet.mergeCells(2, r, 3, r); //合併單元格//Cell cell = sheet.getCell(c, r);Label lbl = new Label(c, r, visitor);lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);lbl = new Label(c, r, "團員");lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);lbl = new Label(c, r, companyDuty);lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);lbl = new Label(c, r, "");lbl.setCellFormat(cf[c++]);sheet.addCell(lbl);}}//WritableCell cell =ws.getWritableCell(0, 0);//獲取第一個單元格wwb.write();out.flush();} catch (Exception e) {e.printStackTrace();} finally {try {wwb.close();} catch (IOException e) {}try {out.close();} catch (IOException e) {}}}
對應模板如下圖: