繁体   English   中英

如何使用Apache Poi逐列编写

[英]How to write column by column using Apache poi

我有如下地图:

Map<String, Map<String, List<String>>> finalDataSet = new LinkedHashMap<>();

它具有如下数据:

{Name1={CurrentUserStory=[AB-1413, AB-1504], CUS_Status=[In Testing, Ready for Deployment], SubTask=[AB-1547, AB-1508], ST_Status=[In Development, Done], TimeSpent=[19800, 23400]},Name2={CurrentUserStory=[AB-1377, AB-149],CUS_Status=[Ready for Testing, Ready for Development],SubTask=[AB-1545, AB-1510], ST_Status=[Ready for Testing, Closed],TimeSpent=[25200, 5400]}}

我想以以下格式将上述数据写入excel。

在此处输入图片说明

当试图将这些数据写入excel时,只有“ TimeSpent”列正在写入,所有列均为空。这是我的代码:

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sh = wb.createSheet("CSG_Report");
Row row = sh.createRow(2);
for (Map.Entry<String, Map<String, List<String>>> m : finalDataSet.entrySet()) {
    int col=1;
    row= sh.createRow(index);
    int userindex=index;
    for(Map.Entry<String, List<String>> innerMap:m.getValue().entrySet())
    {
        index = userindex;
        List<String> val=innerMap.getValue();
        row= sh.createRow(index);
        for (String string : val) {
            row= sh.createRow(index);
            row=sh.getRow(index);
            Cell cell = row.createCell(col);
            cell.setCellValue(string);
            index++;
        }
        col++;
    }
}
FileOutputStream out = new FileOutputStream(new
        File("D:\\Output.xlsx"));
wb.write(out);
out.close();
wb.close();

请帮助我完成它。 感谢您的帮助。 提前致谢。

我为我的问题找到了解决方案。 这是我的代码。

int columnNum=1;
    int fRow=2;
    List<String> cvals = null;
    for (Map.Entry<String, Map<String, List<String>>> m : finalDataSet.entrySet()) {
        Row row = sh.createRow(fRow);
        Cell cell1=row.createCell(0);
        cell1.setCellValue("Name1");

        for(Map.Entry<String, List<String>> innerMap:m.getValue().entrySet()){
            cvals=innerMap.getValue();

             int tempHeight=cvals.size();
             int temp=fRow;
             for (String cval : cvals) {
                 Row row2;
                 if(sh.getPhysicalNumberOfRows()-1>temp-1)
                    {

                        row2=sh.getRow(temp);
                    }
                    else
                    {
                        row2=sh.createRow(temp);
                    }
                 Cell cell3=row2.createCell(columnNum);
                 cell3.setCellValue(cval);
                 temp=temp+1;
            }
             columnNum=columnNum+1; 
        }
        fRow=fRow+cvals.size();
        columnNum=1;
    }

暂无
暂无

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

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