java poi excel2010_java的poi技术读取Excel[2003-2007,2010]

/****/

packagecom.b510.excel;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.ArrayList;importjava.util.List;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.xssf.usermodel.XSSFCell;importorg.apache.poi.xssf.usermodel.XSSFRow;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importcom.b510.common.Common;importcom.b510.excel.util.Util;importcom.b510.excel.vo.Student;/***@authorHongten

* @created 2014-5-20*/

public classReadExcel {/*** read the Excel file

*@parampath the path of the Excel file

*@return*@throwsIOException*/

public List readExcel(String path) throwsIOException {if (path == null ||Common.EMPTY.equals(path)) {return null;

}else{

String postfix=Util.getPostfix(path);if (!Common.EMPTY.equals(postfix)) {if(Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)) {returnreadXls(path);

}else if(Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)) {returnreadXlsx(path);

}

}else{

System.out.println(path+Common.NOT_EXCEL_FILE);

}

}return null;

}/*** Read the Excel 2010

*@parampath the path of the excel file

*@return*@throwsIOException*/

public List readXlsx(String path) throwsIOException {

System.out.println(Common.PROCESSING+path);

InputStream is= newFileInputStream(path);

XSSFWorkbook xssfWorkbook= newXSSFWorkbook(is);

Student student= null;

List list = new ArrayList();//Read the Sheet

for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {

XSSFSheet xssfSheet=xssfWorkbook.getSheetAt(numSheet);if (xssfSheet == null) {continue;

}//Read the Row

for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {

XSSFRow xssfRow=xssfSheet.getRow(rowNum);if (xssfRow != null) {

student= newStudent();

XSSFCell no= xssfRow.getCell(0);

XSSFCell name= xssfRow.getCell(1);

XSSFCell age= xssfRow.getCell(2);

XSSFCell score= xssfRow.getCell(3);

student.setNo(getValue(no));

student.setName(getValue(name));

student.setAge(getValue(age));

student.setScore(Float.valueOf(getValue(score)));

list.add(student);

}

}

}returnlist;

}/*** Read the Excel 2003-2007

*@parampath the path of the Excel

*@return*@throwsIOException*/

public List readXls(String path) throwsIOException {

System.out.println(Common.PROCESSING+path);

InputStream is= newFileInputStream(path);

HSSFWorkbook hssfWorkbook= newHSSFWorkbook(is);

Student student= null;

List list = new ArrayList();//Read the Sheet

for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {

HSSFSheet hssfSheet=hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null) {continue;

}//Read the Row

for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

HSSFRow hssfRow=hssfSheet.getRow(rowNum);if (hssfRow != null) {

student= newStudent();

HSSFCell no= hssfRow.getCell(0);

HSSFCell name= hssfRow.getCell(1);

HSSFCell age= hssfRow.getCell(2);

HSSFCell score= hssfRow.getCell(3);

student.setNo(getValue(no));

student.setName(getValue(name));

student.setAge(getValue(age));

student.setScore(Float.valueOf(getValue(score)));

list.add(student);

}

}

}returnlist;

}

@SuppressWarnings("static-access")privateString getValue(XSSFCell xssfRow) {if (xssfRow.getCellType() ==xssfRow.CELL_TYPE_BOOLEAN) {returnString.valueOf(xssfRow.getBooleanCellValue());

}else if (xssfRow.getCellType() ==xssfRow.CELL_TYPE_NUMERIC) {returnString.valueOf(xssfRow.getNumericCellValue());

}else{returnString.valueOf(xssfRow.getStringCellValue());

}

}

@SuppressWarnings("static-access")privateString getValue(HSSFCell hssfCell) {if (hssfCell.getCellType() ==hssfCell.CELL_TYPE_BOOLEAN) {returnString.valueOf(hssfCell.getBooleanCellValue());

}else if (hssfCell.getCellType() ==hssfCell.CELL_TYPE_NUMERIC) {returnString.valueOf(hssfCell.getNumericCellValue());

}else{returnString.valueOf(hssfCell.getStringCellValue());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值