xls適合百萬級別如下數據表,HSSFWORKBOOK操做.xls的excel文檔,支持模板java
工程結構 apache
代碼eclipse
utilthis
package com.gzn.poi.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; public class WbUtil { /** * 讀取模板 * @param path * @param templateName * @return Workbook * @throws IOException */ public static Workbook getTemplate(String path, String templateName) throws IOException { FileInputStream is = null; Workbook wb = null; is = new FileInputStream(path + templateName); wb = new HSSFWorkbook(is); WbUtil.closeAll(is, null, wb); return wb; } /** * 獲取行單元格樣式列表 * @param nRow 行 * @param cells 單元格數 * @param start 單元格起始下標 * @return List<CellStyle> */ public static List<CellStyle> rowCellsStyle(Row nRow, int cells, int start) { List<CellStyle> cellStyleList = new ArrayList<CellStyle>(cells); if (nRow == null) return cellStyleList; for (int i = 0; i < cells; i++) { Cell cell = nRow.getCell(start++); CellStyle cellStyle = cell.getCellStyle(); cellStyleList.add(cellStyle); } return cellStyleList; } /** * 關閉資源 * @param is * @param os * @param wb */ public static void closeAll(FileInputStream is, FileOutputStream os, Workbook wb) { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (wb != null) { wb.close(); } } catch (IOException e) { e.printStackTrace(); } } }
demospa
package com.gzn.poi.demo; import java.io.FileOutputStream; import java.io.IOException; import java.util.List;
import java.util.ArrayList; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import com.gzn.poi.util.WbUtil; public class Demo1 { public static void main(String[] args) { Workbook wb = null; FileOutputStream os = null;
Demo1 demo1 = new Demo1(); try { wb = demo1.contractProductPrint(); // 輸出數據到磁盤/供用戶下載 os = new FileOutputStream("F:\\poi-demo\\xxx出貨表.xls"); wb.write(os); } catch (Exception e) { e.printStackTrace(); } finally { WbUtil.closeAll(null, os, wb); } } /** * 模板打印 * @return Workbook * @throws IOException */ public Workbook contractProductPrint() throws IOException {
List list = null; Workbook wb = null; wb = WbUtil.getTemplate("F:\\poi-demo\\", "tContractProduct.xls"); // 獲取模板
// 獲取數據集合
// list = ... this.generateContractProductXLS(wb, 0, list); // sheet表生成數據 WbUtil.closeAll(null, null, wb); return wb; } /** * 生成對應的sheet表,並填充數據 * @param wb 工做簿 * @param sheetIndex sheet表 * @param list 數據源 */ public void generateContractProductXLS(Workbook wb, int sheetIndex, List list) { Row nRow = null; Cell nCell = null; Integer rowNo = 0; Integer cellNo = 1; Sheet sheet = wb.getSheetAt(0); // 獲取工做簿的第一個工做表 nRow = sheet.getRow(rowNo++); // 獲取行(大標題行) nCell = nRow.getCell(cellNo); // 獲取行->單元格 String bigTitleCellContent = nCell.getStringCellValue(); // 獲取單元格內容(大標題) CellStyle bigTitleCellStyle = nCell.getCellStyle(); // 獲取單元格樣式(大標題) // 小標題 rowNo++; // 獲取輸出內容行 nRow = sheet.getRow(rowNo); short rowHeight = nRow.getHeight(); // 設置行高 List<CellStyle> rowCellsStyle = WbUtil.rowCellsStyle(nRow, 8, 1); int rowCellsStyleIndex = 0; for (int i = 0; i < 10; i++) { // 遍歷list cellNo = 1; // 重置單元格遊標 nRow = sheet.createRow(rowNo++); // 建立行 nRow.setHeight(rowHeight); // 設置行高 nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格 1內容"); // 單元格插入對象屬性值 nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格2內容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格3內容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格4內容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格5內容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格6內容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格7內容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("單元格8內容"); rowCellsStyleIndex = 0; } } }
模板debug
結果excel
*關於eclipse自動彈出debug窗口
this kind of launch is configured to open the Debug perspective when it...
查看程序是否沒有釋放資源,特別是try catch時,加上finally釋放資源解決問題code