java读取excel数字类型_POI读取Excel 各种特殊数字和类型的转换。

该博客展示了如何使用Java的Apache POI库来读取Excel文件中的数字类型数据,包括字符串、布尔值、公式等不同单元格类型,并提供了详细的代码示例。
摘要由CSDN通过智能技术生成

Java代码packagecom.golden.test;importjava.io.File;importjava.io.FileInputStream;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;/***

*@author崔素强

**/

public classPoiReadXls2 {public static voidmain(String[] args) {

File f= new File("c:\\a.xls");try{

FileInputStream is= newFileInputStream(f);

HSSFWorkbook wbs= newHSSFWorkbook(is);

HSSFSheet childSheet= wbs.getSheetAt(0);//System.out.println(childSheet.getPhysicalNumberOfRows());

System.out.println("有行数" +childSheet.getLastRowNum());for (int j = 0; j < childSheet.getLastRowNum(); j++) {

HSSFRow row=childSheet.getRow(j);//System.out.println(row.getPhysicalNumberOfCells());//System.out.println("有列数" + row.getLastCellNum());

if (null !=row) {for (int k = 0; k < row.getLastCellNum(); k++) {

HSSFCell cell=row.getCell(k);if (null !=cell) {switch(cell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC: //数字

System.out.print(cell.getNumericCellValue()+ " ");break;case HSSFCell.CELL_TYPE_STRING: //字符串

System.out.print(cell.getStringCellValue()+ " ");break;case HSSFCell.CELL_TYPE_BOOLEAN: //Boolean

System.out.println(cell.getBooleanCellValue()+ " ");break;case HSSFCell.CELL_TYPE_FORMULA: //公式

System.out.print(cell.getCellFormula() + " ");break;case HSSFCell.CELL_TYPE_BLANK: //空值

System.out.println(" ");break;case HSSFCell.CELL_TYPE_ERROR: //故障

System.out.println(" ");break;default:

System.out.print("未知类型 ");break;

}

}else{

System.out.print("- ");

}

}

}

System.out.println();

}

}catch(Exception e) {

e.printStackTrace();

}

}

}packagecom.golden.test;importjava.io.File;importjava.io.FileInputStream;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;/***

*@author崔素强

**/

public classPoiReadXls2 {public static voidmain(String[] args) {

File f= new File("c:\\a.xls");try{

FileInputStream is= newFileInputStream(f);

HSSFWorkbook wbs= newHSSFWorkbook(is);

HSSFSheet childSheet= wbs.getSheetAt(0);//System.out.println(childSheet.getPhysicalNumberOfRows());

System.out.println("有行数" +childSheet.getLastRowNum());for (int j = 0; j < childSheet.getLastRowNum(); j++) {

HSSFRow row=childSheet.getRow(j);//System.out.println(row.getPhysicalNumberOfCells());//System.out.println("有列数" + row.getLastCellNum());

if (null !=row) {for (int k = 0; k < row.getLastCellNum(); k++) {

HSSFCell cell=row.getCell(k);if (null !=cell) {switch(cell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC: //数字

System.out.print(cell.getNumericCellValue()+ " ");break;case HSSFCell.CELL_TYPE_STRING: //字符串

System.out.print(cell.getStringCellValue()+ " ");break;case HSSFCell.CELL_TYPE_BOOLEAN: //Boolean

System.out.println(cell.getBooleanCellValue()+ " ");break;case HSSFCell.CELL_TYPE_FORMULA: //公式

System.out.print(cell.getCellFormula() + " ");break;case HSSFCell.CELL_TYPE_BLANK: //空值

System.out.println(" ");break;case HSSFCell.CELL_TYPE_ERROR: //故障

System.out.println(" ");break;default:

System.out.print("未知类型 ");break;

}

}else{

System.out.print("- ");

}

}

}

System.out.println();

}

}catch(Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值