从Excel读取列值作为字符串返回为十进制?

时间:2013-05-10 07:33:56

标签: java excel

我需要读取Excel文件的特定列值。

我无法按原样读取单元格值。

我的代码是

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

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.usermodel.Workbook;


public class ExcelSheetColumn 
{
public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, IOException
{
    Workbook wb = WorkbookFactory.create(new FileInputStream("C:/Users/Pradeep.HALCYONTEKDC/Desktop/My Data/ExcelFiles/XLS1.xls"));
    Sheet sheet = wb.getSheet("Retail - All");
    Row row=null;
    Cell cell ;
    for (int j=0; j< sheet.getLastRowNum()+1; j++)
    {
        row = sheet.getRow(j);

        cell = row.getCell(0);
        if(cell!=null)
        {
            int type = cell.getCellType();
            if (type == HSSFCell.CELL_TYPE_STRING)
              System.out.println(cell.getRichStringCellValue().toString());
            else if (type == HSSFCell.CELL_TYPE_NUMERIC)
                System.out.println(cell.getNumericCellValue());
            else if (type == HSSFCell.CELL_TYPE_BOOLEAN)
                System.out.println( cell.getBooleanCellValue());
            else if (type == HSSFCell.CELL_TYPE_BLANK)
                System.out.println(cell.getColumnIndex() + "] = BLANK CELL");
       }
    }
}
}

Excel列值

79
999861
999861
https://staging.adpcreditsolutions.com
/index/contract_decision
9200278
2011/01/17
79
5032944200
IL4-PCC@TEST.COM
1979/12/31
0.00
0.00

但我得到的输出是

79.0
999861.0
999861.0
https://staging.adpcreditsolutions.com
/index/contract_decision
9200278.0
17-Jan-2011
79.0
5.0329442E9
IL4-PCC@TEST.COM
31-Dec-1979
0.0
0.0

如何阅读我上面的程序代码来读取excel列值?请帮帮我!!

if (type == HSSFCell.CELL_TYPE_NUMERIC)
            {
                String stringValue=""+cell.getNumericCellValue();
                String[] splitValue=stringValue.split(".0");
                System.out.println(splitValue[0]);
            }

3 个答案:

答案 0 :(得分:1)

 System.out.println(cell.toString());//here is the problem

toString()方法返回object的字符串表示形式。通常,toString方法返回一个字符串“ textually表示”这个object

使用cell.getStringCellValue()

而不是

cell.toString()

需要使用propor。

对于数值,您必须使用

getNumericCellValue() 并把条件放在那里

if(cell!=null)
        {
            int type = cell.getCellType();
            if (type == HSSFCell.CELL_TYPE_STRING)
              System.out.println(cell.getRichStringCellValue().toString());
            else if (type == HSSFCell.CELL_TYPE_NUMERIC)

              String[] splits = String.valueOf(cell.getNumericCellValue()).split(".");

           System.out.println(splits[0]);
            else if (type == HSSFCell.CELL_TYPE_BOOLEAN)
                System.out.println( cell.getBooleanCellValue());
            else if (type == HSSFCell.CELL_TYPE_BLANK)
                System.out.println(cell.getColumnIndex() + "] = BLANK CELL");
        }

答案 1 :(得分:0)

您无法从数字字段中读取字符串值。它会抛出异常。

答案 2 :(得分:0)

这对我有用。试试这个

 if (currentCell.getCellType().toString().contentEquals("NUMERIC")) {
        
   value = String.valueOf(currentCell.getNumericCellValue());
   value = value.endsWith(".0") ? value.replace(".0", "") : value;
}