最近在项目中用到了Apache POI插件,用来读取Excel中的数据,现做以下记录,方便后续使用。
Apache POI是一款用Java编写的免费开源的跨平台的Java API,其主要用于对Microsoft Office办公软件的读写。
Apache POI API组件
在使用Apache POI的组件时需要引入相关JAR包(本项目使用Maven构建,因此在pom.xml文件中引入相关JAR包)。
org.apache.poi poi 4.0.1org.apache.poi poi-ooxml 4.0.1org.apache.poi poi-ooxml-schemas 4.0.1commons-logging commons-logging 1.2commons-codec commons-codec 1.11org.apache.commons commons-math3 3.6.1org.apache.commons commons-collections4 4.2org.apache.commons commons-compress 1.18org.apache.xmlbeans xmlbeans 3.0.2log4j log4j 1.2.17
1. 判断文件是否是Excel格式
/* * 判断文件是否是Excel */ public static void checkExcelValid(File file) throws Exception { if(!file.exists()) { throw new Exception("文件不存在"); } if(!(file.isFile() && (file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))) { throw new Exception("文件不是Excel"); } } private static final String EXCEL_XLS= "xls"; private static final String EXCEL_XLSX = "xlsx";
2. 根据Excel版本调用POI API获取Workbook(工作簿)
/* * 根据Excel版本,获取WorkBook */ public static Workbook getWorkBoox(InputStream is, File file) throws IOException { Workbook wb = null; if(file.getName().endsWith(EXCEL_XLS)) { wb = new HSSFWorkbook(is); } else if(file.getName().endsWith(EXCEL_XLSX)) { wb = new XSSFWorkbook(is); } return wb; }
3. 使用文件流读取文件,并验证文件类型
//创建文件对象 File excelFile = new File("D:/docs/模板文件.xlsx"); //文件流 FileInputStream fis = new FileInputStream(excelFile); checkExcelValid(excelFile);
4. 获取Workbook
Workbook wb = getWorkBoox(fis, excelFile);
5. 设置需要读取的表格下标,从0开始,即第一个表格
Sheet sheet = wb.getSheetAt(0);//工作表最后一行int lastRow = sheet.getLastRowNum();
6. 获取表头信息
Row rowz = sheet.getRow(1);//表头int[] arrInt = new int[18];//rowz.getLastCellNum()工作表最后一列for(int y = 0; y < rowz.getLastCellNum(); y++) { Cell cellz = rowz.getCell(y, MissingCellPolicy.RETURN_BLANK_AS_NULL); switchAppendArr(cellz, arrInt);}
7. 循环行,并取单元格的值
//循环行for(int m = 2; m < lastRow; m++) { Row row = sheet.getRow(m); if(row == null) { continue; } for(int i = 0; i < arrInt.length; i++) { Cell cell = row.getCell(arrInt[i], MissingCellPolicy.RETURN_BLANK_AS_NULL); System.out.print(returnTrueCellValue(cell) + " | "); } System.out.println();}
8. 值类型
private static String returnTrueCellValue(Cell cell) { DecimalFormat dfz = new DecimalFormat("0"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); DecimalFormat dfn = new DecimalFormat("0.00"); String value = ""; if(cell == null) { return ""; } CellType ct = cell.getCellType(); if(CellType.STRING.equals(ct)) { value = cell.getStringCellValue(); } else if(CellType.NUMERIC.equals(ct)) { if("General".equals(cell.getCellStyle().getDataFormatString())) { value = dfz.format(cell.getNumericCellValue()); } else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) { value = sdf.format(cell.getDateCellValue()); } else { value = dfn.format(cell.getNumericCellValue()); } } else if(CellType.BOOLEAN.equals(ct)) { value = String.valueOf(cell.getBooleanCellValue()); } else if(CellType.BLANK.equals(ct)) { value = ""; } return value;}
注意:最后将循环中取到的单元格值放入Bean中,然后将值插入数据库。
如果是上传形式的导入功能,则需要在spring-mvc.xml中配置multipartResolver