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

时间:2015-01-29 11:16:34

标签: java excel apache apache-poi

我有一个xlsx文件,其中有多个工作表我正在使用apache poi编写excel,在sheet2中我有2列 我希望通过运行for循环来填充每一列,但我看到只有最后一个循环得到写入前一个在最终写入的输出文件中得到空白,我想写这两个列for循环请帮助。

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

这是完整的代码

    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();
}
}

3 个答案:

答案 0 :(得分:3)

您正在创建两次相同的行 - 可能会覆盖&#34;第一个&#34;在第一个循环中创建的行,使用&#34; second&#34;在第二个循环中创建的行。

如果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());
}

检查哪个数组更大并循环第一个,然后遍历第二个数组,但不使用worksheet1.createRow(i+1) - 使用worksheet1.getRow(i+1),重复使用行元素在第一个循环中创建。

注意:理论上,即使数组大小不同,您仍然可以使用一个循环,只需确保应用相关检查以避免ArrayIndexOutOfBoundsException。

答案 1 :(得分:0)

尝试

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());
}

而不是使用那两个循环。我想你在第二个循环中调用worksheet1.createRow时会覆盖你的行。

答案 2 :(得分:-1)

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
         }

代码输出:_,_,_,_,_,85。 预期产出:80,81,82,83,84,85。

将代码更改为

        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

代码按照预期打印80,81,82,83,84和85但使用前六行代码只打印85.有人可以向我解释为什么第一个错误或不工作,如果可能的话还请你解释第4,5和6行是做什么的。