本文共 3226 字,大约阅读时间需要 10 分钟。
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();
}
}
}
转载地址:http://dxowx.baihongyu.com/