我想创建一个包含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();
}
}
答案 0 :(得分: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)
}