gpt4 book ai didi

java - 将数据拆分为多张 Apache POI

转载 作者:行者123 更新时间:2023-11-30 08:18:32 26 4
gpt4 key购买 nike

我正在为我的 spring mvc webapp 使用 apache poi。我是 apache poi 的新手,我设法将数据生成到 .xls,数据是使用 model 从我的 Controller 传递的。

我的目标是当生成的数据数量达到 25 行时,我想继续生成下一个 25 行(26-50)到另一张表,依此类推。

@Override
protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

int rnum = 1;

response.setHeader("Content-Disposition", "attachment; filename=\"" + new Date() + ".xls\"");
List<Employee> employeeList = (List<Employee>) model.get("empRecords");//get from my controller

HSSFSheet excelSheet = workbook.createSheet("Employee List"+rnum);
setExcelHeader(excelSheet);
setExcelRows(excelSheet, employeeList);
}

public void setExcelHeader(HSSFSheet excelSheet) {
HSSFRow excelHeader = excelSheet.createRow(0);
excelHeader.createCell(0).setCellValue("Id");
}

public void setExcelRows(HSSFSheet excelSheet, List<Employee> empList) {
int record = 1;
for (Employee a : empList) {
HSSFRow excelRow = excelSheet.createRow(record++);
excelRow.createCell(1).setCellValue(a.getId());
}
}

以上是我目前所做的,我该如何进一步进行?

最佳答案

传递工作簿而不是电子表格。这将允许您在函数中创建一个新工作表。

 public void setExcelRows(HSSFWorkbook wb, List<Employee> empList) {
int record = 1; int sheetNum = 0;
HSSFSheet sheet = wb.createSheet("Employee List "+sheetNum);
setExcelHeader(sheet);
for (Employee a : empList) {
if (record == 26){
sheetNum++;
sheet= wb.createSheet("Employee List "+ sheetNum);
setExcelHeader(sheet);
record = 1;
}
HSSFRow excelRow = sheet.createRow(record++);
excelRow.createCell(1).setCellValue(a.getId());
}

关于java - 将数据拆分为多张 Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com