Java 使用 Apache poi 和 servlet 创建多个工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20064481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:47:47  来源:igfitidea点击:

Creating multiple sheets using Apache poi and servlets

javaservletsapache-poi

提问by spt

When i am creating multiple sheets using Apache poi and servlets. It is creating the sheet but not writing the data to file. I am trying to write the first 1000 records to sheet1 and next 1000 to sheet2 through below code, but not working

当我使用 Apache poi 和 servlet 创建多个工作表时。它正在创建工作表但不将数据写入文件。我正在尝试通过下面的代码将前 1000 条记录写入 sheet1,然后将下 1000 条记录写入 sheet2,但不起作用

private void writeDataToExcelFile(String string,
        ArrayList<ArrayList<String>> excelData, OutputStream outputStream) {
    HSSFWorkbook myWorkBook = new HSSFWorkbook();
    String sheetName = "";
    sheetName = "Document-" + 0;
    HSSFSheet mySheet = myWorkBook.createSheet();
    HSSFRow myRow = null;
    HSSFCell myCell = null;
    for (int rowNum = 0; rowNum < excelData.size(); rowNum++) {
        ArrayList<String> rowData = excelData.get(rowNum);
        if(rowNum>0 && rowNum%1000 == 0)
        {
            sheetName = "Document-" + (rowNum/1000);
            mySheet = myWorkBook.createSheet();
        }
        myRow = mySheet.createRow(rowNum);
        for (int cellNum = 0; cellNum < rowData.size(); cellNum++) {
            myCell = myRow.createCell(cellNum);
            myCell.setCellValue(rowData.get(cellNum));
        }
    }
    System.out.println("Last row:" + mySheet.getLastRowNum());
    System.out.println("Row number:" + mySheet.rowIterator().next().getRowNum());
    try {
        myWorkBook.write(outputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

What is wrong with my logic.Please do the needful help. Thanks

我的逻辑有什么问题。请提供必要的帮助。谢谢

采纳答案by Crollster

When you loop through the dataset, you are wanting to split at row 1000 to start a new sheet, which is fine, however when you start the new sheet, the next row you create is row 1001 (the outer loop index variable)

当您遍历数据集时,您希望在第 1000 行拆分以开始新工作表,这很好,但是当您开始新工作表时,您创建的下一行是第 1001 行(外循环索引变量)

myRow = mySheet.createRow(rowNum);

To get the effect you wish, change the loop to be something like this:

要获得您想要的效果,请将循环更改为如下所示:

int currentRow = 0;
for (int rowNum = 0; rowNum < excelData.size(); rowNum++) 
{
  ArrayList<String> rowData = excelData.get(rowNum);

  if(currentRow == 1000)
  {
    sheetName = "Document-" + (rowNum/1000);
    mySheet = myWorkBook.createSheet();
    currentRow = 0;
  }
  myRow = mySheet.createRow(currentRow);
  for (int cellNum = 0; cellNum < rowData.size(); cellNum++) 
  {
    myCell = myRow.createCell(cellNum);
    myCell.setCellValue(rowData.get(cellNum));
  }

  currentRow++;
}

I haven't compiled this, so I don't know if it'll work right away, but it should point you in the right direction.

我还没有编译这个,所以我不知道它是否会立即起作用,但它应该为你指明正确的方向。

HTH

HTH

Edit
Thinking about this further, you could get the same effect from making a 1 line change to the original application (albeit losing a little bit of clarity):

编辑
进一步考虑这一点,您可以通过对原始应用程序进行 1 行更改获得相同的效果(尽管失去了一点清晰度):

myRow = mySheet.createRow(rowNum%1000);