Apache POI for Excel:将单元格类型设置为“文本”对于整个列
我需要使用 Java 库 用于电子表格的 Apache POI。
该文件将在 A 列中包含电话号码列表,格式为“0221...”或“+49221...” - 因此 Excel 默认将它们解释为数字单元格。这很糟糕,因为前导 0 或 + 将被修剪。
为了解决这个问题,我可以使用 cell.setCellType(Cell.CELL_TYPE_STRING) ,它工作得很好,但仅适用于我设置的特定单元格。
如何将此设置应用于整个列(即,甚至对于所有剩余单元格,用户将在其中输入其他电话号码)?
在 Excel 中,这是可能的:选择整个列,并应用单元格类型(该设置在保存/加载文件后仍然有效。)
但我找不到 POI 的正确方法。
- 首先我假设它应该类似于sheet.setDefaultCellType(int colNum)。但我找不到这样的东西(可能我只是瞎了?有很多类似方法在库中应用文本样式,例如“对齐中心”等)
- 然后我想:也许它只能应用于 NamedRange 或类似的东西,但我还没有弄清楚它是如何工作的......
I need to generate an .xls (Excel) file, using the Java library Apache POI for spreadsheets.
The file will contain a list of phone numbers in column A, formatted as "0221...." or "+49221..." - so Excel by default interprets them as numeric cells. This is bad, because the leading 0 or + will get trimmed.
To solve the problem, I can use cell.setCellType(Cell.CELL_TYPE_STRING)
, which works fine, but only for the specific cells I set this for.
How can I apply this setting for the entire column (i.e. even for all the remaining cells, where the user will enter additional phone numbers)?
In Excel, this is possible: Selecting the entire column, and apply the cell type (the setting survives saving/loading the file.)
But I can't find the correct method for POI.
- First I assumed, it should be something like
sheet.setDefaultCellType(int colNum)
. But I can't find anything like this (probably I'm just blind? There are lots of similar methods in the library for applying text styles like "align center" etc.) - Then I thought: Maybe it can only be applied to a NamedRange or something similar, but I haven't managed to work out how this works...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是一些受 Vlad 答案启发的示例代码:
上面的代码将 工作表 第一列的默认样式设置为 TEXT。
谢谢,弗拉德!
Here's some example code inspired by Vlad's answer:
The above code sets the default style for the first column of worksheet to TEXT.
Thanks, Vlad!
这有帮助吗?
Will this help?
现在使用 POI 设置它的更好方法是使用
BuiltinFormats
类。例如:
将列类型转换为数字
可以看到
BuiltinFormats
格式的完整列表,此处A better way to set it now using POI is to use the
BuiltinFormats
class.Eg:
To convert the column type to numeric
The complete list of
BuiltinFormats
formats can be seen, here弗拉德:
这样就解决了大部分问题。我发现所有空行的格式都按照我想要的方式设置。然而,我有初始零的行看起来并且工作得很好;但是,如果我必须用另一个从零开始的值更改预先存在的值,那么零就会消失。因此,我建议在单元格中创建值时添加代码,以将 setCellStyle 重置为文本。以下是一些片段:
方法 public Cell createCell(Row r!, BBjNumber cell, BBjString value!)
methodret c!
这
可能有助于其他一些人单独搜索初始零。
感谢您发帖。
亚历克斯
Vlad:
This got most of the problem done. I found that all the empty rows were formatted the way I wanted them. However rows where I had initial zeros looked and worked fine; however if I had to change a pre-existing value with another value starting with zero the zeroes disappeared. So I recommend adding code to when you create the value in the cell to reset the the setCellStyle to text. here are some snippets:
method public Cell createCell(Row r!, BBjNumber cell, BBjString value!)
methodret c!
methodend
this may help some other sole searching for initial zeroes.
Thank you for posting.
Alex