1.xls一個sheet只能裝65536行,多余則報錯
poi包導出或寫入excel超出65536報錯:
java.lang.IllegalArgumentException: Invalid row number (65536) outside allow
解決:每6w分一個sheet,關鍵代碼紅色部分
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.util.CellRangeAddress; import com.google.gson.JsonArray; import com.google.gson.JsonObject; public class ExcelUtil { public static HSSFWorkbook getHSSFWorkbook(String sheetName, JsonArray title, String[][] values, HSSFWorkbook wb) { // 第一步,創建一個HSSFWorkbook,對應一個Excel文件 if (wb == null) { wb = new HSSFWorkbook(); } // -個sheet限制存65536條,此處僅存60000 System.out.println(values.length); final int sheetNum = (int) Math.ceil((float) values.length / 60000); for (int n = 1; n <= sheetNum; n++) { // 第二步,在workbook中添加一個sheet,對應Excel文件中的sheet final HSSFSheet sheet = wb.createSheet(sheetName + "_" + n); System.out.println("sheetName" + sheetName + "_" + n); sheet.setDefaultColumnWidth(12); // 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制 HSSFRow row; // 第四步,創建單元格,並設置值表頭樣式 final HSSFCellStyle headerStyle = wb.createCellStyle(); headerStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中 headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中 final Font fontStyle = wb.createFont(); // 字體樣式 fontStyle.setBoldweight(Font.BOLDWEIGHT_BOLD); // 加粗 fontStyle.setFontName("黑體"); // 字體 fontStyle.setFontHeightInPoints((short) 11); // 大小 // 將字體樣式添加到單元格樣式中 headerStyle.setFont(fontStyle); // 邊框 headerStyle.setBorderBottom(CellStyle.BORDER_THIN); headerStyle.setBorderLeft(CellStyle.BORDER_THIN); headerStyle.setBorderRight(CellStyle.BORDER_THIN); headerStyle.setBorderTop(CellStyle.BORDER_THIN); // 普通單元格樣式,邊框,水平居中 final HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中 cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); // cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); final HSSFDataFormat df = wb.createDataFormat(); // 此處設置數據格式 cellStyle.setDataFormat(df.getFormat("#,#0.0")); // 小數點后保留兩位,可以寫contentStyle.setDataFormat(df.getFormat("#,#0.00")); // 聲明列對象 HSSFCell cell = null; // 創建標題 JsonArray cellArray = new JsonArray(); JsonObject object = new JsonObject(); JsonObject temobj = new JsonObject(); int rowSkip = 0, cellSkip = 0; String tempCell = ""; for (int rowId = 0; rowId < title.size(); rowId++) { row = sheet.createRow(rowId); object = title.get(rowId).getAsJsonObject(); cellArray = object.get("row").getAsJsonArray(); // colId為excel列索引,cellId為行標題值的數組索引,cellId遇到當前單元格已使用時,填充至下一個可使用的單元格 for (int colId = 0, cellId = 0; cellId < cellArray.size(); colId++) { cell = row.createCell(colId); cell.setCellStyle(headerStyle); if (isMergedRegion(sheet, rowId, colId)) { continue; } temobj = cellArray.get(cellId).getAsJsonObject(); tempCell = temobj.get("cellvalue").toString().replace("\"", ""); try { // System.out.println(tempCell); tempCell = new String(tempCell.getBytes("UTF-8"), "ISO-8859-1"); // System.out.println(tempCell); tempCell = new String(tempCell.getBytes("ISO-8859-1"), "UTF-8"); // System.out.println(tempCell); } catch (final Exception e) { e.printStackTrace(); } cell.setCellValue(tempCell); // System.out.println(cell); // System.out.println(cell.getStringCellValue()); cell.setCellStyle(headerStyle); cellId++; // 合並單元格 rowSkip = temobj.get("rowspan").getAsInt(); cellSkip = temobj.get("colspan").getAsInt(); // System.out.println(rowSkip + "=skip==" + cellSkip); if (rowSkip > 1 && rowSkip-- > 0 || cellSkip > 1 && cellSkip-- > 0) {// 用於起始行列計算時需減1 final CellRangeAddress cra = new CellRangeAddress(rowId, rowId + rowSkip, colId, colId + cellSkip); // 起始行, 終止行, 起始列, 終止列 // 終止行, sheet.addMergedRegion(cra); } } } // 創建內容 -個sheet只能存65536條 for (int i = 0; i < 60000 && i < values.length - (n - 1) * 60000; i++) { row = sheet.createRow(title.size() + i); for (int j = 0; j < values[i].length; j++) { // 將內容按順序賦給對應的列對象 cell = row.createCell(j); cell.setCellValue(values[(n - 1) * 60000 + i][j]); cell.setCellStyle(cellStyle); } } } return wb; } public static boolean isMergedRegion(HSSFSheet sheet, int row, int column) { final int sheetMergeCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergeCount; i++) { final CellRangeAddress range = sheet.getMergedRegion(i); final int firstColumn = range.getFirstColumn(); final int lastColumn = range.getLastColumn(); final int firstRow = range.getFirstRow(); final int lastRow = range.getLastRow(); if (row >= firstRow && row <= lastRow) { if (column >= firstColumn && column <= lastColumn) { return true; } } } return false; } }