java - 将 Java HashMap 导出到 xlsx

标签 java hashmap apache-poi xls

我需要使用 poi 将 HashMap 转换为 xlsx。对于工作表数据 2,我需要这样的东西:

表 1:

enter image description here

但是我有表2:

enter image description here

这是我的 HashMap 列表:

rows=[{kol2=s, kol1=s}, {kol2=bbbb, kol3=bbbb, kol1=aaaa}, {kol2=bbbb, kol3=bbbb, kol1=aaaa}, {kol2=bbbb, kol3=bbbb, kol1=aaaa}, {kol2=s, kol1=s}]}

这是我的代码:

            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet("data");
            XSSFSheet sheet2 = workBook.createSheet("data2");
            int rowCount = 0;
            int help = 1;


                List<HashMap<String, Object>> rows = ((List<HashMap<String, Object>>) x);
                int rowCount2 = 0;
                int header = 1;
                Row header2 = sheet2.createRow(0);
                for (int i = 0; i < rows.size(); i++) {
                    int li = 0;
                    Row row2 = sheet2.createRow(++rowCount2);
                    HashMap<String, Object> row = rows.get(i);
                    int columnCount2 = 0;

                    for (HashMap.Entry<String, Object> subElement : row.entrySet()) {

                        if (subElement.getValue() != null) {

                            if (i == li) {
                                Cell cell = header2.createCell(header);
                                cell.setCellValue(subElement.getKey().toString());
                                header++;
                            }
                            li++;
                            Cell cell2 = row2.createCell(++columnCount2);
                            cell2.setCellValue(subElement.getValue().toString());
                        }
                    }
                }

有人可以帮忙吗?

最佳答案

遍历 HashMap 的 EntrySet

第一个问题是您正在遍历 HashMap

的 entrySet
for (HashMap.Entry<String, Object> subElement : row.entrySet()) {
    // no guaranteed order
}

查看 Set#iterator() 方法的 JavaDoc,您将看到:

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

有些集合是有序的(例如 TreeSet),但是由于您使用的是 HashMap,因此您的 EntrySet 也不会被排序。

请注意工作表中的列顺序是 kol2-kol3-kol1。你不希望它成为 kol1-kol2-kol3 吗?

不创建空列

您忘记为 map 中没有的列创建空单元格。

if (subElement.getValue() != null) {
    // there won't be an empty cell if you e.g. don't have kol2 in your rows Map, 
    // since this just skips your current value
}

这就是为什么你最终会得到类似这样的东西的原因:

kol2   kol3   kol1
s      s      
bbbb   bbbb   aaaa
...

代替:

kol2   kol3   kol1
s             s      
bbbb   bbbb   aaaa
...

在循环内创建标题行

通过在循环内创建标题行,您会使解决方案变得比必要的更复杂。只创建标题行然后遍历 List 中的条目会容易得多。

if (i == li) {
    Cell cell = header2.createCell(header);
    cell.setCellValue(subElement.getKey().toString());
    header++;
}

如果您在循环外执行此操作,则不需要 liheader 变量

建议的解决方案

我会(一开始)想出这样的东西(我添加了一些我通常不会放在那里的额外评论,以更清楚地说明意图是什么以及您需要了解解决方案的哪些方面):

    XSSFSheet sheet2 = workBook.createSheet("data2");
    List<HashMap<String, Object>> rows = ((List<HashMap<String, Object>>) x);

    List<String> headers = Arrays.asList("kol1", "kol2", "kol3");
    int currentRowNumber = 0;

    // create header row
    Row header = sheet2.createRow(currentRowNumber);
    for (int i = 0; i < headers.size(); i++) {
        Cell headerCell = header.createCell(i);
        headerCell.setCellValue(headers.get(i));
    }

    // create data rows (we loop over the rows List)
    for (int i = 0; i < rows.size(); i++) {
        HashMap<String, Object> row = rows.get(i);

        // we neet to increment the rowNumber for the row in the sheet at the beginning of 
        // each row. entry 0 in the rows List is in sheetRow 1, entry 1 in sheetRow 2, etc.
        currentRowNumber++;
        Row sheetRow = sheet2.createRow(currentRowNumber);

        // we can now loop over the columns inside the row loop (using the headers List)
        // we create a Cell for each column, but only fill it if there is
        for (int j = 0; j < headers.size(); j++) {
            Cell cell = sheetRow.createCell(j);

            // only fill the cell if we are having data in the row map for the current column
            String currentColumnName = headers.get(j);
            if (row.containsKey(currentColumnName)) {
                cell.setCellValue(row.get(currentColumnName).toString());
            }
        }
    }

如果您想要不同的列顺序,只需更改标题列表即可(例如 Arrays.asList("kol2", "kol3", "kol1"))。

关于java - 将 Java HashMap 导出到 xlsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42455154/

相关文章:

java - 发送包裹而不关闭时,出现错误

java - 同时搜索多个HashMap

Java - 将 hashmap 写入 csv 文件

java - 从原始值键值对 hashmap Java 中获取键值对

html - 添加将 html 转换为 msword 文档的功能

java - 如何从控制台设置亲和性运行java

java - @Autowired 和线程的陷阱

java - 调用接口(interface)方法而不指定实现接口(interface)的类

java - 为什么在使用 apache poi api 在我的 java seam 应用程序中生成它们时,我会得到损坏的 excel 文件

java - 保存 XSSFWorkbook 时仅更新一行