eazyexcel生成校验单元格内容的excel文件

功能描述

有的时候需要生成excel文件 对单元格填的值进行校验符不符合规则。

这里给出的例子是 excel中填充下拉框选项,如果输入的值不符合下拉框选项 则弹出提示

maven依赖

java 复制代码
  <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>

controller内方法

java 复制代码
@GetMapping("/down") 
public void downTemplate(HttpServletResponse response) throws IOException {


    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
    String fileName = URLEncoder.encode("FileName", "UTF-8").replaceAll("\\+", "%20");
    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    
    ArrayList<String> optionList= Lists.newArrayList();
    optionList.add("选项A");
    optionList.add("选项B");
    optionList.add("选项C");
    SelectedWriteStyleHandler selectedWriteStyleHandler = SelectedWriteStyleHandler.buildSelectList(2,optionList);
    EasyExcel.write(response.getOutputStream(), EvaluationUserImportExcel.class)
            .registerWriteHandler(selectedWriteStyleHandler).sheet("sheetName").doWrite(Lists.newArrayList());

}

自定义SelectedWriteStyleHandler

java 复制代码
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.google.common.collect.Maps;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.List;
import java.util.Map;

/**
 * @Description 
 * @Date 2024-04-29 16:27 
 **/

public class SelectedWriteStyleHandler implements SheetWriteHandler {


    public SelectedWriteStyleHandler(int column,List<String> selectedList){
        this.selectedList=selectedList;
        this.column=column;

    }

    public static SelectedWriteStyleHandler buildSelectList(int column,List<String> selectedList){
       return new SelectedWriteStyleHandler(column,selectedList);
    }
    private List<String> selectedList;

    private int column;
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

        Map<Integer,List<String>> selectMapParam= Maps.newHashMap();
        selectMapParam.put(column,selectedList);

        Sheet sheet = writeSheetHolder.getCachedSheet();
        DataValidationHelper helper = sheet.getDataValidationHelper();

        Workbook cachedWorkbook = writeWorkbookHolder.getCachedWorkbook();

        int index=1;

        for (Map.Entry<Integer, List<String>> entry : selectMapParam.entrySet()) {

            String sheetDicName="dictSheel"+index;
            Sheet dicSheel = cachedWorkbook.createSheet(sheetDicName);

            // 隐藏字典页
            cachedWorkbook.setSheetHidden(index++,true);

            CellRangeAddressList cellList=new CellRangeAddressList(1,1048575,entry.getKey(),entry.getKey());

            int rowLength=entry.getValue().size();
            for (int i = 0; i < rowLength; i++) {
                dicSheel.createRow(i).createCell(0).setCellValue(entry.getValue().get(i));
            }

            String refers=sheetDicName+"!$A$1:$A$"+entry.getValue().size();
            Name name = cachedWorkbook.createName();
            name.setNameName(sheetDicName);
            name.setRefersToFormula(refers);

            DataValidationConstraint listConstraint = helper.createFormulaListConstraint(sheetDicName);

            DataValidation validation = helper.createValidation(listConstraint, cellList);
            // 显示下拉箭头
            validation.setSuppressDropDownArrow(true);
            // 显示错误弹出框
            validation.setShowErrorBox(true);
            validation.createErrorBox("数据错误", "您输入的内容,不符合限制条件。");
//            validation.createPromptBox("XXX","请选择下拉项中的字典值");
            // 选择下拉项后是否允许清除单元格
            validation.setEmptyCellAllowed(false);
            validation.setErrorStyle(DataValidation.ErrorStyle.STOP);

            sheet.addValidationData(validation);

        }


    }
}
相关推荐
bin91538 小时前
【EXCEL数据处理】000017 案例 Match和Index函数。
excel
shandianchengzi10 小时前
【记录】Excel|Excel 打印成 PDF 页数太多怎么办
pdf·excel
bin915313 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
一个散步者的梦20 小时前
Excel常用函数
excel
bin91531 天前
【EXCEL数据处理】000009 案列 EXCEL单元格数字格式。文本型数字格式和常规型数字格式的区别
大数据·前端·数据库·信息可视化·数据分析·excel·数据可视化
Eiceblue2 天前
Python保留数据删除Excel单元格的函数和公式
开发语言·python·excel
bin91532 天前
【EXCEL数据处理】000014 案例 EXCEL分类汇总、定位和创建组。附多个操作案例。
信息可视化·数据挖掘·数据分析·excel·数据可视化·数据图表·excel 数据分析
育种数据分析之放飞自我2 天前
GWAS分析中显著位点如何注释基因:excel???
linux·算法·excel
PowerBI学谦2 天前
Python in Excel 正式发布!
开发语言·python·excel
bin91532 天前
【EXCEL数据处理】000011 案列 EXCEL带有三角形图标的单元格转换,和文本日期格式转换。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化