无法使用多个循环保留数据

时间:2018-06-18 06:16:21

标签: apache-poi

我正在尝试使用多个循环从一些字符串数组向excel表写入数据,但输出文件只包含我尝试编写的那些中的最后一个条目

int isinstart1 = 8;
int strdt1=0;
int bhstrdt1 = 0;
int mtFound1 =0;
int bhmtFound1 = 0;
String [] hdfc = {"APR_2013.xls", "MAY_2013.xls", "JUN_2013.xls", "JUL_2013.xls", "AUG_2013.xls", "SEP_2013.xls", "OCT_2013.xls", "NOV_2013.xls",
                    "DEC_2013.xls", "JAN_2014.xls", "FEB_2014.xls", "MAR_2014.xls", "APR_2014.xls", "MAY_2014.xls", "JUN_2014.xls", "JUL_2014.xls", "AUG_2014.xls",
                    "SEP_2014.xls", "OCT_2014.xls", "NOV_2014.xls", "DEC_2014.xls", "JAN_2015.xls", "FEB_2015.xls", "MAR_2015.xls", "APR_2015.xls", "MAY_2015.xls",
                    "JUN_2015.xls", "JUL_2015.xls", "AUG_2015.xls", "SEP_2015.xls", "OCT_2015.xls", "NOV_2015.xls", "DEC_2015.xls", "JAN_2016.xls", "FEB_2016.xls",
                    "MAR_2016.xls", "APR_2016.xls", "MAY_2016.xls", "JUN_2016.xls", "JUL_2016.xls", "AUG_2016.xls", "SEP_2016.xls", "OCT_2016.xls", "NOV_2016.xls",
                    "DEC_2016.xls", "JAN_2017.xls", "FEB_2017.xls", "MAR_2017.xls", "APR_2017.xls", "MAY_2017.xls", "JUN_2017.xls", "JUL_2017.xls", "AUG_2017.xls",
                    "SEP_2017.xls", "OCT_2017.xls", "NOV_2017.xls", "DEC_2017.xls", "JAN_2018.xls", "FEB_2018.xls", "MAR_2018.xls", "APR_2018.xls"};
            while (!hdfc[strdt1].startsWith(s1.toUpperCase()))
            {
                mtFound1 = strdt1;
                strdt1++;
            }
            while (!bhavCopyDates[bhstrdt1].toLowerCase().contains(s.toLowerCase()))
            {
                bhmtFound1 = bhstrdt1;
                bhstrdt1++;
            }

            for(int ser = mtFound1+1;ser<hdfc.length-1;ser++) {

                //System.out.println(ser);
                Workbook hdfcamc = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH+"/"+hdfc[ser]));

                outputRow = 10;
                for (int twice=1;twice<3;twice++) {
                outputRow = 10;
                 //for (int z = bhmtFound1; z< z+1/*(bhavCopyDates.length-1)*/; z++) { 
                    System.out.println('\n');
                    if(bhmtFound1<bhavCopyDates.length) {
                        Workbook bhavcopy = WorkbookFactory.create(new File("E:/MFDisclosures/BhavCopies/"+bhavCopyDates[bhmtFound1+1]));
                        Sheet sheet = bhavcopy.getSheetAt(0);
                        Sheet hdfcSheet = hdfcamc.getSheetAt(fundOption-1);
                        FileOutputStream outputStream = new FileOutputStream("D:\\"+FundNames[fundOption-1]+".xlsx");
                        for (isinstart1 = 8;isinstart1 < hdfcSheet.getPhysicalNumberOfRows()-1;isinstart1++ ) {
                            for (int rownum = 0; rownum < sheet.getPhysicalNumberOfRows()-1;rownum++) {
                                try {
                                    if (hdfcSheet.getRow(isinstart1).getCell(1).getStringCellValue().equals(sheet.getRow(rownum).getCell(2).getStringCellValue())) {
                                        //write to output sheet
                                        //System.out.println(hdfcSheet.getRow(isinstart1).getCell(1).getStringCellValue());                                                             
                                        Row oprow = sheet0.createRow(outputRow);
                                        Cell opcell = oprow.createCell(outputCell);
                                        opcell.setCellValue(hdfcSheet.getRow(isinstart1).getCell(3).getStringCellValue());
                                        outputCell++; 
                                        opcell = oprow.createCell(outputCell);
                                        opcell.setCellValue(hdfcSheet.getRow(isinstart1).getCell(7).getNumericCellValue());
                                        outputCell++;
                                        opcell = oprow.createCell(outputCell);
                                        opcell.setCellValue(sheet.getRow(rownum).getCell(7).getNumericCellValue());
                                        outputCell = outputCell-2;
                                        outputStream.flush();
                                    }
                                } catch (NullPointerException NPE) {
                                    continue;
                                }
                            }
                            outputRow++;

                        }   
                        workbook1.write(outputStream);
                        outputStream.close();
                        bhavcopy.close();
                        bhmtFound1++;
                        outputCell=outputCell+3;
                    }   
                 }
                hdfcamc.close();
            }
            workbook1.close();

请告诉我哪里出错了。我是Apache Poi的新手。 excel文件只有正确行中的最后一列。 此外,我可以在代码运行时看到excel文件更新,但可以看到该文件是为每个循环重写的。 在此先感谢您的帮助。

0 个答案:

没有答案