在POI合并后的设定值单元格 [英] Setting value to cells after merging in POI

查看:112
本文介绍了在POI合并后的设定值单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在POI下面形成一个excel输出:

I want to form a excel output as below in POI:

从影像一样清楚,我有以下分别Header3,Header4和Header5每3个子栏目。

As clear from the image, I have 3 sub columns each under Header3, Header4 and Header5 respectively.

的名单如下:

利斯塔 - 包含值列A

ListA - Contains values for column A

数组listB - 包含值B列

ListB - Contains values for column B

List1中 - 是数据库中的行,每行3列的列表。第一列会去Header3下,第二下头4和三头下5即列C,F和我分别。

List1 - is a list of database rows with 3 columns per row. First column will go under Header3, Second under Header 4 and Third under Header 5 i.e. columns C, F and I respectively.

列表2和项目list3 - 以List1中每个值Header3,4和5列表2下在列D,G和J,而项目list3下列E,H和K分别分别将类似

List2 and List3 - Similar to List1 with each values going respectively under Header3, 4 and 5. List2 under columns D, G and J while List3 under columns E, H and K respectively.

问题1:

我头名保存为有5个值的列表。

I have Header Names stored as a list having 5 values.

如何对迭代这个列表中的值分配给合并后的地区?

How to iterate against this list to assign the values to the merged regions ?

我在做类似下面,但不工作:

I am doing something like below but is not working:

// first two headers

for (int i = 0; i < headers.size() - 3; i++) {
            headerCell = headerRow.createCell(i);
            headerCell.setCellValue(allHeaders.get(i).toUpperCase());
            headerCell.setCellStyle(styles.get("header"));
        }

// Merging
sheet.addMergedRegion(org.apache.poi.ss.util.CellRangeAddress.valueOf("$C$1:$E$1"));
sheet.addMergedRegion(org.apache.poi.ss.util.CellRangeAddress.valueOf("$F$1:$H$1"));
sheet.addMergedRegion(org.apache.poi.ss.util.CellRangeAddress.valueOf("$I$1:$K$1"));

for (int i = 3; i < headers.size(); i++) {
            headerCell = headerRow.createCell(i);
            headerCell.setCellValue(allHeaders.get(i).toUpperCase());
            headerCell.setCellStyle(styles.get("header"));
        }

问题2:

如何穿过项目list3利斯塔迭代把价值观正如我上面所解释的?

How to iterate through ListA through List3 to put in values as I have explained above?

感谢您阅读!

推荐答案

在现实中我没有完全理解你的问题,但我有一些关键点,我认为,将有助于你。

in reality I did not understand your problem completely, but I have some key points, that I think, will be helpful to you.


  1. 合并区域的第一个单元保持合并单元格的值,其余的细胞保持空白值。如果单元格A2至A5与价值测试,然后迭代的A2合并会显示测试,而其余的将是空白的手段。所以,如果你想读的合并单元格的值,你只需要,如果你想在合并单元格写类似的阅读它的第一个单元格,你只需要在只有第一个单元格写。

  1. The first Cell of merged region keeps the value of the merged cell, rest cells keep blank value. Means if cell A2 to A5 is merged with value "Test" then on iteration A2 will show "Test" while rest will show blank. SO if you want to read the merged cell value, you need to read its first cell only, and similarly if you want to write in the merged cells, you just need to write in first cell only.

sheet.getNumMergedRegions(); 返回整数,将成为合并后的区域的工作表中的总数。您可以使用循环以及遍历它。

sheet.getNumMergedRegions(); return integer that will be the total number of merged region in the sheet. you can iterate through it using loop as well.

的CellRangeAddress合并= sheet.getMergedRegion(INT指数); 会给你从 getNumMergedRegions达到规定的指标范围内地址( )

现在 merge.getFirstRow() merge.getLastRow() merge.getFirstColumn() merge.getLastColumn() merge.getNumberOfCells(),是对于相同的方法。

now merge.getFirstRow(),merge.getLastRow(), merge.getFirstColumn(), merge.getLastColumn(), merge.getNumberOfCells(), are the methods for the same.

通过包含合并单元格和行不包含任何合并单元格行中的迭代是没有太大的不同。希望这将帮助你解决你的问题。

The iteration through row containing merged cells and row not containing any merged cell are not much different. Hope this will help you to resolve your problem.

这篇关于在POI合并后的设定值单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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