java - 写入 Excel 文件时出现问题 (Apache POI)

标签 java excel apache apache-poi

public static String already_exists(ArrayList<String> reg_id, ArrayList<String> doc_id, ArrayList<String> status) throws RowsExceededException, WriteException, IOException{
         WritableWorkbook myFirstWbook = null;
         FileOutputStream out = new FileOutputStream(new File("C://Users//Desktop//OP_demo.xlsx"));
                XSSFWorkbook workbook = new XSSFWorkbook();
                 XSSFSheet sheet = workbook.createSheet("Java Books");

                 int rowCount = 0;
                 int columnCount = 0;
                 for(int i=0;i<reg_id.size();i++)
                    {
                      Object[][] bookData={{reg_id.get(i),doc_id.get(i),status.get(i)}};
                      Row row = sheet.createRow(rowCount++); 
                      //System.out.println(reg_id.get(i)+doc_id.get(i)+status.get(i));
                      for (Object[] aBook : bookData) 
                        {

                         for (Object field : aBook) 
                            {
                             org.apache.poi.ss.usermodel.Cell cell = row.createCell(columnCount++);

                                {
                                 cell.setCellValue(field.toString());
                                }

                            }
                         workbook.write(out);


                      }


                 }

                 out.close();  

        return "";
    }

上面是我用来写入 Excel 文件“OP_Demo”的代码片段。必须使用数组列表 reg_id、doc_id 和 status 的值来填充单元格。但是,当我运行该程序时,只有索引位置 0 处的列表的值才会写入文件。我是否在 for 循环中放错了某个特定语句?

最佳答案

您需要将 workbook.write(out); 移到循环之外。 工作簿.write(out);将工作簿的内容写入文件流,处理完所有记录后需要写入文件,否则每次执行时都会覆盖文件内容。

public static String already_exists(ArrayList<String> reg_id, ArrayList<String> doc_id, ArrayList<String> status) throws RowsExceededException, WriteException, IOException{
             WritableWorkbook myFirstWbook = null;
             FileOutputStream out = new FileOutputStream(new File("C://Users//Desktop//OP_demo.xlsx"));
                    XSSFWorkbook workbook = new XSSFWorkbook();
                     XSSFSheet sheet = workbook.createSheet("Java Books");

                     int rowCount = 0;
                     int columnCount = 0;
                     for(int i=0;i<reg_id.size();i++)
                        {
                          Object[][] bookData={{reg_id.get(i),doc_id.get(i),status.get(i)}};
                          Row row = sheet.createRow(rowCount++); 
                          //System.out.println(reg_id.get(i)+doc_id.get(i)+status.get(i));
                          for (Object[] aBook : bookData) 
                            {

                             for (Object field : aBook) 
                                {
                                 org.apache.poi.ss.usermodel.Cell cell = row.createCell(columnCount++);

                                    {
                                     cell.setCellValue(field.toString());
                                    }

                                }

                          }

                     }

                     workbook.write(out); // writing the workbook to file-stream outside loop
                     out.close();  

            return "";
        }

您可以引用以下链接:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ 它展示了使用 apache POI 读写 excel 文件的基本示例。

关于java - 写入 Excel 文件时出现问题 (Apache POI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52146383/

相关文章:

java - 如何在图形下方的中心添加按钮并将其与 javafx 中的 Controller 文件对齐?

excel - 复制一系列单元格并仅选择包含数据的单元格

java - 如何使用 API 导出跑道中的评论部分

excel - CSV 文件创建,如 unicodetext、endig .csv。 VBA 无法打开、保存和关闭更改

apache - 使用带有 httpd 和 tomcat 的 mod_jk 发布加载资源

java - 具有抽象类的构造函数 - 不接受特定的

Java,在数组上插入对象的方法,返回 boolean 值

java - 如何使用 Apache POI 在不同行创建多个自动筛选器

Apache选项-索引配置不起作用

java.net 源代码阅读器跳行