使用 JAVA 从 excel 中检索 INTEGER 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22221700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:24:45  来源:igfitidea点击:

Retrieve INTEGER values from excel using JAVA

javaexcelapache-poi

提问by user3387609

My aim is to read an excel file uisng POI and print the values present in it. If the value is 5 then my output must be 5. but it is returning as 5.0. The below is the code that I have tried.

我的目标是读取 uisng POI 的 excel 文件并打印其中存在的值。如果值是 5,那么我的输出必须是 5。但它返回 5.0。以下是我尝试过的代码。

    FileInputStream fileInputStream = null;
    XSSFSheet xssfResultSheet = null;
    String filePath = "C:\MyXcel.xlsx";
    fileInputStream = new FileInputStream(new File(filePath));
    XSSFWorkbook workbook = null;
    workbook = new XSSFWorkbook(fileInputStream);
    xssfResultSheet = workbook.getSheet("Sheet1");
    int iRowCount = xssfResultSheet.getLastRowNum();

    for (int i = 1; i <= iRowCount; i++) {
        Row resultRow = xssfResultSheet.getRow(i);
        System.out.println(resultRow.getCell(0));
    }

My Excel has values 1,2,3 but my output is 1.0,2.0,3.0. Instead my output should also be 1,2,3

我的 Excel 的值是 1,2,3,但我的输出是 1.0,2.0,3.0。相反,我的输出也应该是 1,2,3

采纳答案by PopoFibo

Change:

改变:

System.out.println(resultRow.getCell(0));

To:

到:

System.out.println(new DataFormatter().formatCellValue(resultRow.getCell(0)));

Explanation:

解释:

apache-poiprovides for DataFormatterclass as utility to leverage the format of the content as it appearson the excel. You can choose custom formats too, a simple example would be (cell is reference to your XSSFCellobject):

apache-poi提供DataFormatter类作为实用程序来利用显示在 excel 上的内容格式。您也可以选择自定义格式,一个简单的例子是(单元格是对您的XSSFCell对象的引用):

Excel sheet looks like:

Excel 工作表如下所示:

enter image description here

在此处输入图片说明

Code:

代码:

System.out.println(new DataFormatter().formatCellValue(cell));

The above line would print:

上面的行将打印:

50%
$ 1,200
12/21/14
9886605446

Whereas your normal print would interpret it differently:

而您的正常打印会以不同的方式解释它:

0.5
1200.0
21-Dec-2014
9.886605446E9

回答by jantonio.aguilera

POI should have a cell.getRawValueAsString() method, but AFAIK that's not the case. The displayed text you see in excel is the sum of value + format. So if you want to recover it "as displayed":

POI 应该有一个 cell.getRawValueAsString() 方法,但 AFAIK 事实并非如此。你在excel中看到的显示文本是值+格式的总和。因此,如果您想“按显示”恢复它:

  • Retrieve cell value from cell object
  • Retrieve cell format using associated CellStyle.
  • apply format to value using DataFormatter
  • 从单元格对象中检索单元格值
  • 使用关联的 CellStyle 检索单元格格式。
  • 使用 DataFormatter 将格式应用于值

回答by Bogdan Niculeasa

Apache POI interprets your numerial cell values as double, in order to avoid this, try to set the cell type. Maybe this will help you:

Apache POI 将您的数字单元格值解释为 double,为了避免这种情况,请尝试设置单元格类型。也许这会帮助你:

for (int i = 1; i <= iRowCount; i++) { Row resultRow = xssfResultSheet.getRow(i); resultRow.getCell(i).setCellType(Cell.CELL_TYPE_STRING); System.out.println(Integer.parseInt(resultRow.getCell(0).toString())); }

for (int i = 1; i <= iRowCount; i++) { Row resultRow = xssfResultSheet.getRow(i); resultRow.getCell(i).setCellType(Cell.CELL_TYPE_STRING); System.out.println(Integer.parseInt(resultRow.getCell(0).toString())); }

回答by ramakrishna

try this,

尝试这个,

System.out.println((int)Math.round(cell.getNumericCellValue()));

回答by Hamid Raza Abdul

See if this helps:

看看这是否有帮助:

Try parseInt, after converting the cell's value to string using toString.

在使用 toString 将单元格的值转换为字符串后,尝试使用 parseInt。

Cell cell=row.getCell(n);

First, convert cell's value to String using toString()

首先,使用 toString() 将单元格的值转换为 String

String cell_value = cell.toString()

Use parseInt to convert the same into integer:

使用 parseInt 将其转换为整数:

int waitTime = Integer.parseInt(cell_value);

Now test whether the parseInt has done it's job or not.

现在测试 parseInt 是否完成了它的工作。

System.out.print(waitTime+1);