Java 在 XSSF Apache POI 中将特定单元格宽度设置为列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20896517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a specific cell width to column in XSSF Apache POI
提问by om39a
I am trying to export a table as xlsx using Apache POI XSSF. Table has 5 columns.
我正在尝试使用 Apache POI XSSF 将表导出为 xlsx。表有 5 列。
First row has title block merging 5 columns. In the second row, 5 headings for the table. Remaining rows are data. I want to set width of the column as max width of each heading blocks.
第一行有合并 5 列的标题栏。在第二行,表格有 5 个标题。剩余的行是数据。我想将列的宽度设置为每个标题块的最大宽度。
I tried mySheet.autoSizeColumn(colnum)
and mySheet.setColumnWidth(columnIndex, width)
. Problem with AutosizeColumn
, it is returning the highest width of the data in all the rows. So If width of some data in table is more, table header width is becoming very large.
And for the second one, setColumnWidth
, I need to know width of the header cell so I can set it to the sheet. But how to find the width of a specific cell? Had no luck yet in figuring out how to do it. Any idea on how to that?
我试过mySheet.autoSizeColumn(colnum)
和mySheet.setColumnWidth(columnIndex, width)
。问题AutosizeColumn
,它返回所有行中数据的最大宽度。因此,如果表中某些数据的宽度更大,则表头宽度会变得非常大。对于第二个,setColumnWidth
我需要知道标题单元格的宽度,以便我可以将其设置到工作表中。但是如何找到特定单元格的宽度?还没有弄清楚如何做到这一点。关于如何做到这一点的任何想法?
采纳答案by Sankumarsingh
I would suggest simple solution that I have used.
我会建议我使用过的简单解决方案。
Use a condition and after writing the 2nd row, i.e. the heading row, use AutosizeColumn()
it will change the cell width according to the header width and then the width will remain as it is.
使用条件,在写入第二行,即标题行后,使用AutosizeColumn()
它会根据标题宽度更改单元格宽度,然后宽度将保持原样。