若依框架生成多个sheet的Excel方法

打印 上一主题 下一主题

主题 1370|帖子 1370|积分 4110

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
步骤:
1,创建一个Excel的sheet的实体类

  
  1. package com.XXX.common.utils.poi;
  2. import java.util.List;
  3. public class ExcelExp {
  4.     private String fileName;// sheet的名称
  5.     private String[] handers;// sheet里的标题
  6.     private List dataset;// sheet里的数据集
  7.     private Class clazz;
  8.     public ExcelExp(String fileName, List dataset, Class clazz) {
  9.         this.fileName = fileName;
  10.         this.dataset = dataset;
  11.         this.clazz = clazz;
  12.     }
  13.     public String getFileName() {
  14.         return fileName;
  15.     }
  16.     public void setFileName(String fileName) {
  17.         this.fileName = fileName;
  18.     }
  19.     public String[] getHanders() {
  20.         return handers;
  21.     }
  22.     public void setHanders(String[] handers) {
  23.         this.handers = handers;
  24.     }
  25.     public List getDataset() {
  26.         return dataset;
  27.     }
  28.     public void setDataset(List dataset) {
  29.         this.dataset = dataset;
  30.     }
  31.     public Class getClazz() {
  32.         return clazz;
  33.     }
  34.     public void setClazz(Class clazz) {
  35.         this.clazz = clazz;
  36.     }
  37. }
复制代码
2,创建生成多个sheet的工具类

  1. package com.XXX.common.utils.poi;
  2. import com.XXX.common.annotation.Excel;
  3. import com.XXX.common.utils.poi.ExcelExp;
  4. import com.XXX.common.annotation.Excel.ColumnType;
  5. import com.XXX.common.annotation.Excel.Type;
  6. import com.XXX.common.annotation.Excels;
  7. import com.XXX.common.core.text.Convert;
  8. import com.XXX.common.utils.DateUtils;
  9. import com.XXX.common.utils.StringUtils;
  10. import com.XXX.common.utils.file.FileTypeUtils;
  11. import com.XXX.common.utils.file.ImageUtils;
  12. import com.XXX.common.utils.poi.ExcelUtil;
  13. import com.XXX.common.utils.reflect.ReflectUtils;
  14. import org.apache.poi.ss.usermodel.*;
  15. import org.apache.poi.ss.util.CellRangeAddressList;
  16. import org.apache.poi.xssf.streaming.SXSSFWorkbook;
  17. import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  18. import org.apache.poi.xssf.usermodel.XSSFDataValidation;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import javax.servlet.http.HttpServletResponse;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.lang.reflect.Field;
  26. import java.math.BigDecimal;
  27. import java.text.DecimalFormat;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. /**
  31. * Excel相关处理
  32. *
  33. * @author ruoyi
  34. */
  35. public class ExcelUtilManySheet<T> {
  36.     private static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
  37.     /**
  38.      * Excel sheet最大行数,默认65536
  39.      */
  40.     public static final int sheetSize = 65536;
  41.     /**
  42.      * 工作表名称
  43.      */
  44.     private String sheetName;
  45.     /**
  46.      * 导出类型(EXPORT:导出数据;IMPORT:导入模板)
  47.      */
  48.     private Type type;
  49.     /**
  50.      * 工作薄对象
  51.      */
  52.     private Workbook wb;
  53.     /**
  54.      * 工作表对象
  55.      */
  56.     private Sheet sheet;
  57.     /**
  58.      * 样式列表
  59.      */
  60.     private Map<String, CellStyle> styles;
  61.     /**
  62.      * 导入导出数据列表
  63.      */
  64.     private List<T> list;
  65.     /**
  66.      * 注解列表
  67.      */
  68.     private List<Object[]> fields;
  69.     /**
  70.      * 最大高度
  71.      */
  72.     private short maxHeight;
  73.     /**
  74.      * 统计列表
  75.      */
  76.     private Map<Integer, Double> statistics = new HashMap<Integer, Double>();
  77.     /**
  78.      * 数字格式
  79.      */
  80.     private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00");
  81.     /**
  82.      * 实体对象
  83.      */
  84.     public Class<T> clazz;
  85.     public List<ExcelExp> clazzlist;
  86.     public ExcelUtilManySheet(List<ExcelExp> clazzlist) {
  87.         this.clazzlist = clazzlist;
  88.     }
  89.     public void init(List<T> list, String sheetName, Type type) {
  90.         if (list == null) {
  91.             list = new ArrayList<T>();
  92.         }
  93.         this.list = list;
  94.         this.sheetName = sheetName;
  95.         this.type = type;
  96.         createExcelField();
  97.     }
  98.     /**
  99.      * 对excel表单默认第一个索引名转换成list
  100.      *
  101.      * @param is 输入流
  102.      * @return 转换后集合
  103.      */
  104.     public List<T> importExcel(InputStream is) throws Exception {
  105.         return importExcel(StringUtils.EMPTY, is);
  106.     }
  107.     /**
  108.      * 对excel表单指定表格索引名转换成list
  109.      *
  110.      * @param sheetName 表格索引名
  111.      * @param is        输入流
  112.      * @return 转换后集合
  113.      */
  114.     public List<T> importExcel(String sheetName, InputStream is) throws Exception {
  115.         this.type = Type.IMPORT;
  116.         this.wb = WorkbookFactory.create(is);
  117.         List<T> list = new ArrayList<T>();
  118.         Sheet sheet = null;
  119.         if (StringUtils.isNotEmpty(sheetName)) {
  120.             // 如果指定sheet名,则取指定sheet中的内容.
  121.             sheet = wb.getSheet(sheetName);
  122.         } else {
  123.             // 如果传入的sheet名不存在则默认指向第1个sheet.
  124.             sheet = wb.getSheetAt(0);
  125.         }
  126.         if (sheet == null) {
  127.             throw new IOException("文件sheet不存在");
  128.         }
  129.         int rows = sheet.getPhysicalNumberOfRows();
  130.         if (rows > 0) {
  131.             // 定义一个map用于存放excel列的序号和field.
  132.             Map<String, Integer> cellMap = new HashMap<String, Integer>();
  133.             // 获取表头
  134.             Row heard = sheet.getRow(0);
  135.             for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
  136.                 Cell cell = heard.getCell(i);
  137.                 if (StringUtils.isNotNull(cell)) {
  138.                     String value = this.getCellValue(heard, i).toString();
  139.                     cellMap.put(value, i);
  140.                 } else {
  141.                     cellMap.put(null, i);
  142.                 }
  143.             }
  144.             // 有数据时才处理 得到类的所有field.
  145.             Field[] allFields = clazz.getDeclaredFields();
  146.             // 定义一个map用于存放列的序号和field.
  147.             Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
  148.             for (int col = 0; col < allFields.length; col++) {
  149.                 Field field = allFields[col];
  150.                 Excel attr = field.getAnnotation(Excel.class);
  151.                 if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
  152.                     // 设置类的私有字段属性可访问.
  153.                     field.setAccessible(true);
  154.                     Integer column = cellMap.get(attr.name());
  155.                     if (column != null) {
  156.                         fieldsMap.put(column, field);
  157.                     }
  158.                 }
  159.             }
  160.             for (int i = 1; i < rows; i++) {
  161.                 // 从第2行开始取数据,默认第一行是表头.
  162.                 Row row = sheet.getRow(i);
  163.                 T entity = null;
  164.                 for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet()) {
  165.                     Object val = this.getCellValue(row, entry.getKey());
  166.                     // 如果不存在实例则新建.
  167.                     entity = (entity == null ? clazz.newInstance() : entity);
  168.                     // 从map中得到对应列的field.
  169.                     Field field = fieldsMap.get(entry.getKey());
  170.                     // 取得类型,并根据对象类型设置值.
  171.                     Class<?> fieldType = field.getType();
  172.                     if (String.class == fieldType) {
  173.                         String s = Convert.toStr(val);
  174.                         if (StringUtils.endsWith(s, ".0")) {
  175.                             val = StringUtils.substringBefore(s, ".0");
  176.                         } else {
  177.                             String dateFormat = field.getAnnotation(Excel.class).dateFormat();
  178.                             if (StringUtils.isNotEmpty(dateFormat)) {
  179.                                 val = DateUtils.parseDateToStr(dateFormat, (Date) val);
  180.                             } else {
  181.                                 val = Convert.toStr(val);
  182.                             }
  183.                         }
  184.                     } else if ((Integer.TYPE == fieldType || Integer.class == fieldType) && StringUtils.isNumeric(Convert.toStr(val))) {
  185.                         val = Convert.toInt(val);
  186.                     } else if (Long.TYPE == fieldType || Long.class == fieldType) {
  187.                         val = Convert.toLong(val);
  188.                     } else if (Double.TYPE == fieldType || Double.class == fieldType) {
  189.                         val = Convert.toDouble(val);
  190.                     } else if (Float.TYPE == fieldType || Float.class == fieldType) {
  191.                         val = Convert.toFloat(val);
  192.                     } else if (BigDecimal.class == fieldType) {
  193.                         val = Convert.toBigDecimal(val);
  194.                     } else if (Date.class == fieldType) {
  195.                         if (val instanceof String) {
  196.                             val = DateUtils.parseDate(val);
  197.                         } else if (val instanceof Double) {
  198.                             val = DateUtil.getJavaDate((Double) val);
  199.                         }
  200.                     } else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
  201.                         val = Convert.toBool(val, false);
  202.                     }
  203.                     if (StringUtils.isNotNull(fieldType)) {
  204.                         Excel attr = field.getAnnotation(Excel.class);
  205.                         String propertyName = field.getName();
  206.                         if (StringUtils.isNotEmpty(attr.targetAttr())) {
  207.                             propertyName = field.getName() + "." + attr.targetAttr();
  208.                         } else if (StringUtils.isNotEmpty(attr.readConverterExp())) {
  209.                             val = reverseByExp(Convert.toStr(val), attr.readConverterExp(), attr.separator());
  210.                         }
  211.                         ReflectUtils.invokeSetter(entity, propertyName, val);
  212.                     }
  213.                 }
  214.                 list.add(entity);
  215.             }
  216.         }
  217.         return list;
  218.     }
  219.     /**
  220.      * 对list数据源将其里面的数据导入到excel表单
  221.      *
  222.      * @param response  返回数据
  223.      * @param list      导出数据集合
  224.      * @param sheetName 工作表的名称
  225.      * @return 结果
  226.      * @throws IOException
  227.      */
  228.     public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException {
  229.         response.setContentType("application/vnd.ms-excel");
  230.         response.setCharacterEncoding("utf-8");
  231.         this.init(list, sheetName, Type.EXPORT);
  232.         exportExcel(response.getOutputStream());
  233.     }
  234.     /**
  235.      * 对list数据源将其里面的数据导入到excel表单
  236.      *
  237.      * @param response 返回数据
  238.      * @param list     导出数据集合
  239.      * @return 结果
  240.      * @throws IOException
  241.      */
  242.     public void exportExcelManySheet(HttpServletResponse response, List<ExcelExp> list) throws IOException {
  243.         response.setContentType("application/vnd.ms-excel");
  244.         response.setCharacterEncoding("utf-8");
  245.         try {
  246.             createWorkbook();
  247.             for (int index = 0; index <list.size(); index++) {
  248.                 this.clazz = list.get(index).getClazz();
  249.                 this.init(list.get(index).getDataset(), list.get(index).getFileName(), Excel.Type.EXPORT);
  250.                 // 取出一共有多少个sheet.
  251. //                    double sheetNo = Math.ceil(list.size() / sheetSize);
  252.                 createSheetManySheet(index);
  253.                 // 产生一行
  254.                 Row row = sheet.createRow(0);
  255.                 int column = 0;
  256.                 // 写入各个字段的列头名称
  257.                 for (Object[] os : fields) {
  258.                     Excel excel = (Excel) os[1];
  259.                     this.createCell(excel, row, column++);
  260.                 }
  261.                 if (Type.EXPORT.equals(type)) {
  262.                     fillExcelData(index, row);
  263.                     addStatisticsRow();
  264.                 }
  265.             }
  266.             wb.write(response.getOutputStream());
  267.         } catch (IOException e) {
  268.             log.error("导出Excel异常{}", e.getMessage());
  269.         } finally {
  270.             if (wb != null) {
  271.                 try {
  272.                     wb.close();
  273.                 } catch (IOException e1) {
  274.                     e1.printStackTrace();
  275.                 }
  276.             }
  277.             if (response.getOutputStream() != null) {
  278.                 try {
  279.                     response.getOutputStream().close();
  280.                 } catch (IOException e1) {
  281.                     e1.printStackTrace();
  282.                 }
  283.             }
  284.         }
  285. //        exportExcel(response.getOutputStream());
  286.     }
  287.     /**
  288.      * 对list数据源将其里面的数据导入到excel表单
  289.      *
  290.      * @param sheetName 工作表的名称
  291.      * @return 结果
  292.      */
  293.     public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException {
  294.         response.setContentType("application/vnd.ms-excel");
  295.         response.setCharacterEncoding("utf-8");
  296.         this.init(null, sheetName, Type.IMPORT);
  297.         exportExcel(response.getOutputStream());
  298.     }
  299.     /**
  300.      * 对list数据源将其里面的数据导入到excel表单
  301.      *
  302.      * @return 结果
  303.      */
  304.     public void exportExcel(OutputStream outputStream) {
  305.         try {
  306.             // 取出一共有多少个sheet.
  307.             double sheetNo = Math.ceil(list.size() / sheetSize);
  308.             for (int index = 0; index <= sheetNo; index++) {
  309.                 createSheet(sheetNo, index);
  310.                 // 产生一行
  311.                 Row row = sheet.createRow(0);
  312.                 int column = 0;
  313.                 // 写入各个字段的列头名称
  314.                 for (Object[] os : fields) {
  315.                     Excel excel = (Excel) os[1];
  316.                     this.createCell(excel, row, column++);
  317.                 }
  318.                 if (Type.EXPORT.equals(type)) {
  319.                     fillExcelData(index, row);
  320.                     addStatisticsRow();
  321.                 }
  322.             }
  323.             wb.write(outputStream);
  324.         } catch (Exception e) {
  325.             log.error("导出Excel异常{}", e.getMessage());
  326.         } finally {
  327.             if (wb != null) {
  328.                 try {
  329.                     wb.close();
  330.                 } catch (IOException e1) {
  331.                     e1.printStackTrace();
  332.                 }
  333.             }
  334.             if (outputStream != null) {
  335.                 try {
  336.                     outputStream.close();
  337.                 } catch (IOException e1) {
  338.                     e1.printStackTrace();
  339.                 }
  340.             }
  341.         }
  342.     }
  343.     /**
  344.      * 填充excel数据
  345.      *
  346.      * @param index 序号
  347.      * @param row   单元格行
  348.      */
  349.     public void fillExcelData(int index, Row row) {
  350. //        int startNo = index * sheetSize;
  351. //        int endNo = Math.min(startNo + sheetSize, list.size());
  352.         for (int i = 0; i < list.size(); i++) {
  353.             row = sheet.createRow(i + 1 );
  354.             // 得到导出对象.
  355.             T vo = (T) list.get(i);
  356.             int column = 0;
  357.             for (Object[] os : fields) {
  358.                 Field field = (Field) os[0];
  359.                 Excel excel = (Excel) os[1];
  360.                 // 设置实体类私有属性可访问
  361.                 field.setAccessible(true);
  362.                 this.addCell(excel, row, vo, field, column++);
  363.             }
  364.         }
  365.     }
  366.     /**
  367.      * 创建表格样式
  368.      *
  369.      * @param wb 工作薄对象
  370.      * @return 样式列表
  371.      */
  372.     private Map<String, CellStyle> createStyles(Workbook wb) {
  373.         // 写入各条记录,每条记录对应excel表中的一行
  374.         Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
  375.         CellStyle style = wb.createCellStyle();
  376.         style.setAlignment(HorizontalAlignment.CENTER);
  377.         style.setVerticalAlignment(VerticalAlignment.CENTER);
  378.         style.setBorderRight(BorderStyle.THIN);
  379.         style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  380.         style.setBorderLeft(BorderStyle.THIN);
  381.         style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  382.         style.setBorderTop(BorderStyle.THIN);
  383.         style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  384.         style.setBorderBottom(BorderStyle.THIN);
  385.         style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
  386.         Font dataFont = wb.createFont();
  387.         dataFont.setFontName("Arial");
  388.         dataFont.setFontHeightInPoints((short) 10);
  389.         style.setFont(dataFont);
  390.         styles.put("data", style);
  391.         style = wb.createCellStyle();
  392.         style.cloneStyleFrom(styles.get("data"));
  393.         style.setAlignment(HorizontalAlignment.CENTER);
  394.         style.setVerticalAlignment(VerticalAlignment.CENTER);
  395.         style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
  396.         style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  397.         Font headerFont = wb.createFont();
  398.         headerFont.setFontName("Arial");
  399.         headerFont.setFontHeightInPoints((short) 10);
  400.         headerFont.setBold(true);
  401.         headerFont.setColor(IndexedColors.WHITE.getIndex());
  402.         style.setFont(headerFont);
  403.         styles.put("header", style);
  404.         style = wb.createCellStyle();
  405.         style.setAlignment(HorizontalAlignment.CENTER);
  406.         style.setVerticalAlignment(VerticalAlignment.CENTER);
  407.         Font totalFont = wb.createFont();
  408.         totalFont.setFontName("Arial");
  409.         totalFont.setFontHeightInPoints((short) 10);
  410.         style.setFont(totalFont);
  411.         styles.put("total", style);
  412.         style = wb.createCellStyle();
  413.         style.cloneStyleFrom(styles.get("data"));
  414.         style.setAlignment(HorizontalAlignment.LEFT);
  415.         styles.put("data1", style);
  416.         style = wb.createCellStyle();
  417.         style.cloneStyleFrom(styles.get("data"));
  418.         style.setAlignment(HorizontalAlignment.CENTER);
  419.         styles.put("data2", style);
  420.         style = wb.createCellStyle();
  421.         style.cloneStyleFrom(styles.get("data"));
  422.         style.setAlignment(HorizontalAlignment.RIGHT);
  423.         styles.put("data3", style);
  424.         return styles;
  425.     }
  426.     /**
  427.      * 创建单元格
  428.      */
  429.     public Cell createCell(Excel attr, Row row, int column) {
  430.         // 创建列
  431.         Cell cell = row.createCell(column);
  432.         // 写入列信息
  433.         cell.setCellValue(attr.name());
  434.         setDataValidation(attr, row, column);
  435.         cell.setCellStyle(styles.get("header"));
  436.         return cell;
  437.     }
  438.     /**
  439.      * 设置单元格信息
  440.      *
  441.      * @param value 单元格值
  442.      * @param attr  注解相关
  443.      * @param cell  单元格信息
  444.      */
  445.     public void setCellVo(Object value, Excel attr, Cell cell) {
  446.         if (Excel.ColumnType.STRING == attr.cellType()) {
  447.             cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
  448.         } else if (Excel.ColumnType.NUMERIC == attr.cellType()) {
  449.             cell.setCellValue(StringUtils.contains(Convert.toStr(value), ".") ? Convert.toDouble(value) : Convert.toInt(value));
  450.         } else if (Excel.ColumnType.IMAGE == attr.cellType()) {
  451.             ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
  452.                     cell.getRow().getRowNum() + 1);
  453.             String imagePath = Convert.toStr(value);
  454.             if (StringUtils.isNotEmpty(imagePath)) {
  455.                 byte[] data = ImageUtils.getImage(imagePath);
  456.                 getDrawingPatriarch(cell.getSheet()).createPicture(anchor,
  457.                         cell.getSheet().getWorkbook().addPicture(data, getImageType(data)));
  458.             }
  459.         }
  460.     }
  461.     /**
  462.      * 获取画布
  463.      */
  464.     public static Drawing<?> getDrawingPatriarch(Sheet sheet) {
  465.         if (sheet.getDrawingPatriarch() == null) {
  466.             sheet.createDrawingPatriarch();
  467.         }
  468.         return sheet.getDrawingPatriarch();
  469.     }
  470.     /**
  471.      * 获取图片类型,设置图片插入类型
  472.      */
  473.     public int getImageType(byte[] value) {
  474.         String type = FileTypeUtils.getFileExtendName(value);
  475.         if ("JPG".equalsIgnoreCase(type)) {
  476.             return Workbook.PICTURE_TYPE_JPEG;
  477.         } else if ("PNG".equalsIgnoreCase(type)) {
  478.             return Workbook.PICTURE_TYPE_PNG;
  479.         }
  480.         return Workbook.PICTURE_TYPE_JPEG;
  481.     }
  482.     /**
  483.      * 创建表格样式
  484.      */
  485.     public void setDataValidation(Excel attr, Row row, int column) {
  486.         if (attr.name().indexOf("注:") >= 0) {
  487.             sheet.setColumnWidth(column, 6000);
  488.         } else {
  489.             // 设置列宽
  490.             sheet.setColumnWidth(column, (int) ((attr.width() + 0.72) * 256));
  491.         }
  492.         // 如果设置了提示信息则鼠标放上去提示.
  493.         if (StringUtils.isNotEmpty(attr.prompt())) {
  494.             // 这里默认设了2-101列提示.
  495.             setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, column, column);
  496.         }
  497.         // 如果设置了combo属性则本列只能选择不能输入
  498.         if (attr.combo().length > 0) {
  499.             // 这里默认设了2-101列只能选择不能输入.
  500.             setXSSFValidation(sheet, attr.combo(), 1, 100, column, column);
  501.         }
  502.     }
  503.     /**
  504.      * 添加单元格
  505.      */
  506.     public Cell addCell(Excel attr, Row row, T vo, Field field, int column) {
  507.         Cell cell = null;
  508.         try {
  509.             // 设置行高
  510.             row.setHeight(maxHeight);
  511.             // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
  512.             if (attr.isExport()) {
  513.                 // 创建cell
  514.                 cell = row.createCell(column);
  515.                 int align = attr.align().getCode();
  516.                 cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
  517.                 // 用于读取对象中的属性
  518.                 Object value = getTargetValue(vo, field, attr);
  519.                 String dateFormat = attr.dateFormat();
  520.                 String readConverterExp = attr.readConverterExp();
  521.                 String separator = attr.separator();
  522.                 if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) {
  523.                     cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
  524.                 } else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) {
  525.                     cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
  526.                 } else if (value instanceof BigDecimal && -1 != attr.scale()) {
  527.                     cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
  528.                 } else {
  529.                     // 设置列类型
  530.                     setCellVo(value, attr, cell);
  531.                 }
  532.                 addStatisticsData(column, Convert.toStr(value), attr);
  533.             }
  534.         } catch (Exception e) {
  535.             log.error("导出Excel失败{}", e);
  536.         }
  537.         return cell;
  538.     }
  539.     /**
  540.      * 设置 POI XSSFSheet 单元格提示
  541.      *
  542.      * @param sheet         表单
  543.      * @param promptTitle   提示标题
  544.      * @param promptContent 提示内容
  545.      * @param firstRow      开始行
  546.      * @param endRow        结束行
  547.      * @param firstCol      开始列
  548.      * @param endCol        结束列
  549.      */
  550.     public void setXSSFPrompt(Sheet sheet, String promptTitle, String promptContent, int firstRow, int endRow,
  551.                               int firstCol, int endCol) {
  552.         DataValidationHelper helper = sheet.getDataValidationHelper();
  553.         DataValidationConstraint constraint = helper.createCustomConstraint("DD1");
  554.         CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  555.         DataValidation dataValidation = helper.createValidation(constraint, regions);
  556.         dataValidation.createPromptBox(promptTitle, promptContent);
  557.         dataValidation.setShowPromptBox(true);
  558.         sheet.addValidationData(dataValidation);
  559.     }
  560.     /**
  561.      * 设置某些列的值只能输入预制的数据,显示下拉框.
  562.      *
  563.      * @param sheet    要设置的sheet.
  564.      * @param textlist 下拉框显示的内容
  565.      * @param firstRow 开始行
  566.      * @param endRow   结束行
  567.      * @param firstCol 开始列
  568.      * @param endCol   结束列
  569.      * @return 设置好的sheet.
  570.      */
  571.     public void setXSSFValidation(Sheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol) {
  572.         DataValidationHelper helper = sheet.getDataValidationHelper();
  573.         // 加载下拉列表内容
  574.         DataValidationConstraint constraint = helper.createExplicitListConstraint(textlist);
  575.         // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
  576.         CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
  577.         // 数据有效性对象
  578.         DataValidation dataValidation = helper.createValidation(constraint, regions);
  579.         // 处理Excel兼容性问题
  580.         if (dataValidation instanceof XSSFDataValidation) {
  581.             dataValidation.setSuppressDropDownArrow(true);
  582.             dataValidation.setShowErrorBox(true);
  583.         } else {
  584.             dataValidation.setSuppressDropDownArrow(false);
  585.         }
  586.         sheet.addValidationData(dataValidation);
  587.     }
  588.     /**
  589.      * 解析导出值 0=男,1=女,2=未知
  590.      *
  591.      * @param propertyValue 参数值
  592.      * @param converterExp  翻译注解
  593.      * @param separator     分隔符
  594.      * @return 解析后值
  595.      */
  596.     public static String convertByExp(String propertyValue, String converterExp, String separator) {
  597.         StringBuilder propertyString = new StringBuilder();
  598.         String[] convertSource = converterExp.split(",");
  599.         for (String item : convertSource) {
  600.             String[] itemArray = item.split("=");
  601.             if (StringUtils.containsAny(separator, propertyValue)) {
  602.                 for (String value : propertyValue.split(separator)) {
  603.                     if (itemArray[0].equals(value)) {
  604.                         propertyString.append(itemArray[1] + separator);
  605.                         break;
  606.                     }
  607.                 }
  608.             } else {
  609.                 if (itemArray[0].equals(propertyValue)) {
  610.                     return itemArray[1];
  611.                 }
  612.             }
  613.         }
  614.         return StringUtils.stripEnd(propertyString.toString(), separator);
  615.     }
  616.     /**
  617.      * 反向解析值 男=0,女=1,未知=2
  618.      *
  619.      * @param propertyValue 参数值
  620.      * @param converterExp  翻译注解
  621.      * @param separator     分隔符
  622.      * @return 解析后值
  623.      */
  624.     public static String reverseByExp(String propertyValue, String converterExp, String separator) {
  625.         StringBuilder propertyString = new StringBuilder();
  626.         String[] convertSource = converterExp.split(",");
  627.         for (String item : convertSource) {
  628.             String[] itemArray = item.split("=");
  629.             if (StringUtils.containsAny(separator, propertyValue)) {
  630.                 for (String value : propertyValue.split(separator)) {
  631.                     if (itemArray[1].equals(value)) {
  632.                         propertyString.append(itemArray[0] + separator);
  633.                         break;
  634.                     }
  635.                 }
  636.             } else {
  637.                 if (itemArray[1].equals(propertyValue)) {
  638.                     return itemArray[0];
  639.                 }
  640.             }
  641.         }
  642.         return StringUtils.stripEnd(propertyString.toString(), separator);
  643.     }
  644.     /**
  645.      * 合计统计信息
  646.      */
  647.     private void addStatisticsData(Integer index, String text, Excel entity) {
  648.         if (entity != null && entity.isStatistics()) {
  649.             Double temp = 0D;
  650.             if (!statistics.containsKey(index)) {
  651.                 statistics.put(index, temp);
  652.             }
  653.             try {
  654.                 temp = Double.valueOf(text);
  655.             } catch (NumberFormatException e) {
  656.             }
  657.             statistics.put(index, statistics.get(index) + temp);
  658.         }
  659.     }
  660.     /**
  661.      * 创建统计行
  662.      */
  663.     public void addStatisticsRow() {
  664.         if (statistics.size() > 0) {
  665.             Cell cell = null;
  666.             Row row = sheet.createRow(sheet.getLastRowNum() + 1);
  667.             Set<Integer> keys = statistics.keySet();
  668.             cell = row.createCell(0);
  669.             cell.setCellStyle(styles.get("total"));
  670.             cell.setCellValue("合计");
  671.             for (Integer key : keys) {
  672.                 cell = row.createCell(key);
  673.                 cell.setCellStyle(styles.get("total"));
  674.                 cell.setCellValue(DOUBLE_FORMAT.format(statistics.get(key)));
  675.             }
  676.             statistics.clear();
  677.         }
  678.     }
  679.     /**
  680.      * 获取bean中的属性值
  681.      *
  682.      * @param vo    实体对象
  683.      * @param field 字段
  684.      * @param excel 注解
  685.      * @return 最终的属性值
  686.      * @throws Exception
  687.      */
  688.     private Object getTargetValue(T vo, Field field, Excel excel) throws Exception {
  689.         Object o = field.get(vo);
  690.         if (StringUtils.isNotEmpty(excel.targetAttr())) {
  691.             String target = excel.targetAttr();
  692.             if (target.indexOf(".") > -1) {
  693.                 String[] targets = target.split("[.]");
  694.                 for (String name : targets) {
  695.                     o = getValue(o, name);
  696.                 }
  697.             } else {
  698.                 o = getValue(o, target);
  699.             }
  700.         }
  701.         return o;
  702.     }
  703.     /**
  704.      * 以类的属性的get方法方法形式获取值
  705.      *
  706.      * @param o
  707.      * @param name
  708.      * @return value
  709.      * @throws Exception
  710.      */
  711.     private Object getValue(Object o, String name) throws Exception {
  712.         if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name)) {
  713.             Class<?> clazz = o.getClass();
  714.             Field field = clazz.getDeclaredField(name);
  715.             field.setAccessible(true);
  716.             o = field.get(o);
  717.         }
  718.         return o;
  719.     }
  720.     /**
  721.      * 得到所有定义字段
  722.      */
  723.     private void createExcelField() {
  724.         this.fields = new ArrayList<Object[]>();
  725.         List<Field> tempFields = new ArrayList<>();
  726.         tempFields.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
  727.         tempFields.addAll(Arrays.asList(clazz.getDeclaredFields()));
  728.         for (Field field : tempFields) {
  729.             // 单注解
  730.             if (field.isAnnotationPresent(Excel.class)) {
  731.                 putToField(field, field.getAnnotation(Excel.class));
  732.             }
  733.             // 多注解
  734.             if (field.isAnnotationPresent(Excels.class)) {
  735.                 Excels attrs = field.getAnnotation(Excels.class);
  736.                 Excel[] excels = attrs.value();
  737.                 for (Excel excel : excels) {
  738.                     putToField(field, excel);
  739.                 }
  740.             }
  741.         }
  742.         this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
  743.         this.maxHeight = getRowHeight();
  744.     }
  745.     /**
  746.      * 根据注解获取最大行高
  747.      */
  748.     public short getRowHeight() {
  749.         double maxHeight = 0;
  750.         for (Object[] os : this.fields) {
  751.             Excel excel = (Excel) os[1];
  752.             maxHeight = maxHeight > excel.height() ? maxHeight : excel.height();
  753.         }
  754.         return (short) (maxHeight * 20);
  755.     }
  756.     /**
  757.      * 放到字段集合中
  758.      */
  759.     private void putToField(Field field, Excel attr) {
  760.         if (attr != null && (attr.type() == Type.ALL || attr.type() == type)) {
  761.             this.fields.add(new Object[]{field, attr});
  762.         }
  763.     }
  764.     /**
  765.      * 创建一个工作簿
  766.      */
  767.     public void createWorkbook() {
  768.         this.wb = new SXSSFWorkbook(500);
  769.     }
  770.     /**
  771.      * 创建工作表
  772.      *
  773.      * @param sheetNo sheet数量
  774.      * @param index   序号
  775.      */
  776.     public void createSheet(double sheetNo, int index) {
  777.         this.sheet = wb.createSheet();
  778.         this.styles = createStyles(wb);
  779.         // 设置工作表的名称.
  780.         if (sheetNo == 0) {
  781.             wb.setSheetName(index, sheetName);
  782.         } else {
  783.             wb.setSheetName(index, sheetName + index);
  784.         }
  785.     }
  786.     /**
  787.      * 创建工作表
  788.      *
  789.      * @param index   序号
  790.      */
  791.     public void createSheetManySheet( int index) {
  792.         this.sheet = wb.createSheet();
  793.         this.styles = createStyles(wb);
  794.         wb.setSheetName(index, sheetName);
  795.     }
  796.     /**
  797.      * 获取单元格值
  798.      *
  799.      * @param row    获取的行
  800.      * @param column 获取单元格列号
  801.      * @return 单元格值
  802.      */
  803.     public Object getCellValue(Row row, int column) {
  804.         if (row == null) {
  805.             return row;
  806.         }
  807.         Object val = "";
  808.         try {
  809.             Cell cell = row.getCell(column);
  810.             if (StringUtils.isNotNull(cell)) {
  811.                 if (cell.getCellType() == CellType.NUMERIC || cell.getCellType() == CellType.FORMULA) {
  812.                     val = cell.getNumericCellValue();
  813.                     if (DateUtil.isCellDateFormatted(cell)) {
  814.                         val = DateUtil.getJavaDate((Double) val); // POI Excel 日期格式转换
  815.                     } else {
  816.                         if ((Double) val % 1 != 0) {
  817.                             val = new BigDecimal(val.toString());
  818.                         } else {
  819.                             val = new DecimalFormat("0").format(val);
  820.                         }
  821.                     }
  822.                 } else if (cell.getCellType() == CellType.STRING) {
  823.                     val = cell.getStringCellValue();
  824.                 } else if (cell.getCellType() == CellType.BOOLEAN) {
  825.                     val = cell.getBooleanCellValue();
  826.                 } else if (cell.getCellType() == CellType.ERROR) {
  827.                     val = cell.getErrorCellValue();
  828.                 }
  829.             }
  830.         } catch (Exception e) {
  831.             return val;
  832.         }
  833.         return val;
  834.     }
  835. }
复制代码
使用方式

  1.     @PreAuthorize("@ss.hasPermi('product:data:export')")
  2.     @Log(title = "标题", businessType = BusinessType.EXPORT)
  3.     @PostMapping("/export")
  4.     public void export(HttpServletResponse response, Data  data) {
  5.         // 查询列表
  6.         List<ProductData> list1 = Service.selectDataList(data);
  7.         List<ProductData> list2 = Service.selectDataList(data);
  8.         // 创建Excel工作表
  9.         ExcelExp e1 = new ExcelExp("sheet1", list1, Data.class);
  10.         ExcelExp e2 = new ExcelExp("sheet2", list2, Data.class);
  11.         List<ExcelExp> sheet = new ArrayList<>();
  12.         sheet.add(e1);
  13.         sheet.add(e2);
  14.         ExcelUtilManySheet<List<ExcelExp>> utilManySheet = new ExcelUtilManySheet<>(sheet);
  15.         try {
  16.             utilManySheet.exportExcelManySheet(response, sheet);
  17.         } catch (IOException e) {
  18.             e.printStackTrace();
  19.         }
  20.     }
复制代码
这样就可以使用若依框架,创建多个sheet的Excel文件

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表