Java 读取Excel文件数据,兼容.xls与.xlsx

/**
 * @param filePath 文件路径
 * @Description 判断Excel版本
 * @Throws
 * @Return org.apache.poi.ss.usermodel.Workbook
 * @Date 2020-07-23 18:56:29
 * @Author WangKun
 */
public static Workbook readExcel(String filePath) {
    if (filePath == null) {
        return null;
    }
    String extString = filePath.substring(filePath.lastIndexOf("."));
    try {
        InputStream is = new FileInputStream(filePath);
        if (".xls".equals(extString)) {
            return new HSSFWorkbook(is);
        } else if (".xlsx".equals(extString)) {
            return new XSSFWorkbook(is);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
/**
 * @param file
 * @Description 文件解析
 * @Throws
 * @Return void
 * @Date 2020-07-23 18:57:32
 * @Author WangKun
 */
public void importExcel(File file) {
        Workbook wb;
        Sheet sheet;
        Row row;
        wb = readExcel(file.getAbsolutePath());
        if (wb != null) {
            try {
                List>> list = new ArrayList<>();
                // 循环页签
                for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
                    // 指定页签的值
                    sheet = wb.getSheetAt(sheetNum);
                    // 定义存放一个页签中所有数据的List
                    List> sheetList = new ArrayList<>();
                    // 循环行
                    for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                        // 指定行的值
                        row = sheet.getRow(rowNum);
                        // 定义存放一行数据的List
                        List rowList = new ArrayList<>();
                        // 循环列
                        for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
                            Cell cell = sheet.getRow(rowNum).getCell(cellNum);
                            rowList.add(getStringCellValue(cell));
                        }
                        sheetList.add(rowList);
                    }
                    list.add(sheetList);
                    //得到数据list
                }
                // 处理数据
            } catch (Exception e) {
                logger.error("遍历数据失败", e);
                e.printStackTrace();
            }
        }
    }
 
  
/**
 * @param cell
 * @Description 单元格格式
 * @Throws
 * @Return java.lang.String
 * @Date 2020-07-23 18:56:54
 * @Author WangKun
 */
public static String getStringCellValue(Cell cell) {
    String cellvalue;
    if (cell == null) {
        return "";
    }
    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            cellvalue = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = cell.getDateCellValue();
                cellvalue = sdf.format(date);
            } else {
                cellvalue = String.valueOf(cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cellvalue = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            cellvalue = "";
            break;
        default:
            cellvalue = "";
            break;
    }
    return cellvalue;
}

mave配置



    org.apache.xmlbeans
    xmlbeans
    2.6.0


    org.apache.poi
    poi
    3.9


    org.apache.poi
    poi-ooxml
    3.9


    org.apache.poi
    poi-ooxml-schemas
    3.9


    org.apache.commons
    commons-collections4
    4.1

关于java读取Excel表格数据流出现的一系列报错问题的解决方案,含辛茹苦磨砺总结得出!

参考:https://blog.csdn.net/weixin_40295575/article/details/81354099

 

 

 

你可能感兴趣的:(工作记录,poi,excel,java,spring)