使用apache POI将hashmap的每个元素(键-值对)写到同一个工作簿中的不同excel表中

时间:2022-01-31 20:25:33

I want to write each key-value pair of the Hashmap to a different sheet in the same workbook.The Hashmap has the following values:

我想将Hashmap的每个键值对写入同一个工作簿中的不同表。Hashmap有以下值:

 SP001(key) :: 9087897867,8908789867,7896756789(value)
 SP002(key) :: 5241526352,4121451252,4152634512(value)
 SP003(key) :: 4152784524,4578451245,4152784596(value)

The key should be used to name the excel sheet and the value(containing numbers separated by comma) should be written in cells as displayed in image below

键应该用于命名excel表,值(包含用逗号分隔的数字)应该写在单元格中,如下图所示

使用apache POI将hashmap的每个元素(键-值对)写到同一个工作簿中的不同excel表中

1 个解决方案

#1


1  

I've been involved with ApachePOI alot recently in my own project and I must say it can prove very useful. I threw this together without running it through an IDE or anything, so it might need a check, but you basically want:

最近在我自己的项目中,我一直在参与ApachePOI,我必须说它非常有用。我把它放在一起,没有通过IDE或任何东西运行,所以它可能需要检查,但基本上你想:

Workbook workbook = new XSSFWorkbook();
for(String key : yourMap.keySet()){
    Sheet sheet = workbook.createSheet(key);
    List<Integer> values = yourMap.get(key);
    int row = 0;
    for(Integer value : values){
        sheet.creatRow(row).createCell(0).setCellValue(value);
        row++;
    }
}

I've kept some sources I found quite handy that I like to share when I see ApachePOI brought up. If you want a brief overview of core class descriptions you can view them here and if you want a boatload of examples, here is a whole list of 'em from Apache's site. Pretty useful stuff. Hope this helps

我保留了一些我觉得很方便的资料,当我看到ApachePOI长大的时候,我喜欢分享。如果您想要简要概述核心类描述,您可以在这里查看它们,如果您想要大量的示例,这里是Apache站点的完整列表。非常有用的东西。希望这有助于

#1


1  

I've been involved with ApachePOI alot recently in my own project and I must say it can prove very useful. I threw this together without running it through an IDE or anything, so it might need a check, but you basically want:

最近在我自己的项目中,我一直在参与ApachePOI,我必须说它非常有用。我把它放在一起,没有通过IDE或任何东西运行,所以它可能需要检查,但基本上你想:

Workbook workbook = new XSSFWorkbook();
for(String key : yourMap.keySet()){
    Sheet sheet = workbook.createSheet(key);
    List<Integer> values = yourMap.get(key);
    int row = 0;
    for(Integer value : values){
        sheet.creatRow(row).createCell(0).setCellValue(value);
        row++;
    }
}

I've kept some sources I found quite handy that I like to share when I see ApachePOI brought up. If you want a brief overview of core class descriptions you can view them here and if you want a boatload of examples, here is a whole list of 'em from Apache's site. Pretty useful stuff. Hope this helps

我保留了一些我觉得很方便的资料,当我看到ApachePOI长大的时候,我喜欢分享。如果您想要简要概述核心类描述,您可以在这里查看它们,如果您想要大量的示例,这里是Apache站点的完整列表。非常有用的东西。希望这有助于