* {所需相关jar下载: * commons-collections4-4.4.jar * commons-compress-1.19.jar * poi-4.1.1.jar * poi-ooxml-4.1.1.jar * poi-ooxml-schemas-4.1.1.jar * }
import java.io.FileInputStream; 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.CellType; 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; /** * @author 马家立 * @version 创建时间:2019年11月15日下午5:56:37 * @Description:TODO 读取Excel相关类 * {所需相关jar: * commons-collections4-4.4.jar * commons-compress-1.19.jar * poi-4.1.1.jar * poi-ooxml-4.1.1.jar * poi-ooxml-schemas-4.1.1.jar * } */ public class ReadExcel { /** * @Title:getDataFromExcel * @author:马家立 * @date:2019年11月19日 上午10:02:22 * @Description:TODO 根据Excel的地址读取Excel表格数据 * @param filePathExcel的绝对路径 * @return Map<String,Object> {end:ok或者error;msg:错误信息原因;counts:读取条数} */ public static Map<String, Object> getDataFromExcel(String filePath) { // 声明结果map Map<String, Object> resultMap = new HashMap<String, Object>(); // 判断是否为excel类型文件 if (!filePath.endsWith(".xls") && !filePath.endsWith(".xlsx")) { System.out.println("文件不是excel类型"); resultMap.put("end", "error"); resultMap.put("msg", "文件不是excel类型"); return resultMap; } // 声明文本输入流 FileInputStream fis = null; // 声明一个新的工作簿 Workbook wookbook = null; // 声明一个新的工作表 Sheet sheet = null; try { // 获取一个绝对地址的流 fis = new FileInputStream(filePath); // 2003版本的excel,用.xls结尾, 2007版本以.xlsx if (filePath.endsWith(".xls")) { wookbook = new HSSFWorkbook(fis);// 得到工作簿 } else { // XSSFWorkbook wookbook = new XSSFWorkbook(fis);// 得到工作簿 } // 得到第一个工作表 sheet = wookbook.getSheetAt(0); // 得到第二个工作表 // sheet = wookbook.getSheetAt(1); // 得到第三个工作表 // sheet = wookbook.getSheetAt(2); // 封装处理Excel工作子表的数据 resultMap = packageDataBySheet(sheet); } catch (Exception e) { e.printStackTrace(); } return resultMap; } /** * @Title:packageDataBySheet * @author:马家立 * @date:2019年11月19日 上午9:55:26 * @Description:TODO 封装处理Excel工作子表的数据 * @param sheetExcel工作簿中的子表 * @return Map<String,Object> {end:ok或者error;msg:错误信息原因;counts:读取条数} */ public static Map<String, Object> packageDataBySheet(Sheet sheet) { // 返回结果map Map<String, Object> resultMap = new HashMap<String, Object>(); try { resultMap.put("end", "ok"); // 获得表头 Row rowHead = sheet.getRow(0); // 获取Excel的所有行数量 int rows = sheet.getLastRowNum(); // 获取Excel的所有列数量 int lines = rowHead.getPhysicalNumberOfCells(); if (0 == rows) { System.out.println("Excel文件内没有数据!"); resultMap.put("end", "error"); resultMap.put("msg", "Excel文件内没有数据!"); return resultMap; } // 读取条数 int counts = 0; // 是否跳过读取下一行 boolean isNext = false; // 外圈循环读取所有行:获得所有行的数据 for (int i = 0; i <= rows; i++) { // 是否跳过读取下一行:每次初始化为false isNext = false; counts++; // 获得第i行对象 Row row = sheet.getRow(i); // 获取单元格为空则直接下一行 if (isNullAndEmpty(row)) { continue; } List<String> list = new ArrayList<>(); // 内圈循环读取所有列:遍历每一行的的数据,lineNum:列数 for (int j = 0; j < lines; j++) { // 获取该单元格相应的类型的值 String str = getRightTypeCell(row.getCell(j), i, j); // 如果第一列为空则直接读取下一行 if (isNullAndEmpty(str) && (0 == j)) { isNext = true; break; } list.add(str); } // 是否跳过读取下一行 if (isNext) { continue; } String str1 = list.get(0); // 参数1 // String str2 = list.get(1); // 参数2 // String str3 = list.get(2); // 参数3 // and so on... if (i == 0) { if ("str1".endsWith(str1)) { System.out.println("读取的排课Excel数据格式正确"); } else { resultMap.put("end", "error"); resultMap.put("msg", "读取的排课Excel数据格式错误!"); System.err.println("读取的排课Excel数据格式错误"); break; } } else { /** * 处理数据 */ } } resultMap.put("counts", counts + ""); } catch (Exception e) { e.printStackTrace(); resultMap.put("end", "error"); resultMap.put("msg", "OperationExcel的packageDataBySheet方法异常!"); } return resultMap; } /** * @Title:getRightTypeCell * @author:马家立 * @date:2019年11月18日 下午5:37:04 * @Description:TODO 返回该单元格相应的类型的值 * @param cell一个单元格的对象 * @param rowNum行数 * @param lineNum列数 * @return * @throws Exception * String */ public static String getRightTypeCell(Cell cell, int rowNum, int lineNum) throws Exception { // 单元格内容 String value = ""; System.out.println("rowNum:" + rowNum + "\tlineNum:" + lineNum); // 如果单元格为空或者单元格里面的数据为空则返回 if (isNullAndEmpty(cell) || isNullAndEmpty(cell.getStringCellValue())) { value = ""; } if ((cell == null) || cell.equals(null) || (cell.getCellType() == CellType.BLANK)) { value = ""; } else { // 判断数据类型 switch (cell.getCellType()) { case FORMULA: value = "" + cell.getCellFormula(); break; case NUMERIC: value = "" + cell.getNumericCellValue(); break; case STRING: value = cell.getStringCellValue(); break; default: break; } } return value; } /** * @Title:isNullAndEmpty * @author:马家立 * @date:2019年11月19日 上午10:23:49 * @Description:TODO 校验对象是否为空 * @param obj校验对象 * @return boolean */ public static boolean isNullAndEmpty(Object obj) { if ((null != obj) && !"".equals(obj.toString()) && !"null".equals(obj)) { return false; } else { return true; } } public static void main(String[] args) throws Exception { // 读取文件地址 String file_path = "D:\\123.xlsx"; // 传对象是为了让该方法灵活 Map<String, Object> map = getDataFromExcel(file_path); String end = map.get("end") + ""; System.out.println("封装处理结果:" + end); } }
原文:https://www.cnblogs.com/mjtabu/p/11887746.html