EasyExcel导出自定义样式 背景 字体等定义

如题 :
公司有个需求 是导入的要每个字段进行校验,不符合规则则不让导入并保存进行导出并将错误字段进行标红

公司用的是EasyExcel做的导出   网上查询相关资料 找到解决办法如下
了解到EasyExcel可以进行自定义样式 
以下为代码
EasyExcel.write(response.getOutputStream(), TServicePersonExcelNewDTO.class)
                .inMemory(true)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())//自定义样式
                .registerWriteHandler(new CustomCellWriteHandler())//自定义样式  
                .sheet().doWrite(list);

以下为自定义样式类 因我需求是单元格变色 所以字体变色就进行注释处理了 可以根据自己需求进行自己选择

public class CustomCellWriteHandler  extends AbstractCellWriteHandler {
    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        // 设置行高
        short height = 650;
        row.setHeight(height);
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        Sheet sheet = writeSheetHolder.getSheet();
        sheet.setColumnWidth(cell.getColumnIndex(), 3000);
        //当前行的第i列
        int i = cell.getColumnIndex();
        //不处理第一行
        if (0 != cell.getRowIndex()) {
        	//此处判断为不符合要求 所以需求进行标红处理  可以根据自己需求进行判断使用  或者全量使用
            if (cell.getStringCellValue().contains("_false")) {
                // 根据单元格获取workbook
                Workbook workbook = cell.getSheet().getWorkbook();
                //设置行高
                writeSheetHolder.getSheet().getRow(cell.getRowIndex()).setHeight((short) (1.4 * 256));
                // 单元格策略
                WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
                // 设置背景颜色白色
                contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
                // 设置垂直居中为居中对齐
                contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                // 设置左右对齐为靠左对齐
                contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
                // 设置单元格上下左右边框为细边框
                contentWriteCellStyle.setBorderBottom(BorderStyle.MEDIUM);
                contentWriteCellStyle.setBorderLeft(BorderStyle.MEDIUM);
                contentWriteCellStyle.setBorderRight(BorderStyle.MEDIUM);
                contentWriteCellStyle.setBorderTop(BorderStyle.MEDIUM);
                // 创建字体实例
                WriteFont cellWriteFont = new WriteFont();
                // 设置字体大小
                //cellWriteFont.setFontName("宋体");
                cellWriteFont.setFontHeightInPoints((short) 12);
                //设置字体颜色
                //cellWriteFont.setColor(IndexedColors.RED1.getIndex());
                //单元格颜色
                contentWriteCellStyle.setFillForegroundColor(IndexedColors.RED1.getIndex());
                contentWriteCellStyle.setWriteFont(cellWriteFont);
                CellStyle cellStyle = StyleUtil.buildHeadCellStyle(workbook, contentWriteCellStyle);
                //设置当前行第i列的样式
                cell.getRow().getCell(i).setCellStyle(cellStyle);
            }        }

    }

以下为导出效果 可以看到不符合要求部分背景色已变成红色
493f0bdbd8404145b4a4b10c184f9d24.png

根据提供的引用内容,easyExcel虽然比poi更为简单api和更高性能,但是其下载格式不符合要求,需要进行个性化需求的设置。而easyExcel导出定义图片格式,可以通过以下步骤实现: 1. 首先,需要在pom.xml文件中添加依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> </dependency> ``` 2. 然后,在需要导出的实体类中,添加一个byte[]类型的字段,用于存储图片的二进制数据。 ```java public class DemoData { private String name; private Integer age; private byte[] image; // 省略getter/setter方法 } ``` 3. 接着,在写入Excel时,将图片的二进制数据写入到对应的单元格中。 ```java // 构造数据 List<DemoData> list = new ArrayList<>(); DemoData data = new DemoData(); data.setName("张三"); data.setAge(20); // 读取图片文件 File file = new File("image.png"); byte[] imageBytes = FileUtils.readFileToByteArray(file); data.setImage(imageBytes); list.add(data); // 写入Excel String fileName = "demo.xlsx"; String sheetName = "Sheet1"; ExcelWriter excelWriter = EasyExcel.write(fileName).build(); WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build(); excelWriter.write(list, writeSheet); excelWriter.finish(); ``` 4. 最后,在读取Excel时,将图片的二进制数据读取出来,并将其转换为图片格式。 ```java // 读取Excel String fileName = "demo.xlsx"; String sheetName = "Sheet1"; ExcelReader excelReader = EasyExcel.read(fileName).build(); ReadSheet readSheet = EasyExcel.readSheet(sheetName).build(); List<DemoData> list = excelReader.read(readSheet).head(DemoData.class).sheet().doReadSync(); // 将图片的二进制数据转换为图片格式 for (DemoData data : list) { byte[] imageBytes = data.getImage(); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes)); // 处理图片 // ... } excelReader.finish(); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值