Apache POI for Excel:将单元格类型设置为“文本”对于整个列

发布于 2024-11-17 08:21:26 字数 899 浏览 3 评论 0原文

我需要使用 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 技术交流群。

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

发布评论

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

评论(4

以下是一些受 Vlad 答案启发的示例代码:

DataFormat fmt = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(fmt.getFormat("@"));
worksheet.setDefaultColumnStyle(0, textStyle);

上面的代码将 工作表 第一列的默认样式设置为 TEXT

谢谢,弗拉德!

Here's some example code inspired by Vlad's answer:

DataFormat fmt = workbook.createDataFormat();
CellStyle textStyle = workbook.createCellStyle();
textStyle.setDataFormat(fmt.getFormat("@"));
worksheet.setDefaultColumnStyle(0, textStyle);

The above code sets the default style for the first column of worksheet to TEXT.

Thanks, Vlad!

温折酒 2024-11-24 08:21:26

有帮助吗?

通过使用 @ 符号创建数据格式并在 CellStyle 中使用它
然后传递给 setDefaultColumnStyle() 方法的对象,它是
在本例中,可以将列的默认数据类型设置为文本。
我没有进一步实验,但怀疑有可能做到
与其他样式对象类似的东西将默认类型设置为
数字甚至自定义格式,例如货币。

Will this help?

By creating a data format with the @ symbol and using that in a CellStyle
object that is then passed to the setDefaultColumnStyle() method, it was
possible to set the default data type of the column to, in this case, text.
I have not experiemented further but do suspect it would be possible to do
something similar with other style objects to set the default type to
numeric or even a customised format such as currency.

东走西顾 2024-11-24 08:21:26

现在使用 POI 设置它的更好方法是使用 BuiltinFormats 类。

例如:

将列类型转换为数字

 CellStyle numericStyle = workbook.createCellStyle(); 
 numericStyle.setDataFormat(BuiltinFormats.getBuiltinFormat(2)); // 2 For Number 
 worksheet.setDefaultColumnStyle(colId, numericStyle);

可以看到BuiltinFormats格式的完整列表,此处

A better way to set it now using POI is to use the BuiltinFormats class.

Eg:

To convert the column type to numeric

 CellStyle numericStyle = workbook.createCellStyle(); 
 numericStyle.setDataFormat(BuiltinFormats.getBuiltinFormat(2)); // 2 For Number 
 worksheet.setDefaultColumnStyle(colId, numericStyle);

The complete list of BuiltinFormats formats can be seen, here

感受沵的脚步 2024-11-24 08:21:26

弗拉德:
这样就解决了大部分问题。我发现所有空行的格式都按照我想要的方式设置。然而,我有初始零的行看起来并且工作得很好;但是,如果我必须用另一个从零开始的值更改预先存在的值,那么零就会消失。因此,我建议在单元格中创建值时添加代码,以将 setCellStyle 重置为文本。以下是一些片段:
方法 public Cell createCell(Row r!, BBjNumber cell, BBjString value!)

c!=#this!.createCell(r!, cell)
c!.setCellValue(value!)
c!.setCellStyle(#text_format!)

methodret c!

#text_format! = #wb!.createCellStyle()
#text_format!.setDataFormat(#format!.getFormat("@"))

可能有助于其他一些人单独搜索初始零。
感谢您发帖。
亚历克斯

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!)

c!=#this!.createCell(r!, cell)
c!.setCellValue(value!)
c!.setCellStyle(#text_format!)

methodret c!

#text_format! = #wb!.createCellStyle()
#text_format!.setDataFormat(#format!.getFormat("@"))

methodend

this may help some other sole searching for initial zeroes.
Thank you for posting.
Alex

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