poi解析excel(四)
原创
©著作权归作者所有:来自51CTO博客作者口袋里的小龙的原创作品,请联系作者获取转载授权,否则将追究法律责任
package com.dragon.test;
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.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ReadExcel {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "C:/\\TestExcel.xls";
//创建文件对象
File file = new File(url);
try {
//创建文件读取流
FileInputStream fileInputStream = new FileInputStream(file);
//创建excel文件流
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileInputStream);
//获取当前Excel的所有工作表的数量
int sheetCount = hssfWorkbook.getNumberOfSheets();
System.out.println("读取文件成功!"+hssfWorkbook.getNumberOfSheets());
for (int i = 0; i < sheetCount; i++) {
//获取工作表的名称
String sheetName = hssfWorkbook.getSheetName(i);
System.out.println("工作表的名称是:"+sheetName);
//获取当前工作表
HSSFSheet sheet = hssfWorkbook.getSheetAt(i);
//获取工作表的有数据的总行数
int rowCount = sheet.getPhysicalNumberOfRows();
System.out.println("实际的总行数是:"+rowCount);
for (int j = 0; j < rowCount; j++) {
//获得行
HSSFRow hssfRow = sheet.getRow(j);
//获得每行单元格的总数
int cellCount = hssfRow.getPhysicalNumberOfCells();
for (int k = 0; k < cellCount; k++) {
//获得单元格对象
HSSFCell hssfCell = hssfRow.getCell(k);
//判断单元格的文本类型
switch (hssfCell.getCellType()) {
//字符类型
case HSSFCell.CELL_TYPE_STRING:
System.out.println(hssfCell.getStringCellValue());
break;
//数字类型
case HSSFCell.CELL_TYPE_NUMERIC:
int age = (int) hssfCell.getNumericCellValue();
System.out.println(age);
break;
//boolean类型
case HSSFCell.CELL_TYPE_BOOLEAN:
System.out.println(hssfCell.getBooleanCellValue());
break;
//空白的
case HSSFCell.CELL_TYPE_BLANK:
System.out.println("");
break;
default:
break;
}
}
}
// System.out.println("1234==="+sheet.getFirstRowNum());
// System.out.println("1234==="+sheet.getLastRowNum());
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
就这么简单 和解析xml差不多哦 呵呵