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


    功能描述

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

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

    maven依赖

    1. cn.afterturn
    2. easypoi-spring-boot-starter
    3. 4.4.0

    controller内方法

    1. @GetMapping("/down")
    2. public void downTemplate(HttpServletResponse response) throws IOException {
    3. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    4. response.setCharacterEncoding("utf-8");
    5. // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
    6. String fileName = URLEncoder.encode("FileName", "UTF-8").replaceAll("\\+", "%20");
    7. response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    8. ArrayList optionList= Lists.newArrayList();
    9. optionList.add("选项A");
    10. optionList.add("选项B");
    11. optionList.add("选项C");
    12. SelectedWriteStyleHandler selectedWriteStyleHandler = SelectedWriteStyleHandler.buildSelectList(2,optionList);
    13. EasyExcel.write(response.getOutputStream(), EvaluationUserImportExcel.class)
    14. .registerWriteHandler(selectedWriteStyleHandler).sheet("sheetName").doWrite(Lists.newArrayList());
    15. }

    自定义SelectedWriteStyleHandler 

    1. import com.alibaba.excel.write.handler.SheetWriteHandler;
    2. import com.alibaba.excel.write.handler.context.SheetWriteHandlerContext;
    3. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
    4. import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
    5. import com.google.common.collect.Maps;
    6. import org.apache.poi.ss.usermodel.*;
    7. import org.apache.poi.ss.util.CellRangeAddress;
    8. import org.apache.poi.ss.util.CellRangeAddressList;
    9. import java.util.List;
    10. import java.util.Map;
    11. /**
    12. * @Description
    13. * @Date 2024-04-29 16:27
    14. **/
    15. public class SelectedWriteStyleHandler implements SheetWriteHandler {
    16. public SelectedWriteStyleHandler(int column,List selectedList){
    17. this.selectedList=selectedList;
    18. this.column=column;
    19. }
    20. public static SelectedWriteStyleHandler buildSelectList(int column,List selectedList){
    21. return new SelectedWriteStyleHandler(column,selectedList);
    22. }
    23. private List selectedList;
    24. private int column;
    25. @Override
    26. public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
    27. Map> selectMapParam= Maps.newHashMap();
    28. selectMapParam.put(column,selectedList);
    29. Sheet sheet = writeSheetHolder.getCachedSheet();
    30. DataValidationHelper helper = sheet.getDataValidationHelper();
    31. Workbook cachedWorkbook = writeWorkbookHolder.getCachedWorkbook();
    32. int index=1;
    33. for (Map.Entry> entry : selectMapParam.entrySet()) {
    34. String sheetDicName="dictSheel"+index;
    35. Sheet dicSheel = cachedWorkbook.createSheet(sheetDicName);
    36. // 隐藏字典页
    37. cachedWorkbook.setSheetHidden(index++,true);
    38. CellRangeAddressList cellList=new CellRangeAddressList(1,1048575,entry.getKey(),entry.getKey());
    39. int rowLength=entry.getValue().size();
    40. for (int i = 0; i < rowLength; i++) {
    41. dicSheel.createRow(i).createCell(0).setCellValue(entry.getValue().get(i));
    42. }
    43. String refers=sheetDicName+"!$A$1:$A$"+entry.getValue().size();
    44. Name name = cachedWorkbook.createName();
    45. name.setNameName(sheetDicName);
    46. name.setRefersToFormula(refers);
    47. DataValidationConstraint listConstraint = helper.createFormulaListConstraint(sheetDicName);
    48. DataValidation validation = helper.createValidation(listConstraint, cellList);
    49. // 显示下拉箭头
    50. validation.setSuppressDropDownArrow(true);
    51. // 显示错误弹出框
    52. validation.setShowErrorBox(true);
    53. validation.createErrorBox("数据错误", "您输入的内容,不符合限制条件。");
    54. // validation.createPromptBox("XXX","请选择下拉项中的字典值");
    55. // 选择下拉项后是否允许清除单元格
    56. validation.setEmptyCellAllowed(false);
    57. validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
    58. sheet.addValidationData(validation);
    59. }
    60. }
    61. }

  • 相关阅读:
    Apache网页优化
    备战数学建模47-数模常规算法之图论(攻坚站12)
    每日一题——LeetCode1646.获取生成数组中的最大值
    [Python]多态、类方法、类属性
    基于Python的海量豆瓣电影、数据获取、数据预处理、数据分析、可视化、大屏设计项目(含数据库)
    ElasticSearch ( 三 ) 基本操作
    计算机、通信方向学习考证经验分享
    [零基础学IoT Pwn] 复现Netgear WNAP320 RCE
    0-30 VDC 稳压电源,电流控制 0.002-3 A
    自然语言处理 Paddle NLP - 开放域对话系统-理论
  • 原文地址:https://blog.csdn.net/LQ137969328/article/details/139330974