项目中用到文件导入导出功能特别常见,尤其是excel来管理数据。
导入导出excel原理几乎一样,调用poi的api,但又各有侧重,导入侧重的是判别格式,包括null,"",以及正则表达式等等,
导出侧重的是生成格式,包括字体,颜色,合并单元格,数据有效性等等
今天先整个导入吧,相对简单点:
package org.john.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImportUtil { public static HashMap<String, List<Map<String, String>>> readExcel( String path) throws Exception { if (path == null) { return null; } System.out.println("@_@ the pathString is:" + path); Workbook wb = null; // 获取后缀名 String ext = path.substring(path.lastIndexOf(".")); System.out.println("@_@ the path after is :" + path); System.out.println("@_@ the ext is:" + ext); try { FileInputStream fis = new FileInputStream(path); System.out.println("fis:" + fis.toString()); if (".xls".equals(ext)) { // 03版本 wb = new HSSFWorkbook(fis); System.out.println("is 03 version"); } if (".xlsx".equals(ext)) { // 07版本 wb = new XSSFWorkbook(fis); System.out.println("is 07 version"); } else { wb = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (wb == null) { throw new Exception("Excel对象为空!!!"); } // 用hashMap存储多个sheet HashMap<String, List<Map<String, String>>> ExcelMap = new HashMap<String, List<Map<String, String>>>(); List<Map<String, String>> excelList = null; Map<String, String> excelMap = null; // 获取工作表个数 int numOfSheets = wb.getNumberOfSheets(); if (numOfSheets < 0) { return null; } for (int i = 0; i < numOfSheets; i++) { // 有隐藏的直接跳过 if (wb.isSheetHidden(i)) { continue; } Sheet sheet = wb.getSheetAt(i); // 这个用法也常见。 // Sheet sheet=wb.getSheet("sheet1"); String sheetName = wb.getSheetName(i); // list存储每个工作表的信息。 excelList = new ArrayList<Map<String, String>>(); // 工作行 Row row; // 工作单元格 Cell cell; // 行 int rownum = 0; // 列 int columnum = 0; // 获取总行数 rownum = sheet.getLastRowNum(); // 此处判断内容是否为空。先跳过 // if(rownum==0){} row = sheet.getRow(0); if (row == null) { continue; } // 获取总列数 columnum = row.getLastCellNum(); if (columnum == 0) { continue; } // 遍历每一条数据 for (int j = 0; j < rownum; j++) { // 获取列对象; row = sheet.getRow(j); if (row == null) { continue;// 跳过空行 } excelMap = new HashMap<String, String>(); // 循环每个cell for (int k = 0; k < columnum; k++) { // 对获取的单元格进行格式处理,全部设为String格式。 row.getCell(k).setCellType(Cell.CELL_TYPE_STRING); // 存储每个单元格的值。 String cellStr = row.getCell(k).getStringCellValue().trim(); // 遇到%需要特殊处理,因为Excel默认处理了百分数; if (row.getCell(k).getCellStyle().getDataFormatString() .indexOf("%") != -1) { System.out.println("@_@ cellStr before :" + cellStr); if (!cellStr.matches("[0-9.]+")) { throw new Exception("格式错误" + "第 " + j + "行,第 " + k + "列"); } cellStr = Double.parseDouble(cellStr) * 100 + "%"; System.out.println("@_@ cellStr after :" + cellStr); } // 存储每条数据 excelMap.put("column" + new Integer(k + 1), cellStr); } if (excelMap == null || excelMap.size() == 0) { continue; } excelList.add(excelMap); } System.out.println("@_@ excelList is :" + excelList); ExcelMap.put(sheetName, excelList); } return ExcelMap; } }