java-POI处理excel文件方法
处理excel文件的开源库有很多,常见的POI、jxls。。。。。
重点分析下POI处理excel的方法:
1、写文件
// 按照行优先进行数据表格的初始化 public static void createRows() throws IOException { Workbook wb = new HSSFWorkbook(); // 创建表格 Sheet sheet = wb.createSheet("测试Sheet_01"); List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); int rowCount = list.size(); // 根据数据集设置行数 for (int i = 0; i < rowCount; i++) { Row row = sheet.createRow(i);// 创建行,表头是第0行 for (int j = 0; j < list.size(); j++) { // 为第一个单元格赋值 Cell cell = row.createCell(0); cell.setCellValue("测试数据"); // 为第二个单元格赋值 Cell cell_1 = row.createCell(1); cell_1.setCellValue("test"); } } FileOutputStream fos = new FileOutputStream("/Users/file/测试的Excel.xls"); wb.write(fos); if (null != fos) { fos.close(); } System.out.println("测试数据完成输出。。"); }
2、读文件
// 将文件内容转换为二维数组 @SuppressWarnings("resource") private static String[][] getData(FileItem fileItem, int ignoreRows) throws FileNotFoundException, IOException { List<String[]> result = new ArrayList<String[]>(); int rowSize = 0; BufferedInputStream in = new BufferedInputStream(fileItem.getInputStream()); Workbook wb; if (fileItem.getName().indexOf(".xlsx") > -1) { wb = new XSSFWorkbook(in); } else { wb = new HSSFWorkbook(in); } Cell cell = null; for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { Sheet st = wb.getSheetAt(sheetIndex); // 第一行为标题,不取 for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) { Row row = st.getRow(rowIndex); if (row == null) { continue; } int tempRowSize = row.getLastCellNum() + 1; if (tempRowSize > rowSize) { rowSize = tempRowSize; } String[] values = new String[rowSize]; Arrays.fill(values, ""); boolean hasValue = false; for (int columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { String value = ""; cell = row.getCell(columnIndex); if (cell != null) { //按照单元格数据类型进行数据处理 switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: if (HSSFDateUtil.isCellDateFormatted(cell)) { Date date = cell.getDateCellValue(); if (date != null) { value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } else { value = ""; } } else { value = new DecimalFormat("0").format(cell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_FORMULA: // 导入时如果为公式生成的数据则无值 if (!cell.getStringCellValue().equals("")) { value = cell.getStringCellValue(); } else { value = cell.getNumericCellValue() + ""; } break; case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_ERROR: value = ""; break; case HSSFCell.CELL_TYPE_BOOLEAN: value = (cell.getBooleanCellValue() == true ? "Y" : "N"); break; default: value = ""; } } if (columnIndex == 0 && value.trim().equals("")) { break; } values[columnIndex] = rightTrim(value); hasValue = true; } if (hasValue) { result.add(values); } } } in.close(); String[][] returnArray = new String[result.size()][rowSize]; for (int i = 0; i < returnArray.length; i++) { returnArray[i] = (String[]) result.get(i); } return returnArray; }
上述文件处理的过程能够判断单元格的数据类型,判断逻辑较为繁琐,如果是自行定义模板文件可以直接将文件模板中的单元格数据类型全部处理成文本类型;
这样后台程序全部按照字符串数据类型处理即可,相对容易,而且不易出错。
3、容易出现的错误
对office2003和office2007的处理异常,异常如下
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)
解决方法是按照上面的
fileItem.getName().indexOf(".xlsx")文件后缀的判断逻辑进行分类处理即可。