[alibaba/easyexcel]无法解析所有得excel内容

2024-05-30 650 views
8

上传excel时发现如果内容有多个回车 只会获取最后一段数据

** @Data public class ExcelDemoListeners extends AnalysisEventListener {

private List<Demo> demos = new ArrayList<>();

@Override
public void invoke(Object data, AnalysisContext context) {
    Map<String,Object> map = (Map<String,Object>) data;

// Demo demo = new Demo(list.get(0), list.get(1)); // demos.add(demo); }

@Override
public void doAfterAllAnalysed(AnalysisContext context) {

}

}**

  @Slf4j
@Component
public class ExcelManager {

    public String uploadExcel(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."), filename.length());
        if (!(Objects.equals(suffix, ExcelTypeEnum.XLSX.getValue()) || Objects.equals(suffix, ExcelTypeEnum.XLS.getValue()))) {
            log.info("传入得文件不是excel文件--{}", filename);
            return null;
        }
        InputStream inputStream = file.getInputStream();
        ExcelDemoListeners listeners = new ExcelDemoListeners();
        EasyExcelFactory.read(inputStream, listeners).headRowNumber(1).sheet("Sheet1").doReadSync();
        List<Demo> demos = listeners.getDemos();
        return JSONObject.toJSONString(demos);
    }
}

** excel 内容为:

MySQL安装
请参考MySQL环境安装
笔者安装的时候的MySQL版本是8.0.15
库表的创建

最后解析出来得数据 "1" -> "笔者安装的时候的MySQL版本是8.0.15 库表的创建"

**

回答

7

image

2

image

6
com.alibaba easyexcel 2.0.0-beta1 commons-collections4 org.apache.commons
2

用这个版本依赖得poi是可以获取到对应得excel得内容 [["1","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["2","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["3","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["4","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["5","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["6","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["7","MySQL安装\n\n请参考MySQL环境安装\n笔者安装的时候的MySQL版本是8.0.15\n\n库表的创建"],["8","依赖环境准备\n\nDemo代码下载\n\nDemo下载地址:github\n代码下载后结构:"],["9","依赖环境准备\n\nDemo代码下载\n\nDemo下载地址:github\n代码下载后结构:"]]

7
public static <T> List<T> resolverExcel07(InputStream is, Integer headerRow, String sheetName, Integer columnCount, Class<T> clazz)
            throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
        XSSFWorkbook document = new XSSFWorkbook(is);
        XSSFSheet sheet = document.getSheet(sheetName);
        if (Objects.isNull(headerRow)) {
            headerRow = 0;
        }
        List<List<String>> lists = new ArrayList<>();
        for (int i = headerRow; ; i++) {
            XSSFRow row = sheet.getRow(i);
            if (Objects.isNull(row)) {
                break;
            }
            XSSFCell cell = row.getCell(0);
            cell.setCellType(CellType.STRING);
            String seq = cell.getStringCellValue();
            if (StringUtils.isBlank(seq)) {
                break;
            }
            List<String> list = new ArrayList<>();
            for (int j = 0; j < columnCount; j++) {
                XSSFCell innerCell = row.getCell(j);
                innerCell.setCellType(CellType.STRING);
                list.add(innerCell.getStringCellValue());
            }
            lists.add(list);
        }
        return assembleObject(lists, clazz);
    }

private static <T> List<T> assembleObject(List<List<String>> lists, Class<T> clazz)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        List<Object> objs = new ArrayList<>();
        for (List<String> list : lists) {
            Object o = Class.forName(clazz.getName()).newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                Annotation[] annotations = field.getAnnotations();
                if (ArrayUtils.isEmpty(annotations)) {
                    continue;
                }
                for (Annotation annotation : annotations) {
                    if (annotation instanceof ExcelProperty) {
                        ExcelProperty property = (ExcelProperty) annotation;
                        int index = property.index();
                        field.setAccessible(true);
                        field.set(o, list.get(index));
                        break;
                    }
                }
            }
            objs.add(o);
        }
        return (List<T>) objs;
    }
6

确认已经升级到了 2.0.5 。然后提供下 xlsx文件。我这里好排查

9

2.0.5这个已经修复了