是否可以使用 Apache POI API 设置 XSSF 工作表的列宽?

发布于 2024-11-13 09:30:19 字数 294 浏览 4 评论 0原文

我需要根据管理层提供的给定模式从我的应用程序生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽梦紫曦~ 2024-11-20 09:30:19

好的,我想我现在明白你的问题了,你的业务需求规定你必须根据标准 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.

奶茶白久 2024-11-20 09:30:19

其实我从一开始就错了!我的(可怕的!) 糟糕!宽度列已经被设置,但没想到!这一切只是一个简单的“三规则”,我就已经准备好了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文