0

我想创建一个包含 5 张工作表的 excel 文档,工作表的数据是动态的,并且我有最大行数限制。目前我的方法是一次创建一张纸,并用数据填充它。我正在检查是否超过了最大行数,并创建了一个新的 Excel 文档。但是,这不会等待在创建新工作簿之前检查其他工作表是否也超过最大行限制

我的代码示例

private void populateDetailsSheets(String[] data) throws IOException
   {
      currentRow = getCurrentRow();
      rowCount++;
      short cellNumber = 0;
      for (String value : data)
      {
         POIExcelUtil.createCellWithContent(currentRow,value,cellNumber++).setCellStyle(contentStyle);
      }
      writeToFileOnExhaustingMaxRows();
   }



private void writeToFileOnExhaustingMaxRows() throws IOException
   {
      if(sheet.getLastRowNum() + 2 > Integer.valueOf(SystemProperty.MAX_RECORDS_PER_EXCEL))
      {
         writeToFile();
         rowCount = 0;
         createWorkbook();
         titleStyle = createTitleStyle();
         headerStyle = createHeaderStyle();
         columnHeaderStyle = createColumnHeaderStyle();
         columnHeaderStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
         columnHeaderStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
         columnHeaderStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
         columnHeaderStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
         contentStyle = createCellStyle();
         contentStyle.setBorderLeft(CellStyle.BORDER_THIN);
         contentStyle.setBorderTop(CellStyle.BORDER_THIN);
         contentStyle.setBorderRight(CellStyle.BORDER_THIN);
         contentStyle.setBorderBottom(CellStyle.BORDER_THIN);
         rowCount = 0;
         createSheet("title sheet");
         populateReportSettingsSheet();

         rowCount =0;
         createSheet(sheetNames[index]);
         setColumnWidth();
         createColumnHeader();
      }
   }
4

1 回答 1

1

我假设你想在一张满时跳到下一张sheet。这里的诀窍。

public class JumpToNewSheet {
private static HSSFSheet allocateNewSheet(HSSFWorkbook wb, String sheetName) {
    HSSFSheet sheet = wb.createSheet(sheetName);
    /*You can add style here too or write header here*/
    return sheet;
}
public HSSFWorkbook exportExcel_xls(MyTable table) {
    int cellNumber = 12;
    List<TupleData> tuples = table.getTuples();
    HSSFWorkbook wb = new HSSFWorkbook();//Whole book
    HSSFSheet currentSheet = allocateNewSheet(wb, "SHEET");//Initial sheet
    int sheetCounter = 1;//Start at 1 because we already created Initial sheet
    int rowCounter = 0;//1st row in sheet
    for(TupleData t: tuples) {//Loop through data
        if(rowCounter % 65535 == 0) {//Max row reached, build new sheet
            //Increase sheetCounter
            sheetCounter++;
            String new_sheetName = "SHEET_"+sheetCounter;//Name of sheet
            currentSheet = allocateNewSheet(wb, new_sheetName);//Point currentSheet to new sheet
            //Reset rowCounter to 0
            rowCounter = 0;
        }
        Row row = currentSheet.createRow(rowCounter);
        for(int i=0; i <=cellNumber; i++) {
            Cell cell = row.createCell(i);
            //Write data.......
            //.............
        }//End inner for loop
        rowCounter++;
    }//End for loop
  }//End exportExcel_xls(MyTable table)
}
于 2017-10-24T15:07:57.890 回答