无法写入Excel文件(Apache POI)

时间:2018-09-03 08:58:05

标签: 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”的代码段。必须使用arraylists reg_id,doc_id和status的值来填充单元格。但是,当我运行程序时,仅索引位置0处的列表的值被写入文件。我是否在for循环中放了一条特定的语句?

1 个答案:

答案 0 :(得分:1)

您需要将workbook.write(out);移出循环。 workbook.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文件的基本示例。