poi 讀取Excel檔案模板生成報表檔案
阿新 • • 發佈:2019-02-07
採用poi實現報表功能。
支援excel中多sheet,支援單個sheet頁中多表格
1.將excel模板檔案中的頭、底部的內容讀入記憶體
2.提取模板列資料、格式
3.建立新的XSSFWorkbook物件
4.新增資料(報表內容,程式碼未體現)
5.將新檔案寫入到本地磁碟
6.所需jar:
sheet1:
sheet2:
55行
package com.suixingpay; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.suixingpay.domain.BookDomain; import com.suixingpay.domain.SheetDomain; import com.suixingpay.domain.TableDomain; /** * * @author Fu Wei * @date 2012-12-18 下午6:12:33 * @Package com.suixingpay * @version V1.0 * @Description TODO */ public class PoiTest { public static void main(String[] args) { // 第一個Table TableDomain tableDomain1 = new TableDomain(); tableDomain1.setStartRowNum(0); tableDomain1.setEndRowNum(7); tableDomain1.setTotalRowLen(2); // 第二個Table TableDomain tableDomain2 = new TableDomain(); tableDomain2.setStartRowNum(9); tableDomain2.setEndRowNum(16); tableDomain2.setTotalRowLen(2); List<TableDomain> tables = new ArrayList<TableDomain>(); tables.add(tableDomain1); tables.add(tableDomain2); SheetDomain sheetDomain1 = new SheetDomain(); sheetDomain1.setTables(tables); sheetDomain1.setSheetName("優惠資訊情況統計報表"); // 第二個sheet TableDomain tableDomain3 = new TableDomain(); tableDomain3.setStartRowNum(0); tableDomain3.setEndRowNum(55); tableDomain3.setTotalRowLen(1); List<TableDomain> tables2 = new ArrayList<TableDomain>(); tables2.add(tableDomain3); SheetDomain sheetDomain2 = new SheetDomain(); sheetDomain2.setTables(tables2); sheetDomain2.setSheetName("測試"); //新增sheet List<SheetDomain> sheets = new ArrayList<SheetDomain>(); sheets.add(sheetDomain1); sheets.add(sheetDomain2); // book檔案 BookDomain bookDomain = new BookDomain(); bookDomain.setBookName("xx.xlsx"); bookDomain.setPath("E:\\"); bookDomain.setSheets(sheets); PoiTest p = new PoiTest(); XSSFWorkbook wk = p.createXSSFWorkBook("E:\\a.xlsx", bookDomain); p.writeWorkBook(wk, new File(bookDomain.getPath() + bookDomain.getBookName())); } /** * 寫xls檔案 * * @param wk * @param file */ public void writeWorkBook(XSSFWorkbook wk, File file) { try { OutputStream out = new FileOutputStream(file); wk.write(out); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 設定列樣式 * * @param srcStyle * 源 模板樣式 * @param desStyle * 目標樣式 */ public void setCellStyle(XSSFCellStyle srcStyle, XSSFCellStyle desStyle) { // 資料格式 desStyle.setDataFormat(srcStyle.getDataFormat()); desStyle.setRotation(srcStyle.getRotation()); // 填充單元格 desStyle.setFillPattern(srcStyle.getFillPattern()); // 列填充前景色 XSSFColor foregroundColor = srcStyle.getFillForegroundColorColor(); if (foregroundColor != null) { desStyle.setFillForegroundColor(foregroundColor); } // 邊框樣式 desStyle.setBorderBottom(srcStyle.getBorderBottom()); desStyle.setBorderTop(srcStyle.getBorderTop()); desStyle.setBorderLeft(srcStyle.getBorderLeft()); desStyle.setBorderRight(srcStyle.getBorderRight()); // 對齊方向 desStyle.setAlignment(srcStyle.getAlignment()); desStyle.setVerticalAlignment(srcStyle.getVerticalAlignment()); } /** * 設定字型 * * @param srcFont * 源模板字型 * @param desFont * 目標字型 */ public void setFont(Font srcFont, Font desFont) { desFont.setColor(srcFont.getColor()); desFont.setFontName(srcFont.getFontName()); desFont.setItalic(srcFont.getItalic()); desFont.setBoldweight(srcFont.getBoldweight()); desFont.setFontHeight(srcFont.getFontHeight()); desFont.setCharSet(srcFont.getCharSet()); } /** * 列拷貝 * * @param srcCell * @param desCell * @param wookBook */ public void copyCell(XSSFCell srcCell, XSSFCell desCell, XSSFWorkbook wookBook) { XSSFCellStyle srcStyle = srcCell.getCellStyle(); XSSFCellStyle desStyle = wookBook.createCellStyle(); Font srcFont = srcStyle.getFont(); Font desFont = wookBook.createFont(); // 設定列style setCellStyle(srcStyle, desStyle); // 設定列字型 setFont(srcFont, desFont); // 新增字型 desStyle.setFont(desFont); // 列新增樣式 desCell.setCellStyle(desStyle); // 列值拷貝 copyValue(srcCell, desCell); } /** * 列中的值拷貝 * * @param srcCell * @param distCell */ public void copyValue(XSSFCell srcCell, XSSFCell distCell) { int srcCellType = srcCell.getCellType(); distCell.setCellType(srcCellType); if (srcCellType == XSSFCell.CELL_TYPE_NUMERIC) { if (HSSFDateUtil.isCellDateFormatted(srcCell)) { distCell.setCellValue(srcCell.getDateCellValue()); } else { distCell.setCellValue(srcCell.getNumericCellValue()); } } else if (srcCellType == XSSFCell.CELL_TYPE_STRING) { distCell.setCellValue(srcCell.getRichStringCellValue()); } else if (srcCellType == XSSFCell.CELL_TYPE_BLANK) { } else if (srcCellType == XSSFCell.CELL_TYPE_BOOLEAN) { distCell.setCellValue(srcCell.getBooleanCellValue()); } else if (srcCellType == XSSFCell.CELL_TYPE_ERROR) { distCell.setCellErrorValue(srcCell.getErrorCellValue()); } else if (srcCellType == XSSFCell.CELL_TYPE_FORMULA) { distCell.setCellFormula(srcCell.getCellFormula()); } else { } } /** * 建立xls檔案 * * @param tmpFilePath * 模板檔案路徑 * @param bookDomain * 模板配置物件 * @return */ public XSSFWorkbook createXSSFWorkBook(String tmpFilePath, BookDomain bookDomain) { // 目標book XSSFWorkbook desWworkBook = new XSSFWorkbook(); InputStream is = null; try { is = new FileInputStream(new File(tmpFilePath)); // 源book XSSFWorkbook srcWorkBook = new XSSFWorkbook(is); if (srcWorkBook != null) { for (int i = 0; i < bookDomain.getSheetNum(); i++) { XSSFSheet srcSheet = srcWorkBook.getSheetAt(i); XSSFSheet desSheet = desWworkBook.createSheet(); List<TableDomain> tables = bookDomain.getSheets().get(i).getTables(); for (TableDomain table : tables) { for (int j = table.getStartRowNum(); j < table.getEndRowNum(); j++) { // 提取行 XSSFRow srcRow = srcSheet.getRow(j); // 建立行 XSSFRow desRow = desSheet.createRow(j); int lastRowNum = srcRow.getLastCellNum(); // 設定行高 desRow.setHeight(srcRow.getHeight()); for (int k = srcRow.getFirstCellNum(); k < lastRowNum; k++) { // 設定列寬 desSheet.setColumnWidth(k, srcSheet.getColumnWidth(k)); // 獲取模板列資料 XSSFCell srcCell = srcRow.getCell(k); // 建立新列 XSSFCell desCell = desRow.createCell(k); // 拷貝列內容 copyCell(srcCell, desCell, desWworkBook); } } } // 設定整個sheet的跨列 int num = srcSheet.getNumMergedRegions(); for (int n = 0; n < num; n++) { desSheet.addMergedRegion(srcSheet.getMergedRegion(n)); } desWworkBook.setSheetName(i, bookDomain.getSheets().get(i).getSheetName()); } } is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return desWworkBook; } }
package com.suixingpay.domain; import java.util.List; /** * * * @author Fu Wei * @date 2012-12-17 下午3:25:48 * @Package com.suixingpay.domain * @version V1.0 * @Description TODO */ public class BookDomain { private String bookName; private String path; // sheet 集合 private List<SheetDomain> sheets ; public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public List<SheetDomain> getSheets() { return sheets; } public void setSheets(List<SheetDomain> sheets) { this.sheets = sheets; } private BookDomain(String bookName, String path, List<SheetDomain> sheets) { super(); this.bookName = bookName; this.path = path; this.sheets = sheets; } public BookDomain() { super(); } /** * 獲取sheet頁數 * @return */ public int getSheetNum(){ return sheets == null ? 0 : sheets.size(); } }
package com.suixingpay.domain; import java.util.List; /** * sheet 實體 * @author Fu Wei * @date 2012-12-17 下午3:20:24 * @Package com.suixingpay.domain * @version V1.0 * @Description TODO */ public class SheetDomain { private String sheetName = ""; private List<TableDomain> tables; public String getSheetName() { return sheetName; } public void setSheetName(String sheetName) { this.sheetName = sheetName; } public List<TableDomain> getTables() { return tables; } public void setTables(List<TableDomain> tables) { this.tables = tables; } private SheetDomain(String sheetName, List<TableDomain> tables) { super(); this.sheetName = sheetName; this.tables = tables; } public SheetDomain() { super(); } }
package com.suixingpay.domain;
/**
* 每個sheet頁中的 表的格式
*
* @author Fu Wei
* @date 2012-12-17 下午2:37:33
* @Package com.suixingpay.domain
* @version V1.0
* @Description TODO
*/
public class TableDomain {
// 起始行數
private int startRowNum = 0;
// 結束下標
private int endRowNum = 0;
// 總結行長度
private int totalRowLen = 0;
public int getStartRowNum() {
return startRowNum;
}
public void setStartRowNum(int startRowNum) {
this.startRowNum = startRowNum;
}
public int getEndRowNum() {
return endRowNum;
}
public void setEndRowNum(int endRowNum) {
this.endRowNum = endRowNum;
}
public int getTotalRowLen() {
return totalRowLen;
}
public void setTotalRowLen(int totalRowLen) {
this.totalRowLen = totalRowLen;
}
private TableDomain(int startRowNum, int endRowNum, int totalRowLen) {
super();
this.startRowNum = startRowNum;
this.endRowNum = endRowNum;
this.totalRowLen = totalRowLen;
}
public TableDomain() {
super();
}
}