使用easyexcel做数据校验错误导出excel表格批注+背景(1.0) 继续优化升级

最终效果:
在这里插入图片描述
这次对文件进行了进一步的提升,优化, 这次是批量数据校验判断,只判断了是否为空,空就作批注加背景.如下:
这模拟数据.还需要更多的实验才能优化

待优化点:
待升级 动态获取获取head头
待处理 对没一列进行数据校验,因为目前只做了""处理
待优化批注提示语 目前使用的是map 使用的是map.get(i) 来填充.

//批注语 
Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(0, "必填");
            map.put(1, "必填!");
            map.put(2, "重要");

目前使用的是模拟数据测试

//模拟表格空数据. 
private List<DemoData1> data1() {
        List<DemoData1> list = new ArrayList<DemoData1>();
        list.add(new DemoData1("字符串1",new Date(),0.56));
        list.add(new DemoData1("字符串2",new Date(),0.57));
        list.add(new DemoData1(null,new Date(),0.58));
        list.add(new DemoData1("字符串4",new Date(),0.59));
        list.add(new DemoData1("字符串5",null,0.60));
        list.add(new DemoData1("字符串6",new Date(),null));
        list.add(new DemoData1("字符串7",new Date(),0.62));
        list.add(new DemoData1("字符串8",new Date(),0.63));
        return list;
    }

实体类 :
需要注意点就是表格头是根据value值来填充的,如果没有
value注解(@ExcelProperty(index = 0,value = “字符串标题”))默认使用的 变量名来代替

package com.alibaba.easyexcel.test.demo.write;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
 * 基础数据类
 *
 * @author Jiaju Zhuang
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DemoData1 {
    @ExcelProperty(index = 0,value = "字符串标题")
    private String string;
    @ExcelProperty(index = 1,value = "日期标题")
    private Date date;
    @ExcelProperty(index = 2,value = "数字标题")
    private Double doubleData;
}

测试类:
需开启内存不然无法批注

 /**
     * 插入批注
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link DemoData}
     * <p>
     * 2. 注册拦截器 {@link CommentWriteHandler}
     * <p>
     * 2. 直接写即可
     */
    @Test
    public void commentWrite1() {
        String fileName = "d:/"+ "commentWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
        // 这里要注意inMemory 要设置为true,才能支持批注。目前没有好的办法解决 不在内存处理批注。这个需要自己选择。
        EasyExcel.write(fileName, DemoData1.class).inMemory(Boolean.TRUE).registerWriteHandler(new CommentWriteHandler1())
            .sheet("模板").doWrite(data1());
    }

实现类 CommentWriteHandler1
注意一点,如果是对空单元格进行处理的话,写的话是需要创建单元格的不能使用getCell来获取,不然会报错:
sheet.getRow(relativeRowIndex + 1).createCell(i).setCellComment(comment);

package com.alibaba.easyexcel.test.demo.write;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 自定义拦截器.新增注释,第一行头加批注
 *
 * @author Jiaju Zhuang
 */
public class CommentWriteHandler1 extends AbstractRowWriteHandler {
    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
        //如果false 就读取行数据.
        if (!isHead) {
            Sheet sheet = writeSheetHolder.getSheet();
            //用来做批注语
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(0, "必填");
            map.put(1, "必填!");
            map.put(2, "重要");
            //循环是设置批量批示的
            for (int i = 0; i < 3; i++) {
                // String value = row.getCell(i+1).getStringCellValue();
                Cell cell = row.getCell(i);
                if ("".equals(cell.toString())) {
                    setPostil(sheet, relativeRowIndex, i,map.get(i));
                }
            }
        }
    }
    private void setPostil(Sheet sheet, Integer relativeRowIndex, Integer i,String msg) {
        Workbook workbook = sheet.getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        //设置前景填充样式
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //设置前景色为红色
        cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        //设置垂直居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();
        // 在第一行 第二列创建一个批注
        Comment comment =
            drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 3, 3, (short) 5, 6));
        // 输入批注信息
        comment.setString(new XSSFRichTextString(msg));
        // 将批注添加到单元格对象中 对空处理的话是需要创建的 createCell
        sheet.getRow(relativeRowIndex + 1).createCell(i).setCellComment(comment);
        sheet.getRow(relativeRowIndex + 1).createCell(i).setCellStyle(cellStyle);
    }
}

依赖:
目前最新2.2.0-beta2
由于使用mvn导入无法下载,只能通过下载源码进行测试,有很多限制.

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.0-beta2</version>
        </dependency>

后续会继续更新. 如有帮助麻烦点个赞谢谢.如有问题,欢迎指正.共同学习

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值