java解析Excel(兼容2003及2007)
刚开始从网上找了个例子使用new HSSFWorkbook(new FileInputStream(excelFile))来读取Workbook,
对Excel2003以前(包括2003)的版本没有问题,但读取Excel2007时发生如下异常:
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
该错误意思是说,文件中的数据是用Office2007+XML保存的,而现在却调用OLE2 Office文档处理,应该使用POI不同的部分来处理这些数据,比如使用XSSF来代替HSSF。
String fileName = file.getOriginalFilename();
Workbook hssfWorkbook = null;
if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
workbook = new XSSFWorkbook(file.getInputStream());
} else {
workbook = new HSSFWorkbook(file.getInputStream());
}
List<String> list = new ArrayList<String>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
//HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
//HSSFRow hssfRow = hssfSheet.getRow(rowNum);
Row hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
//HSSFCell xh = hssfRow.getCell(0);
Cell xh = hssfRow.getCell(0);
if (xh == null) {
continue;
}
list.add(xh.getStringCellValue());
}
}
如果只是支持Excel2003的话,需要导入的poi包只需要:
- dom4j-1.6.1.jar
- poi-3.8-20120326.jar
但是如果要同时支持Excel2003和Excel2007就得需要:
dom4j-1.6.1.jar
poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
xmlbeans-2.3.0.jar
另外,发生如下异常:
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
……
是因为在hssfWorkbook = new HSSFWorkbook(is); 创建失败抛出异常后FileInputStream被关闭了,所以在创建XSSFWorkbook之前要再重新创建FileInputStream。