importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStream;importjava.io.PushbackInputStream;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;importjava.util.List;importorg.apache.poi.POIXMLDocument;importorg.apache.poi.openxml4j.exceptions.InvalidFormatException;importorg.apache.poi.openxml4j.opc.OPCPackage;importorg.apache.poi.poifs.filesystem.POIFSFileSystem;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.xmlbeans.impl.piccolo.io.FileFormatException;/***yanbiao 2016.10.25*/
public classExcelUtil {private static final String EXTENSION_XLS = "xls";private static final String EXTENSION_XLSX = "xlsx";/*** 文件检查*/
private void preReadCheck(String filePath) throwsFileNotFoundException, FileFormatException {
File file= newFile(filePath);if (!file.exists()) {throw new FileNotFoundException("导入的文件不存在:" +filePath);
}if (!(filePath.endsWith(EXTENSION_XLS) ||filePath.endsWith(EXTENSION_XLSX))) {throw new FileFormatException("传入的文件不是excel");
}
}/*** 取得WorkBook对象
* xls:HSSFWorkbook,03版
* xlsx:XSSFWorkbook,07版*/
private Workbook getWorkbook(String filePath) throwsIOException, InvalidFormatException {//直接判断后缀来返回相应的Workbook对象多数情况没问题,但是这个更保险,第3条已经说明
Workbook wb = null;
InputStream is= newFileInputStream(filePath);if (!is.markSupported()) {
is= new PushbackInputStream(is, 8);
}if(POIFSFileSystem.hasPOIFSHeader(is)) {return newHSSFWorkbook(is);
}if(POIXMLDocument.hasOOXMLHeader(is)) {return newXSSFWorkbook(OPCPackage.open(is));
}throw new IllegalArgumentException("您的excel版本目前不支持poi解析");
}/*** 读取excel文件内容*/
public Map> readExcel(String filePath) throwsFileNotFoundException, FileFormatException {//检查和获取workbook对象
this.preReadCheck(filePath);
Workbook wb= null;
Map> map = new HashMap>();try{
wb= this.getWorkbook(filePath);//默认只读取第一个sheet
Sheet sheet = wb.getSheetAt(0);int rowcount = sheet.getLastRowNum();//逻辑行,包括空行
int cellcount = sheet.getRow(0).getLastCellNum();//第一行(将来作为字段的行)有多少个单元格
for (int i=0;i
List list = new ArrayList();
Row row=sheet.getRow(i);if(null!=row){for (int j=0;j
list.add(getCellValue(row.getCell(j)));//这里也是用for循环,用Cell c:row这样的遍历,空单元格就被抛弃了
}
System.out.println("第"+(row.getRowNum()+1)+"行数据:"+list.toString());
map.put(row.getRowNum(), list);
}else{for (int j=0;j
list.add("无数据");
}
System.out.println("第"+(i+1)+"行数据:"+list.toString());
map.put(i, list);
}
}
}catch(Exception e) {
System.out.println("读取Excel异常:"+e.getMessage());
e.printStackTrace();
}finally{if (wb != null) {try{
wb.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returnmap;
}/*** 取单元格的值*/
privateString getCellValue(Cell c) {if (c == null) {return "无数据";
}
String value= "";switch(c.getCellType()){case HSSFCell.CELL_TYPE_NUMERIC://数字
value = c.getNumericCellValue()+"";break;case HSSFCell.CELL_TYPE_STRING://字符串
value =c.getStringCellValue();break;case HSSFCell.CELL_TYPE_BOOLEAN://boolean
value = c.getBooleanCellValue()+"";break;case HSSFCell.CELL_TYPE_FORMULA://公式
value = c.getCellFormula()+"";break;case HSSFCell.CELL_TYPE_BLANK://空值
value= "无数据";break;caseHSSFCell.CELL_TYPE_ERROR:
value= "非法字符";break;default:
value= "未知类型";break;
}returnvalue;
}
}