Use Ali's EasyExcel to report Convert excel format exception. You can try specifying the 'excelType' yourself

79. Use Ali's EasyExcel to report Convert excel format exception. You can try specifying the 'excelType' yourself

1. Check the project version. If it is 2.x, it can be replaced with 3.1.x or above, and it will be automatically recognized

Without verification, the project uses 2.x. After changing the version number, many old things cannot be recognized

The version used by the project is 2.x, the solution

1、网上说是流的属性发生变化了,不是一个文件流,重新new了文件流还是报错  ----未解决
List<Object> objects = EasyExcel.read(new BufferedInputStream(inputStream), easyExcelExceptionUtil).sheet(sheetName.getSheetName()).doReadSync();

2、指定文件的类型    还是报错     ---未解决
List<Object> objects = EasyExcel.read(new BufferedInputStream(inputStream), easyExcelExceptionUtil).excelType(ExcelTypeEnum.XLS).sheet(sheetName.getSheetName()).doReadSync();

3、查看源码发现,read方法有好多种,决定更换读取方式,放置文件的路径  -- 解决
List<Object> objects = EasyExcel.read(caseUploadPath + dataImportDto.getFileUrl(), easyExcelExceptionUtil).sheet(sheetName.getSheetName()).doReadSync();
此时发现读取只能读取单个类型的文件xlsx 或者xls 中的一种
加上判断,用来区分文件类型
 根据文件来切割获取文件类型
String[] fileNameArr = file.getName().split("\\.");
List<Object> objects;
//				xls 或者 xlxs
if (ObjectUtil.equal(fileNameArr[1],"xls")){
    
    
    objects = EasyExcel.read(caseUploadPath + dataImportDto.getFileUrl(), easyExcelExceptionUtil).excelType(ExcelTypeEnum.XLS).sheet(sheetName.getSheetName()).doReadSync();
}else {
    
    
    objects = EasyExcel.read(caseUploadPath + dataImportDto.getFileUrl(), easyExcelExceptionUtil).sheet(sheetName.getSheetName()).doReadSync();
}

Some methods of read

    /**
     * Build excel the read
     *
     * @param pathName
     *            File path to read.
     * @param head
     *            Annotate the class for configuration information.
     * @param readListener
     *            Read listener.
     * @return Excel reader builder.
     */
    public static ExcelReaderBuilder read(String pathName, Class head, ReadListener readListener) {
    
    
        ExcelReaderBuilder excelReaderBuilder = new ExcelReaderBuilder();
        excelReaderBuilder.file(pathName);
        if (head != null) {
    
    
            excelReaderBuilder.head(head);
        }
        if (readListener != null) {
    
    
            excelReaderBuilder.registerReadListener(readListener);
        }
        return excelReaderBuilder;
    }

    /**
     * Build excel the read
     *
     * @param inputStream
     *            Input stream to read.
     * @return Excel reader builder.
     */
    public static ExcelReaderBuilder read(InputStream inputStream) {
    
    
        return read(inputStream, null, null);
    }

    /**
     * Build excel the read
     *
     * @param inputStream
     *            Input stream to read.
     * @param readListener
     *            Read listener.
     * @return Excel reader builder.
     */
    public static ExcelReaderBuilder read(InputStream inputStream, ReadListener readListener) {
    
    
        return read(inputStream, null, readListener);
    }

Guess you like

Origin blog.csdn.net/weixin_43987718/article/details/130171170