Java - Apache POI - 无法用循环填充行和单元格 (Excel) [英] Java - Apache POI - Trouble filling rows and cells with loops (Excel)

查看:44
本文介绍了Java - Apache POI - 无法用循环填充行和单元格 (Excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个哈希映射:

HashMap<String, ArrayList<String>> matrix = new HashMap<String, ArrayList<String>>();

我想用这种模式填充一个excel表:

I want to fill an excel sheet in this pattern:

hashkey1 | hashkey2 | hashkey3 | hashkey4
value1-1 | value2-1 | value3-1 | value4-1  
value1-2 | value2-2 | value3-2 | value4-2  
value1-3 | value2-3 | value3-3 | value4-3  
value1-4 | value2-4 | value3-4 | value4-4  
value1-5 | value2-5 | value3-5 | value4-5  

hashKeys"是Categories",每个键都有自己的ArrayList.ArrayList 的每个字符串元素都应绘制在其对应的键下.

The "hashKeys" are "Categories", and every key has its own ArrayList. Every string-element of the ArrayLists shall be drawn under its correspondent key.

实际代码如下:

int keyCell = -2;
int row = 5;
Row keyRow = worksheet.createRow(4);
Row valueRow = null;
for (Map.Entry<String, ArrayList<String>> e : matrix.entrySet()) {           
    keyRow.createCell(keyCell += 2).setCellValue(e.getKey());
    for (String s : e.getValue()) { 
        if ((row - 5) < (e.getValue().size())) {
            valueRow = worksheet.createRow(row += 1);
            valueRow.createCell(keyCell).setCellValue(s);
            } else {
                valueRow.createCell(keyCell).setCellValue(s);
            }
        }
    }

它工作得很好,除了结果是这种模式:

It works beautifully, except for the fact that the result goes in this pattern:

hashkey1 | hashkey2 | hashkey3 | hashkey4
value1-1 |          |          |            
value1-2 |          |          |            
value1-3 |          |          |           
value1-4 |          |          |           
value1-5 | value2-5 | value3-5 | value4-5  

我认为它完全按照我想要的方式工作,但是由于在每个循环中创建的新行,单元格在每个循环中都会被擦除.这是一个非常具有挑战性的问题.我遇到了巨大的困难,现在我完全被困住了.什么都行不通.单元格总是会被擦除.

I think it is working exactly how I want, but the cells get erased at each loop because of the new row that is created at each loop. This is a very challenging problem. I had huge difficulty to come to this, and now I'm absolutely stuck. Nothing works. The cells ALWAYS get erased.

好吧,我希望这是一个不太冗长的话题.我真的很感谢大家的帮助.

Well, I hope this is a not too prolix topic. I really thank you all for any help.

推荐答案

在内部 for 循环中,keyCell 永远不会改变 value.这意味着您每次都写入相同的单元格值.(由于这个问题,我很惊讶代码竟然能正常工作.)

In the inner for loop, keyCell never changes value. This means you are writing to the same cell value each time. (I'm surprised the code works at all due to this problem.)

 for (String s : e.getValue()) { 
        if ((row - 5) < (e.getValue().size())) {
            valueRow = worksheet.createRow(row += 1);
            valueRow.createCell(keyCell).setCellValue(s);
            } else {
                valueRow.createCell(keyCell).setCellValue(s);
            }
        }

所以首先,我会解决这个问题并使用 keyCell++.如果这不起作用,请添加每次调用 createCell() 时显示行和 keyCell 编号的调试.这将显示索引是否遵循正确的模式.你想要的模式:

So first, I'd fix this problems and use keyCell++. If that doesn't work, add debugging showing the row and keyCell number each time you call createCell(). This will show if the indexes are following the correct pattern or not. You want the pattern:

  • 0, 0
  • 0, 1
  • ....
  • 0, 4
  • 1, 0

这篇关于Java - Apache POI - 无法用循环填充行和单元格 (Excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆