引言:最近一直在接触excel的问题,网页也有很多关于POI解析excel的资料,我也简单的整理了下,有不对地方的还望及时指正,渴望与大家交流并学习。
public int importExcel(File file) throws Exception { FileInputStream fis = null; String sheetName = "sheetname"; try { fis = new FileInputStream(file); POIFSFileSystem fs = new POIFSFileSystem(fis); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet searchWordSheet = wb.getSheet(sheetName); // sheet 空校验 if (searchWordSheet == null) { return -2; } // sheet 无数据 if (searchWordSheet.getLastRowNum() < 1) { return -1; } List<String []> ispList = new LinkedList<String []>(); //循环得到excel的每一行,然后得到每一个cell的值。 for (int i = 1; i <= searchWordSheet.getLastRowNum(); i++) { HSSFRow row = searchWordSheet.getRow(i); if (row == null) { continue; } String [] isp = new String [3]; cell = row.getCell((short) 0); Long value = getNumberValueFromCell(cell); isp[0] = value.toString(); cell = row.getCell((short) 1); isp[1] = getStringValueFromCell(cell); cell = row.getCell((short) 2); isp[2] = getStringValueFromCell(cell); ispList.add(isp); } // 数据保存入库 List<String []> ispSaveList = new LinkedList<String []>(); for (String [] isp : ispList) { ispSaveList.add(isp); } saveIspFullInfoList(ispSaveList); }
public Long getNumberValueFromCell(HSSFCell cell) {
try {
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
return NumberUtils.isNumber(cell.getStringCellValue()) ? Long
.parseLong(cell.getStringCellValue()) : null;
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
return new Double(cell.getNumericCellValue()).longValue();
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public String getStringValueFromCell(HSSFCell cell) {
if (isNullForCellValue(cell)) {
return null;
} else {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
return new Double(cell.getNumericCellValue()).longValue()+"";
} else {
return null;
}
}
}
public Object getSourceValueFromCell(HSSFCell cell) {
if (isNullForCellValue(cell)) {
return null;
} else {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
return new Double(cell.getNumericCellValue()).longValue();
} else {
return null;
}
}
}
以上三块代码是获得不同类型的cell的值
部分代码解释: saveIspFullInfoList(ispSaveList);将从Excel得到的数据保存到数据库中
HSSFSheet searchWordSheet = wb.getSheet(sheetName);通过sheet的名称得到对于的sheet页(一个excel文件可能有多个sheet页)。
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);
这块也就是通过文件得到HSSFWorkbook,在之前的文章中也说过,不同版本的excel需要不同的方式来解析。所以也可以采用之前文章中的方式来进行对于的解析。代码如下:
01.public static Workbook getWorkbook(InputStream is) throws IOException{ 02. Workbook wb = null; 03. if(!is.markSupported()){ 04. is = new PushbackInputStream(is,8); 05. } 06. if (POIFSFileSystem.hasPOIFSHeader(is)) { //Excel2003及以下版本 07. wb = (Workbook) new HSSFWorkbook(is); 08. }else if (POIXMLDocument.hasOOXMLHeader(is)) { //Excel2007及以上版本 09. wb = new XSSFWorkbook(is); 10. }else{ 11. throw new IllegalArgumentException("你的Excel版本目前poi无法解析!"); 12. } 13. // } 14. return wb; 15. }
导入所需的包可以在这里下载http://download.csdn.net/detail/javaweiming/5849101.