读取相同excel文件后写入时出错-java poi

读取相同excel文件后写入时出错-java poi,java,excel,apache-poi,Java,Excel,Apache Poi,两天来,我一直被困在这个问题上,没有取得任何突破。我使用Java8和ApachePOI进行所有与excel相关的工作。 基本上,流程如下图所示: 我希望将“编辑或附加”值添加到现有工作表中,而不是覆盖它。考虑到上图有4个阶段,我使用的一些代码是: 第二阶段(B):将文件从文件x.xlsx写入文件a.xlsx: try{ public FileOutputStream out = new FileOutputStream(new File("c://file_path_file_a.x

两天来,我一直被困在这个问题上,没有取得任何突破。我使用Java8和ApachePOI进行所有与excel相关的工作。 基本上,流程如下图所示:

我希望将“编辑或附加”值添加到现有工作表中,而不是覆盖它。考虑到上图有4个阶段,我使用的一些代码是:

第二阶段(B):将文件从
文件x.xlsx
写入
文件a.xlsx

try{

    public FileOutputStream out = new FileOutputStream(new File("c://file_path_file_a.xlsx"));
    public XSSFWorkbook newWorkbook = new XSSFWorkbook();
    public XSSFSheet sheet = newWorkbook.createSheet("vertical");


                for(String key : map_data.keySet()){
                    List<String> values = map_data.get(key);

                    // for values in each key, store it in sheet
                    for (String value:values){

                    // make instance of new row that will print data 
                    // simultaneously in two columns
                        Row newRow = sheet.createRow(row);

                        newRow.createCell(0).setCellValue(key); // 1st column
                        newRow.createCell(6).setCellValue(value); // 6th column

                        row++;
                        }

                }

                // write the contents of workbook into the excel file
                newWorkbook.write(out);

                // close all the respective files and workbooks
                out.close();
                newWorkbook.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
try {

      public FileOutputStream output_excel_file = new FileOutputStream(new  File("c://file_path_file_a.xlsx"));
      public XSSFWorkbook workbook = new XSSFWorkbook();
      public XSSFSheet sheet = workbook.getSheet("vertical");


            for (int i=0; i<qvm_1st_col.size(); i++) {

                if (error_text.contains(1st_col.get(i))) {

                    int index = error_text.indexOf(1st_col.get(i));
                    String value = dsps.get(index);

                    Row row = sheet.createRow(i);
                    row.createCell(12).setCellValue(value); // append values to 12th column


                }

            } // for loop

            output_excel_file.close();
            workbook.close();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
所以D阶段的代码失败了,算法就停在那里

我的尝试:

try{

    public FileOutputStream out = new FileOutputStream(new File("c://file_path_file_a.xlsx"));
    public XSSFWorkbook newWorkbook = new XSSFWorkbook();
    public XSSFSheet sheet = newWorkbook.createSheet("vertical");


                for(String key : map_data.keySet()){
                    List<String> values = map_data.get(key);

                    // for values in each key, store it in sheet
                    for (String value:values){

                    // make instance of new row that will print data 
                    // simultaneously in two columns
                        Row newRow = sheet.createRow(row);

                        newRow.createCell(0).setCellValue(key); // 1st column
                        newRow.createCell(6).setCellValue(value); // 6th column

                        row++;
                        }

                }

                // write the contents of workbook into the excel file
                newWorkbook.write(out);

                // close all the respective files and workbooks
                out.close();
                newWorkbook.close();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
try {

      public FileOutputStream output_excel_file = new FileOutputStream(new  File("c://file_path_file_a.xlsx"));
      public XSSFWorkbook workbook = new XSSFWorkbook();
      public XSSFSheet sheet = workbook.getSheet("vertical");


            for (int i=0; i<qvm_1st_col.size(); i++) {

                if (error_text.contains(1st_col.get(i))) {

                    int index = error_text.indexOf(1st_col.get(i));
                    String value = dsps.get(index);

                    Row row = sheet.createRow(i);
                    row.createCell(12).setCellValue(value); // append values to 12th column


                }

            } // for loop

            output_excel_file.close();
            workbook.close();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
除了上面的代码外,我还试图阅读这里提到的关于堆栈溢出的同一问题的多个答案,但我无法理解它将如何解决我的问题,因为我将编写两次输出excel文件,或者将来可能会更多。而且,一切都应该在一次执行中发生。因此,在
1级
完成工作后,
2级
应开始工作,以此类推

我必须读取以前的数据,并从其他参考文件中附加更多数据。这件事让我有点不知所措

谢谢你抽出时间

p.S:
我还读取了阶段B和阶段C之间的输出文件,以在
arraylist
中获得一些值。我这样做只是为了存储输出excel文件中的一些主要生成键

我想当您想将新列附加到现有工作表时,应该使用
sheet.getRow(I)
而不是
sheet.createRow(I)
。在上一个代码中
xssf工作簿=new xssf工作簿()创建新的空工作簿。所以
XSSFSheet sheet=workbook.getSheet(“垂直”)必须失败,因为此新工作簿中没有名为“垂直”的工作表。您说您已经在阶段a中读取了Excel文件。因此,在阶段C中,您还需要通过读取“文件a.xlsx”而不是创建新工作簿来获取工作簿。@AxelRichter您认为我只需要将该工作簿从该类导入到当前类中,并在其中添加列吗?你能详细解释一下吗?谢谢。那篇文章帮助我在工作表中添加数据。我面临的唯一问题是,输出现在正确地显示在我创建的新excel文件中,而不是在同一个文件中。如果我尝试在同一个文件中写入所有内容,则该文件会由于某些问题而损坏。我正试图弄明白这一点。