EasyExcel自定义参数的读取导出

EasyExcel自定义参数的读取导出

1、导出
	ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();
	// 写出额外参数的Handler
    excelWriterBuilder.registerWriteHandler(new SheetWriteHandler() {
        @Override
        public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
            Workbook workbook = writeWorkbookHolder.getWorkbook();
            // extraProp:额外参数Map
            for (Map.Entry<String, String> entry : extraProp.entrySet()) {
                if (workbook instanceof XSSFWorkbook){
                    // .xlsx设置文件属性
                    POIXMLProperties.CustomProperties customProperties = ((XSSFWorkbook) workbook).getProperties().getCustomProperties();
                    if (ExcelUtils.hasNoProperty(customProperties, entry.getKey())){
                        customProperties.addProperty(entry.getKey(), entry.getValue());
                    }
                } else if (workbook instanceof SXSSFWorkbook){
                    // .xlsx设置文件属性
                    POIXMLProperties.CustomProperties customProperties = ((SXSSFWorkbook) workbook).getXSSFWorkbook().getProperties().getCustomProperties();
                    if (ExcelUtils.hasNoProperty(customProperties, entry.getKey())) {
                        customProperties.addProperty(entry.getKey(), entry.getValue());
                    }
                } else if (workbook instanceof HSSFWorkbook){
                    // .xls设置文件属性
                    HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook;
                    hssfWorkbook.createInformationProperties();
                    DocumentSummaryInformation documentSummaryInformation = hssfWorkbook.getDocumentSummaryInformation();
                    CustomProperties customProperties = documentSummaryInformation.getCustomProperties();
                    if (Objects.isNull(customProperties)){
                        customProperties = new CustomProperties();
                    }
                    if (!ExcelUtils.hasProperty(customProperties, entry.getKey())) {
                        //添加自定义属性
                        customProperties.put(entry.getKey(), entry.getValue());
                        documentSummaryInformation.setCustomProperties(customProperties);
                    }
                }
            }
        }
    });
2、读取
Map<String, Object> extraProp = new HashMap<>();
try {
    // context: com.alibaba.excel.context.AnalysisContext
    ReadWorkbookHolder readWorkbookHolder = context.readWorkbookHolder();
    if (readWorkbookHolder instanceof XlsxReadWorkbookHolder) {
        // 获取.xlsx文件额外属性
        OPCPackage opcPackage = ((XlsxReadWorkbookHolder) readWorkbookHolder).getOpcPackage();
        POIXMLProperties poiProperties = new POIXMLProperties(opcPackage);
        POIXMLProperties.CustomProperties customProperties = poiProperties.getCustomProperties();
        List<CTProperty> propertyList = customProperties.getUnderlyingProperties().getPropertyList();
        for (CTProperty property : propertyList) {
            extraProp.put(property.getName(), property.getLpwstr());
        }
    } else if (readWorkbookHolder instanceof XlsReadWorkbookHolder){
        // 获取.xls文件额外属性
        DocumentSummaryInformation documentSummaryInformation = ((XlsReadWorkbookHolder) readWorkbookHolder).getHssfWorkbook().getDocumentSummaryInformation();
        CustomProperties customProperties = documentSummaryInformation.getCustomProperties();
        List<CustomProperty> propertyList = customProperties.properties();
        for (CustomProperty property : propertyList) {
            extraProp.put(property.getName(), property.getValue());
        }
    }
	// 读取的结果存储在extraProp中
} catch (XmlException | OpenXML4JException | IOException e) {
    throw new RuntimeException("解析Excel额外属性异常");
}

在这里插入图片描述

对于 EasyExcel 来说,自定义类转换器是用来处理 Excel 中的自定义对象转换的。下面是一个示例代码,演示了如何实现自定义类转换器: ```java // 自定义类转换器 public class CustomConverter implements Converter<CustomObject> { @Override public Class<CustomObject> supportJavaTypeKey() { return CustomObject.class; } @Override public CellData<?> convertToExcelData(CustomObject customObject, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { // 将自定义对象转换为需要导出的数据类型,比如字符串 String data = customObject.toString(); return new CellData<>(data); } @Override public CustomObject convertToJavaData(CellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) { // 将读取到的数据转换为自定义对象 String data = Objects.requireNonNull(cellData.getStringValue()); // 这里根据实际情况进行解析,创建自定义类对象 CustomObject customObject = new CustomObject(data); return customObject; } } ``` 在使用 EasyExcel 导出或导入 Excel 文件时,可以注册自定义类转换器,示例代码如下: ```java public class ExcelUtils { public static void exportData(List<CustomObject> dataList, String filePath) { // 创建工作簿 try (OutputStream outputStream = new FileOutputStream(filePath); ExcelWriter excelWriter = EasyExcel.write(outputStream).build()) { // 注册自定义类转换器 ConverterRegistry converterRegistry = excelWriter.getConverterRegistry(); converterRegistry.registerConverter(new CustomConverter()); // 设置表头和数据 Sheet sheet = new Sheet(1, 0, CustomObject.class); excelWriter.write(dataList, sheet); } catch (IOException e) { e.printStackTrace(); } } public static List<CustomObject> importData(String filePath) { // 读取Excel文件 try (InputStream inputStream = new FileInputStream(filePath); ExcelReader excelReader = EasyExcel.read(inputStream).build()) { // 注册自定义类转换器 ConverterRegistry converterRegistry = excelReader.getConverterRegistry(); converterRegistry.registerConverter(new CustomConverter()); // 设置表头和数据 Sheet sheet = new Sheet(1, 1, CustomObject.class); ReadSheet readSheet = EasyExcel.readSheet(0).build(); excelReader.read(readSheet); // 获取读取到的数据 List<CustomObject> dataList = readSheet.getList(); return dataList; } catch (IOException e) { e.printStackTrace(); } return Collections.emptyList(); } } ``` 以上代码中的 `CustomObject` 是自定义的类,你需要根据实际情况进行替换。同时,你可以根据需要自定义其他类型的转换器来处理不同类型的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值