简体   繁体   English

无法通过多个for循环写入Excel行列(apache poi)

[英]Not able to write excel row column (apache poi) by multiple for loop

I have a xlsx file which has multiple sheet i am using apache poi for writing excel, in sheet2 i have 2 columns each column i want to populate by running a for loop , but i see that only last for loop get written previous one get blank in final written output file, i want to write both column by these for loop please help . 我有一个xlsx文件,其中有多个工作表,我正在使用apache poi编写excel,在sheet2中,我每行有2列,我想通过运行for循环来填充,但是我看到只有最后一个for循环才被写入,而前一个则为空。在最终的书面输出文件中,我想通过forfor循环写入这两列,请帮助。

for(int  i=0;i<fileNamesArray.length;i++)
    {
        XSSFRow row = worksheet1.createRow(i+1);
        cell = row.createCell(0);

        cell.setCellValue(fileNamesArray[i].toString());

    }//this dont get written 



    for(int i=0;i<fileDatesArray.length;i++)
    {
        XSSFRow row = worksheet1.createRow(i+1);
        cell = row.createCell(1);
        cell.setCellValue(fileDatesArray[i].toString());

    }//only this get written

this is complete code 这是完整的代码

    public class DashBoard {

    public void writeDashBoard() throws IOException, SQLException
    {
        CODToolUtil codToolUtil = new CODToolUtil();
        // Read property file to initialize constants
        String templateDashBoardFile = codToolUtil.getPropValues("templateDashBoardFile");
        String outputDir = codToolUtil.getPropValues("outputDir");
        String dirSeprator = codToolUtil.getPropValues("dirSeprator");
        String fdate = CODToolUtil.getDate();
        CODDAO coddao=new CODDAO();
        LinkedHashSet<String> hs= new LinkedHashSet<String>();
        LinkedHashSet<String> hs1= new LinkedHashSet<String>();
        FileInputStream fsIP= new FileInputStream(new File(templateDashBoardFile)); //Template file
        XSSFWorkbook wb = new XSSFWorkbook(fsIP);

        XSSFSheet worksheet = wb.getSheetAt(0);



        Cell cell = null; 

        cell = worksheet.getRow(1).getCell(0);
        cell.setCellValue(CODToolUtil.getDate());//Date
        cell = worksheet.getRow(1).getCell(1);
        int allfiles=coddao.getAllfiles();
        cell.setCellValue(allfiles);//All Files

        cell = worksheet.getRow(1).getCell(2);
        int callfilesY=coddao.getAllProcessedfilesCallY();
        cell.setCellValue(callfilesY);//All Y Files

        cell = worksheet.getRow(1).getCell(3);
        int callfilesN=coddao.getAllProcessedfilesCallN();
        cell.setCellValue(callfilesN);//All N Files

        cell = worksheet.getRow(1).getCell(4);
        int allLTE=coddao.getAllProcessedfilesLTE();
        cell.setCellValue(allLTE);//All LTE Files

        cell = worksheet.getRow(1).getCell(5);
        int allWCDMA=coddao.getAllProcessedfilesWCDMA();
        cell.setCellValue(allWCDMA);//All WCDMA Files
        //Sheet 0 OverView Complete
        //Sheet 1 Successfull CT
        XSSFSheet worksheet1 = wb.getSheetAt(1);

        hs=coddao.getAllProcessedfilesNameY();
        hs1=coddao.getAllProcessedfilesDateY();
        Object[] fileNamesArray =  hs.toArray();
        Object[] fileDatesArray =  hs1.toArray();





        for(int  i=0;i<fileNamesArray.length;i++)
        {
            XSSFRow row = worksheet1.createRow(i+1);
            cell = row.createCell(0);

            cell.setCellValue(fileNamesArray[i].toString());

        }//this dont get written 



        for(int i=0;i<fileDatesArray.length;i++)
        {
            XSSFRow row = worksheet1.createRow(i+1);
            cell = row.createCell(1);
            cell.setCellValue(fileDatesArray[i].toString());

        }//only this get written



        fsIP.close();
        File saveDirectory = new File(outputDir);// Create OutPutDirectory
        saveDirectory.mkdir();
        String savefilePath = saveDirectory.getAbsolutePath();
        FileOutputStream output_file = newFileOutputStream(newFile(savefilePath+dirSeprator+fdate+"-"+templateDashBoardFile)); // save in output
        wb.write(output_file); // write changes save it.
        output_file.close(); // close the stream




    }

public static void main(String[] args) throws IOException, SQLException {
    new DashBoard().writeDashBoard();
}
}

You are creating the same row twice - probably overriding the "first" row created in the first loop, with the "second" row created in the second loop. 您将创建同一行两次-可能会覆盖在第一个循环中创建的“第一”行,以及在第二个循环中创建的“第二”行。

If fileNamesArray and fileDatesArray are the same size, you can combine the loops as: 如果fileNamesArray和fileDatesArray的大小相同,则可以将循环组合为:

for(int  i=0;i<fileNamesArray.length;i++)
{
    XSSFRow row = worksheet1.createRow(i+1);
    cell1 = row.createCell(0);
    cell1.setCellValue(fileNamesArray[i].toString());

    cell2 = row.createCell(1);
    cell2.setCellValue(fileDatesArray[i].toString());
}

check which array is bigger and loop through it first , then loop through the second array, but instead of using worksheet1.createRow(i+1) - use worksheet1.getRow(i+1) , reusing the row element you created in the first loop. 检查哪个数组更大并首先遍历该数组,然后遍历第二个数组,但不要使用worksheet1.createRow(i+1) -使用worksheet1.getRow(i+1) ,重新使用在第一个数组中创建的行元素环。

Note: in theory, even if the arrays are of different sizes you can still use one loop, just make sure you apply relevant checks to avoid ArrayIndexOutOfBoundsException. 注意:从理论上讲,即使数组的大小不同,您仍然可以使用一个循环,只需确保应用相关检查即可避免ArrayIndexOutOfBoundsException。

Try 尝试

for(int  i=0;i<fileNamesArray.length;i++)
{
    XSSFRow row = worksheet1.createRow(i+1);
    cell = row.createCell(0);
    cell.setCellValue(fileNamesArray[i].toString());
    cell = row.createCell(1);
    cell.setCellValue(fileDatesArray[i].toString());
}

Instead of of using those 2 loops. 而不是使用那两个循环。 I would imagine you are overwriting the your row when you call worksheet1.createRow in the second loop. 我想您在第二个循环中调用worksheet1.createRow时,您将覆盖您的行。

gradeList is an ArrayList of strings with the value "80", "81" ... "85" gradeList是一个字符串数组ArrayList,其值为“ 80”,“ 81” ...“ 85”

         for(int y = 0; y < gradeList.size(); y++){
            HSSFRow row1 = worksheet.createRow((short) 1);//1
            HSSFCell cell1 =row1.createCell((short) y+1);//2
            cell1.setCellValue("" + gradeList.get(y));//3
            HSSFCellStyle cellStylei = workbook.createCellStyle();//4
            cellStylei.setFillForegroundColor(HSSFColor.GREEN.index);
            cell1.setCellStyle(cellStylei);//6
         }

Output of Code: _, _, _, _, _, 85. intended Output: 80, 81, 82, 83, 84, 85. 代码输出:_,_,_,_,_,85。预期输出:80、81、82、83、84、85。

After changing the code to 将代码更改为

        HSSFRow row1 = worksheet.createRow((short) 1);//1
        HSSFCell cell1;
        for(int y = 0; y < gradeList.size(); y++){

            cell1 = row1.createCell((short) y+1);//2
            cell1.setCellValue("" + gradeList.get(y));//3

        }
        HSSFCellStyle cellStylei = workbook.createCellStyle();//4
        cellStylei.setFillForegroundColor(HSSFColor.GREEN.index);//5

the code prints 80, 81, 82, 83, 84, and 85 as intended but using the previous six line code it only prints 85. Can someone please explain to me why is first one wrong or not working, and if possible also can you please also explain what lines 4,5, and 6 do. 该代码按预期打印80、81、82、83、84和85,但使用前面的六行代码仅打印85。有人可以向我解释为什么第一个错误或不起作用,如果可以的话,您也可以还请说明第4、5和6行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM