Apache POI Excel - 如何配置要扩展的列?
我正在使用 Apache POI API 生成 Excel 电子表格来输出一些数据。
我面临的问题是,当创建并打开电子表格时,列不会扩展,因此一些长文本(例如日期格式的文本)乍一看不会显示。
我可以双击 Excel 中的列边框来展开或拖动边框来调整列宽,但可能有 20 多个列,我不可能每次打开电子表格时都手动执行此操作:(
我发现 方法) groupRow()
和 setColumnGroupCollapsed()
可能可以做到这一点,但可能我使用了错误的方式。
(虽然可能是错误的 strong>示例代码片段
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
//create sheet
Sheet sheet = wb.createSheet("masatoSheet");
//not really working yet.... :(
//set group for expand/collapse
//sheet.groupRow(0, 10); //just random fromRow toRow argument values...
//sheet.setColumnGroupCollapsed(0, true);
//create row
Row row = sheet.createRow((short)0);
//put a cell in the row and store long text data
row.createCell(0).setCellValue("Loooooooong text not to show up first");
创建此电子表格时,单元格中会出现“Looooooong text not to show up first”字符串,但由于该列未展开,因此仅显示“Loooooooo”。
我如何配置它,以便当我打开电子表格时,该列已经展开???
I am using Apache POI API
to generate excel spreadsheet
to output some data.
The problem I am facing is when the spreadsheet is created and opened, columns are not expanded so that some long text like Date formatted text is not showing up on first glance.
I could just double click the column border in excel to expand or drag the border to adjust the column width but there could be 20+ columns and there is no way I want to do that manually every time I open the spreadsheet :(
I found out (though could be wrong method) groupRow()
and setColumnGroupCollapsed()
might be able to do the trick but no luck. Maybe I'm using it in wrong way.
Sample Code snippet
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
//create sheet
Sheet sheet = wb.createSheet("masatoSheet");
//not really working yet.... :(
//set group for expand/collapse
//sheet.groupRow(0, 10); //just random fromRow toRow argument values...
//sheet.setColumnGroupCollapsed(0, true);
//create row
Row row = sheet.createRow((short)0);
//put a cell in the row and store long text data
row.createCell(0).setCellValue("Loooooooong text not to show up first");
When this spreadsheet is created, the "Looooooong text not to show up first" string is in the cell but since the column is not expanded only "Loooooooo" is showing up.
How can I configure it so that when I open my spreadsheet, the column is already expanded???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
将所有数据添加到工作表后,您可以在工作表上调用
autoSizeColumn(int column)
以将列自动调整为正确的大小。这是指向API。
请参阅此帖子以获取更多参考
使用apache poi时将excel单元格大小与内容大小匹配的问题
After you have added all your data to the sheet, you can call
autoSizeColumn(int column)
on your sheet to autofit the columns to the proper sizeHere is a link to the API.
See this post for more reference
Problem in fitting the excel cell size to the size of the content when using apache poi
提示:要使自动调整大小正常工作,应在将数据填充到 Excel 后调用
sheet.autoSizeColumn(columnNumber)
。在填充数据之前调用该方法不会有任何效果。
Tip : To make Auto size work , the call to
sheet.autoSizeColumn(columnNumber)
should be made after populating the data into the excel.Calling the method before populating the data, will have no effect.
如果您想自动调整工作簿中所有列的大小,以下方法可能有用:
If you want to auto size all columns in a workbook, here is a method that might be useful:
您可以尝试这样的操作:
这里参数是:工作表中的列号及其宽度
但是,宽度单位很小,例如您可以尝试 4000。
You can try something like this:
Here params are:column number in sheet and its width
But,the units of width are pretty small, you can try 4000 for example.
对于 Excel POI:
For Excel POI:
如果您知道列的数量(如果它等于集合列表)。您可以简单地使用这个衬垫来调整一张纸的所有列(如果您至少使用 java 8):
If you know the count of your columns (f.e. it's equal to a collection list). You can simply use this one liner to adjust all columns of one sheet (if you use at least java 8):
下面的示例代码
// 这很重要
// 参数必须是单元格编号
检查输出并疯狂:)
sample code below
//this is crucial
//argument must be cell number
Check the output and go nuts :)
您可以在循环之后添加它。
You can add this, after your loop.
我使用下面的简单解决方案:
这是您的工作簿和工作表:
然后将数据添加到包含列和行的工作表中。将数据添加到工作表后,将以下代码写入
autoSizeColumn
宽度。此处,您添加工作表中的列数,而不是 15。
希望有人能帮助这个。
I use below simple solution:
This is your workbook and sheet:
then add data to your sheet with columns and rows. Once done with adding data to sheet write following code to
autoSizeColumn
width.Here, instead 15, you add the number of columns in your sheet.
Hope someone helps this.
您也可以将文本换行。 PFB示例代码:
You can wrap the text as well. PFB sample code:
非常简单,使用这一行代码
dataSheet.autoSizeColumn(0)
或给出括号中的列数
dataSheet.autoSizeColumn(单元格编号)
Its very simple, use this one line code
dataSheet.autoSizeColumn(0)
or give the number of column in bracket
dataSheet.autoSizeColumn(cell number )
您可以使用调整列大小sheet.autoSizeColumn,但不要每次都使用它,当您完成写入时使用它
you can use resize column sheet.autoSizeColumn but dont use it each time use it when you done writing
将数据添加到工作表后,这对我有用。
After adding the data to the sheet, this working for me.
如果您想进一步扩展单元格,可以使用
setColumnWidth()
。You can use
setColumnWidth()
if you want to expand your cell more.