是否可以使用 Apache POI API 设置 XSSF 工作表的列宽?
我需要根据管理层提供的给定模式从我的应用程序生成 MS Excel 2007+ 文件。大部分工作已完成,但我必须将列宽设置为 11,34 厘米。我尝试使用 setColumnWidth(int columnIndex, int width) 方法,但无论我提供什么值,它都不起作用。 JavaDoc 说:
设置宽度(以字符宽度的1/256为单位)
单个单元格的最大列宽为 255 个字符。该值表示使用标准字体格式化的单元格中可以显示的字符数。
如何做到这一点?
I need to generate a MS Excel 2007+ file from my application according to a given pattern provided by management. Most of the work is done, but I have to set the column width to 11,34 centimeters. I tried to use the setColumnWidth(int columnIndex, int width)
method, but regardless of the value I provide it does not work. JavaDoc says:
Set the width (in units of 1/256th of a character width)
The maximum column width for an individual cell is 255 characters. This value represents the number of characters that can be displayed in a cell that is formatted with the standard font.
How to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我想我现在明白你的问题了,你的业务需求规定你必须根据标准 Excel 字体字符宽度设置列的宽度。
我解决这个问题的方法是在 Excel 中的示例上使用 Print Screen 键,然后粘贴到 Paint 或类似程序中并计算像素宽度。然后,我将使用标尺工具来测量像素/厘米,并将所得数字用作我的固定列宽。
这可能需要一些尝试和错误才能得到正确的结果。
话虽如此,我将分享我的个人观点,我认为这是一个懒惰且弱的业务需求。什么是“标准字体”? Excel 是哪个版本?这一要求对开发人员来说有太多的解释。
Ok, I think I understand your problem now, your business requirements state that you must set the width of the column based on standard Excel font character width.
The way I would approach this would be to use the Print Screen key on an example in Excel, then paste into Paint or a similar program and count pixel width. I would then use the ruler tool to measure pixels/cm and use the resulting number as my fixed column width.
This will probably involve a little bit of trial and error to get right.
Now with that said I will share my personal opinion that I feel this a LAZY and WEAK business requirement. What is "standard font"? Which version of Excel? This requirement is open to entirely too much developer interpretation.
其实我从一开始就错了!我的(可怕的!) 糟糕!宽度列已经被设置,但没想到!这一切只是一个简单的“三规则”,我就已经准备好了。
Actually, I was mistaken from the very beggining! My (terrible!) bad! Width columns were already being set, but thought not! All it took as a simple "rule of three" and I was set.