POI读取Excel带格式数据

package com.hs.tiger.application;

import java.io.FileInputStream;
import java.io.InputStream;

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

public class ParseValidator {
public static void main(String[] args) throws Exception {
InputStream inp = new FileInputStream(
"E:\\JEE\\Workspaces\\MyEclipse 8.6\\Mirror\\src\\com\\hs\\tiger\\application\\Numeric.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);
HSSFSheet sheet = wb.getSheetAt(0);
try
{
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Cell c = sheet.getRow(i).getCell(0);

//System.out.println(c.getCellType());

//c.setCellType(Cell.CELL_TYPE_STRING);
System.out.println(c.getCellStyle().getDataFormatString());
try{
System.out.println(c.getStringCellValue());
}
catch (Exception e) {
try
{
System.out.println(c.getNumericCellValue());
}
catch (Exception ex) {
// TODO: handle exception
}
}

}
}
catch (Exception e) {
// TODO: handle exception
}

/*
* for (Iterator<org.apache.poi.ss.usermodel.Row> rit =
* sheet.rowIterator(); rit.hasNext(); ) { row = rit.next(); for
* (Iterator<Cell> cit = row.cellIterator(); cit.hasNext(); ) { cell =
* cit.next(); System.out.println(cell.getBooleanCellValue()); } }
*/

}
}


使用poi读取excel内容的问题:
例如excel中cell中显示值为20,100,612.00,实际值为20100612,
使用getNumbericCellValue()读取的的实际值,
是否有函数能读取cell的显示值,求大侠指教!


显示值20,100,612.00只是格式化了而已
你读取之后再用JAVA格式化一次就行了
DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();           
df.applyPattern("###,###.00");       
System.out.println(df.format(new Integer("20100612")));

你可能感兴趣的:(C++,c,MyEclipse,C#,Excel)