0

我有一个带有一些值的 excel 文件,例如:

**Status Code** **Method Name**
400                  createRequest
401                  testRequest
402                  mdm
403                  fileUpload

以及下面的代码来读取和打印数据[稍后我会把它们放在 HashMap 中]

package com.poc.excelfun;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ReadExcelData {
    public static void main(String[] args) {
        try {

            FileInputStream file = new FileInputStream(new File("attachment_status.xls"));

            //Get the workbook instance for XLS file 
            HSSFWorkbook workbook = new HSSFWorkbook(file);

            //Get first sheet from the workbook
            HSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows from first sheet
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()) {
                Row row = rowIterator.next();

                //For each row, iterate through each columns
                Iterator<Cell> cellIterator = row.cellIterator();
                while(cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch(cell.getCellType()) {
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t\t");
                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t\t");
                            break;
                    }
                }
                System.out.println("");
            }
            file.close();
            /*
             * The following code to create a new work book with the value fetched from the given work book
             * FileOutputStream out = 
                new FileOutputStream(new File("attachment_status_new.xls"));
            workbook.write(out);
            out.close();*/

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

但上面的代码返回以下内容:

Status Code     Method Name     
400.0       createRequest       
401.0       testRequest     
402.0       mdm     
403.0       fileUpload

如果出现了状态码,.0但我只想得到400不带.0

这该怎么做。

我已经用于poiexcel操作。

最好的问候安托

4

2 回答 2

1

cell.getNumericCellValue()返回 double vaule 将其转换int为小数点将被忽略。

于 2013-05-09T08:35:20.283 回答
1

Excel 将文件格式中的几乎所有数字都存储为浮点值,这就是为什么 POI 会为您返回一个数字单元格的双精度值,因为那是真正存在的。

虽然您可以将其转换为 int,但这只是针对一种情况,而不是一般情况。我认为您可能想要的(尽管您的问题不太清楚)是在Java中获取一个包含数字的字符串,就像它在Excel中一样?因此,包含 400.0 和整数格式规则的单元格将作为字符串“400”返回。

如果是这样,您想做与我在此处的回答完全相同的事情。去引用:

您要做的是使用DataFormatter 类。您将此单元格传递给它,它会尽力返回一个字符串,其中包含 Excel 会为您显示该单元格的内容。如果你给它传递一个字符串单元格,你会得到这个字符串。如果您传递一个应用了格式规则的数字单元格,它将根据它们格式化数字并返回字符串。

对于您的情况,我假设数字单元格应用了整数格式规则。如果您要求 DataFormatter 格式化这些单元格,它会返回一个包含整数字符串的字符串。

于 2013-05-09T10:19:34.850 回答