当前位置:   article > 正文

easyexcel 3.0.x 版本实现指定列 锁定以及指定列隐藏_easyexcel 隐藏列

easyexcel 隐藏列

1:效果示例

2:代码示例:

UnLockCell.java
  1. package com.example.juc.zhujie;
  2. /**
  3. * @Author
  4. * @Date Created in 2023/12/19 10:09
  5. * @DESCRIPTION:
  6. * @Version V1.0
  7. */
  8. import java.lang.annotation.*;
  9. /**
  10. * 用于标记锁定哪些列不需要锁定
  11. * @author 12926
  12. */
  13. @Target(value = {ElementType.FIELD})
  14. @Retention(RetentionPolicy.RUNTIME)
  15. @Documented
  16. public @interface UnLockCell {
  17. }

 实例类:

  1. package com.example.juc.studyExcel.entity;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import com.alibaba.excel.annotation.write.style.ColumnWidth;
  4. import com.example.juc.zhujie.UnLockCell;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. /**
  9. * @Author x
  10. * @Date Created in 2023/12/6 8:31
  11. * @DESCRIPTION:
  12. * @Version V1.0
  13. */
  14. @Data
  15. @NoArgsConstructor
  16. @AllArgsConstructor
  17. public class Student {
  18. @ExcelProperty("用户id")
  19. @UnLockCell
  20. private String id;
  21. @ExcelProperty("姓名")
  22. private String name;
  23. @ExcelProperty("年龄")
  24. @ColumnWidth(5)
  25. private String age;
  26. }

controller层

  1. import com.alibaba.excel.EasyExcel;
  2. import com.alibaba.excel.ExcelWriter;
  3. import com.alibaba.excel.write.metadata.WriteSheet;
  4. import com.alibaba.excel.write.metadata.WriteWorkbook;
  5. import com.example.juc.studyExcel.entity.Student;
  6. import com.example.juc.test.handler.CellHandler;
  7. import com.example.juc.test.handler.LockSheetWriteHandler;
  8. import com.example.juc.test.handler.StyleWriteHandler;
  9. import org.apache.poi.ss.usermodel.Sheet;
  10. import org.apache.poi.ss.usermodel.Workbook;
  11. import org.springframework.web.bind.annotation.GetMapping;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.net.URLEncoder;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.stream.Collectors;
  20. import java.util.stream.Stream;
  21. /**
  22. * @Author
  23. * @Date Created in 2023/12/16 16:32
  24. * @DESCRIPTION:
  25. * @Version V1.0
  26. */
  27. @RestController
  28. @RequestMapping("testSuoding")
  29. public class 测试excel列锁定 {
  30. /**
  31. * 文件下载(失败了会返回一个有部分数据的Excel)
  32. * <p>
  33. * 1. 创建excel对应的实体对象 参照{@link }
  34. * <p>
  35. * 2. 设置返回的 参数
  36. * <p>
  37. * 3. 直接写,这里注意,finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大
  38. */
  39. @GetMapping("download")
  40. public void download(HttpServletResponse response) throws IOException {
  41. List<Student> list = new ArrayList<>();
  42. list.add(new Student("1","夏天","18"));
  43. list.add(new Student("2","夏天","18"));
  44. list.add(new Student("3","夏天","18"));
  45. list.add(new Student("4","夏天","18"));
  46. list.add(new Student("5","夏天","18"));
  47. // 设置响应头
  48. response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  49. response.setCharacterEncoding("utf-8");
  50. String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
  51. response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
  52. EasyExcel.write(response.getOutputStream(),Student.class)
  53. .registerWriteHandler(new LockSheetWriteHandler()) //锁整张sheet
  54. .registerWriteHandler(new CellHandler(1)) //根据自定义注解解锁某些特定的列
  55. .sheet("模板")
  56. .doWrite(list);
  57. }
  58. }
LockSheetWriteHandler.java
  1. package com.example.juc.test.handler;
  2. import com.alibaba.excel.write.handler.SheetWriteHandler;
  3. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
  4. import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
  5. import org.apache.poi.ss.usermodel.Cell;
  6. import org.apache.poi.ss.usermodel.CellStyle;
  7. import org.apache.poi.ss.usermodel.Row;
  8. import org.apache.poi.ss.usermodel.Sheet;
  9. import org.apache.poi.xssf.streaming.SXSSFSheet;
  10. import java.util.UUID;
  11. /**
  12. * @author
  13. * @since 2024/1/12 16:08
  14. */
  15. public class LockSheetWriteHandler implements SheetWriteHandler {
  16. @Override
  17. public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
  18. }
  19. @Override
  20. public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
  21. Sheet sheet = writeSheetHolder.getSheet();
  22. //锁定工作簿,设置保护密码
  23. String passWord = UUID.randomUUID().toString();
  24. sheet.protectSheet(passWord);
  25. // 锁定单元格不可选中(防止别人直接复制内容到其他excel修改)
  26. ((SXSSFSheet) writeSheetHolder.getSheet()).lockSelectLockedCells(true);
  27. }
  28. }
CellHandler.java
  1. package com.example.juc.test.handler;
  2. import com.alibaba.excel.annotation.ExcelProperty;
  3. import com.alibaba.excel.metadata.Head;
  4. import com.alibaba.excel.metadata.data.CellData;
  5. import com.alibaba.excel.metadata.data.WriteCellData;
  6. import com.alibaba.excel.write.handler.CellWriteHandler;
  7. import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
  8. import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
  9. import com.example.juc.zhujie.UnLockCell;
  10. import org.apache.poi.ss.usermodel.*;
  11. import org.apache.poi.ss.util.CellUtil;
  12. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  13. import java.lang.reflect.Field;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * @Author xlj
  19. * @Date Created in 2023/12/19 10:11
  20. * @DESCRIPTION:
  21. * @Version V1.0
  22. */
  23. public class CellHandler implements CellWriteHandler {
  24. private static final String PASSWORD = "1qaz!QAZ";
  25. private static final Integer A = 1;
  26. private static final Integer B = 0;
  27. private final Integer bigData;
  28. public CellHandler(int i) {
  29. this.bigData = i;
  30. }
  31. @Override
  32. public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
  33. }
  34. @Override
  35. public int order() {
  36. return Integer.MAX_VALUE;
  37. }
  38. @Override
  39. public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
  40. }
  41. @Override
  42. public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
  43. if (isHead) {
  44. return; // 不处理表头
  45. }
  46. String fieldName = head.getFieldName();
  47. Class<?> clazz = writeSheetHolder.getClazz();
  48. Field[] fields = clazz.getDeclaredFields();
  49. for (Field field : fields) {
  50. if (field.getName().equals(fieldName)) {
  51. if (field.isAnnotationPresent(UnLockCell.class) && field.isAnnotationPresent(ExcelProperty.class)) {
  52. Map<String, Object> properties = new HashMap<>(1);
  53. properties.put(CellUtil.LOCKED, false);
  54. CellUtil.setCellStyleProperties(cell, properties);
  55. Sheet sheet = writeSheetHolder.getSheet();
  56. if (bigData == 1) {
  57. sheet.setColumnHidden(A,true);
  58. } else {
  59. sheet.setColumnHidden(B,true);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }

注意:

pom依赖如下:

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi</artifactId>
  4. <version>4.1.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi-ooxml</artifactId>
  9. <version>4.1.2</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.apache.poi</groupId>
  13. <artifactId>poi-ooxml-schemas</artifactId>
  14. <version>4.1.2</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.alibaba</groupId>
  18. <artifactId>easyexcel</artifactId>
  19. <version>3.1.1</version>
  20. </dependency>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/170248
推荐阅读
相关标签
  

闽ICP备14008679号