apache poi支援java對許多格式的檔案進行導入、操作、生成,而poi的幾個jar包有各自支援的檔案格式,可以選擇性導入需要的jar包。具體如下:
Component | Application type | Maven artifactId | Notes |
---|---|---|---|
POIFS | OLE2 Filesystem | poi | Required to work with OLE2 / POIFS based files |
HPSF | OLE2 Property Sets | poi | |
HSSF | Excel XLS | poi | For HSSF only, if common SS is needed see below |
HSLF | PowerPoint PPT | poi-scratchpad | |
HWPF | Word DOC | poi-scratchpad | |
HDGF | Visio VSD | poi-scratchpad | |
HPBF | Publisher PUB | poi-scratchpad | |
HSMF | Outlook MSG | poi-scratchpad | |
OpenXML4J | OOXML | poi-ooxml plus one of poi-ooxml-schemas, ooxml-schemas | Only one schemas jar is needed, see below for differences |
XSSF | Excel XLSX | poi-ooxml | |
XSLF | PowerPoint PPTX | poi-ooxml | |
XWPF | Word DOCX | poi-ooxml | |
Common SS | Excel XLS and XLSX | poi-ooxml | WorkbookFactory and friends all require poi-ooxml, not just core poi |
詳情可以到官網http://poi.apache.org/components/上檢視。
使用poi生成excel檔案
java使用poi的api生成excel時,有清晰的邏輯過程,每一行代碼的操作可以準确對應到在excel檔案上的每一個操作。 下面的例子沒有解釋那麼詳細,因為很多方法作用熟悉excel的人一眼就看出來了
//建立Workbook 對應一個excel檔案
HSSFWorkbook workbook = new HSSFWorkbook();
//建立sheet 對應excel檔案中的sheet
HSSFSheet sheet = workbook.createSheet("handleKnot");
//設定列寬 256代表一個字元寬度 10*256代表10個字元寬度
sheet.setColumnWidth(0, 10*256);
//生成行
Row row_1 = sheet.createRow(0)
Row row_2 = sheet.createRow(1)
//設定行高
row_1.setHeightInPoints(80);
row_2.setHeightInPoints(170);
//生成單元格
Cell cell_1_1 = row_1.createCell(0);
Cell cell_2_1 = row_2.createCell(0);
Cell cell_2_2 = row_2.createCell(1);
// 配置字型樣式
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short)18);
font.setFontName("黑體");
// 配置單元格樣式
CellStyle style = workbook.createCellStyle();
style.setWrapText(true);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setFont(font);
//為單元格配置樣式
cell_1_1.setCellStyle(style);
cell_2_1.setCellStyle(style);
cell_2_2.setCellStyle(style);
// 設定單元格值
cell_1_1.setCellValue("清遠醫保醫療智能稽核平台(事後)業務辦結表");
cell_2_1.setCellValue("需處理的業務事項");
//位單元格輸入特殊格式值 比如單元格值換行就要如下這樣寫
String teString = "mao \r\n kk \r\n justin";
HSSFRichTextString richTextString = new HSSFRichTextString(teString);
cell_2_2.setCellValue(richTextString);
// 合并單元格參數時起始行 、終止行、起始列、終止列
CellRangeAddress address1 = new CellRangeAddress(0,0,0,3);
sheet.addMergedRegion(address1);
// 設定合并單元格的邊框 這裡要注意被合并的單元格要先生成 在合并 不然這個邊框設定不生效
RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, address1, sheet, workbook);
RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, address1, sheet, workbook);
RegionUtil.setBorderRight(CellStyle.BORDER_THIN, address1, sheet, workbook);
RegionUtil.setBorderTop(CellStyle.BORDER_THIN, address1, sheet, workbook);
// 設定列印頁面
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
應用
在生産環境中,大多數的excel都是以一行标題+多行值得形式呈現的,這樣再每個單元格設定是相當低效的。
比如要将資料庫中查詢的資料存到excel中并導出,可以将标題與對應内容在庫中查詢時的key存在一個list中,再将庫中取出的資料key值與list中的key值比對,再存到對應位置。我說的相當粗略,對于每個單元格格式内容的生成都可以包裝