Apache POI-HSSF:获取小数而不是文本字符串

发布于 2024-11-11 17:05:00 字数 264 浏览 0 评论 0原文

我正在使用 Apache POI-HSSF 来处理 Excel 文件。

我的电子表格中有一个看起来像“115”的单元格。我验证它的格式为“文本”(格式单元格 -> 文本)。

但是,当我将其读为 row.getCell(0).toString()

我得到这个字符串:“115.0”

这是不正确的。我应该得到“115”,因为它被明确格式化为文本。我怎样才能得到想要的结果?单元格可以是任何内容,数字或字符,我希望与单元格中的字符串相同。谢谢

I am using Apache POI-HSSF for working with Excel files.

I have a cell in my spreadsheet that looks like "115". I verified that it is formatted as "Text" (Format Cells -> Text).

However, when I read it in as
row.getCell(0).toString()

I get this string: "115.0"

This is incorrect. I should be getting "115" since it's explicitly formatted as Text. How can I get the desired result? The cell can be anything, numbers or characters, and I expect the same string as in the cell. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

鸠魁 2024-11-18 17:05:00

格式化为文本并不意味着存储为文本,它们是不同的。 Excel 已将您的单元格存储为数字,当您向 POI 询问该单元格时,您会得到一个数字单元格。

如果您询问返回的单元格是什么类型,您会发现它的类型是 CELL_TYPE_NUMERIC 而不是 CELL_TYPE_STRING

您可能想要做的是使用 DataFormatter 类 按照 Excel 设置单元格格式。然后它就会像你期望的那样。 (单元格也将格式化为货币、百分比等)

Formatted as text does not mean stored as text, they're different. Excel has stored your cell as a number, and when you ask POI for the cell you get a numeric cell back.

If you ask the cell you get back what type it is, you'll discover it's of type CELL_TYPE_NUMERIC and not CELL_TYPE_STRING

What you'll likely want to do is use the DataFormatter class to have your cell formatted as per Excel. It'll then look like you expect. (As will cells formatted as currency, percentages etc too)

抚笙 2024-11-18 17:05:00

您应该调用 HSSFCell.getCellType() 方法来确定其类型。这是处理字符串或数字类型单元格的方法。 (您可以轻松添加其他类型。)用于数字的格式将是有效格式,但不一定与电子表格的格式匹配。 (这将在下面介绍。)

public static String getCellStringValue(final HSSFCell cell) {
    int cellType = cell.getCellType();
    String value;
    if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
        // Locale is optional here
        DataFormatter dataFormatter = new DataFormatter(Locale.US);
        value = dataFormatter.formatCellValue(cell);
    } else {
        // HSSFCell.CELL_TYPE_STRING
        value = cell.getStringCellValue();
    } // more cell types are possible. Add whatever you need.
    return value;
}

该代码不一定会格式化 Excel 中显示的数字。
如果您需要格式与电子表格格式完全匹配,您可以从单元格本身获取格式化程序。为此,您可以使用 DataFormatter 实例创建 Format 实例:

public static String getCellStringValue(final HSSFCell cell) {
    int cellType = cell.getCellType();
    String value;
    if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
        // Locale is optional here
        DataFormatter dataFormatter = new DataFormatter(Locale.US);
        Format format = dataFormatter.createFormat(cell);
        value = format.format(cell.getNumericCellValue());
    } else {
        // HSSFCell.CELL_TYPE_STRING
        value = cell.getStringCellValue();
    } // more cell types are possible. Add whatever you need.
    return value;
}

You should be calling the HSSFCell.getCellType() method to determine its type. Here's a method that handles cells of type String or Numeric. (You can easily add other types.) The format used for numbers will be a valid format, but won't necessarily match that of the SpreadSheet. (That's covered below.)

public static String getCellStringValue(final HSSFCell cell) {
    int cellType = cell.getCellType();
    String value;
    if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
        // Locale is optional here
        DataFormatter dataFormatter = new DataFormatter(Locale.US);
        value = dataFormatter.formatCellValue(cell);
    } else {
        // HSSFCell.CELL_TYPE_STRING
        value = cell.getStringCellValue();
    } // more cell types are possible. Add whatever you need.
    return value;
}

That code won't necessarily format the number as it appears in Excel.
If you need the format to exactly match the spreadsheet format, you can get a formatter from the cell itself. To do this, you can use your DataFormatter instance to create a Format instance:

public static String getCellStringValue(final HSSFCell cell) {
    int cellType = cell.getCellType();
    String value;
    if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
        // Locale is optional here
        DataFormatter dataFormatter = new DataFormatter(Locale.US);
        Format format = dataFormatter.createFormat(cell);
        value = format.format(cell.getNumericCellValue());
    } else {
        // HSSFCell.CELL_TYPE_STRING
        value = cell.getStringCellValue();
    } // more cell types are possible. Add whatever you need.
    return value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文