爪哇 - Apache的POI - 故障填充行和细胞回路(EXCEL) [英] Java - Apache POI - Trouble filling rows and cells with loops (Excel)

查看:135
本文介绍了爪哇 - Apache的POI - 故障填充行和细胞回路(EXCEL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个HashMap的:

There's a HashMap:

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是类别,每一个按键都有自己的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.

下面是实际code:

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.

推荐答案

在内部循环,keyCell永远不会改变的价值。这意味着你在每次写入同一单元格的值。 (我很惊讶的code在所有工作,由于这个问题。)

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 ++。如果还是不行,添加调试显示行数keyCell每次调用createCell()的时间。这将显示是否索引按照正确的方式与否。你想要的方式:

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


这篇关于爪哇 - Apache的POI - 故障填充行和细胞回路(EXCEL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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