使用 Apache poi 和 servlet 创建多个工作表 [英] Creating multiple sheets using Apache poi and servlets

查看:28
本文介绍了使用 Apache poi 和 servlet 创建多个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

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

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)

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

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

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):

myRow = mySheet.createRow(rowNum%1000);

这篇关于使用 Apache poi 和 servlet 创建多个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆