hutool工具包实现的导出实体到excel功能

说明

在此工具类中使用了hutool工具包,具体依赖介绍参考官方https://www.hutool.cn/docs/#/poi/概述
以及 lombok,在实体属性中需加入@ApiModelProperty注解,代码中也有说明,有不明白的或者更好的方法欢迎留言。

配套前端代码地址:前端接收下载后端文件流

/**
 * @author: yang
 * @Date: Create in 2020/7/16
 * @Explain: 封装hutool工具类实现的导出excel功能
 */
public class ExportExcelUtil {
  static final Log logger = LogFactory.get(ExportExcelUtil.class);
  public static void
  export(HttpServletResponse response, Class clazz, List <? > list) throws Exception {
    if(!list.isEmpty()) {
      if(!list.get(0).getClass().equals(clazz)) {
        logger.error("数据类型与传入的集合数据类型不一致!数据类型:{}; 集合数据类型:{}", clazz, list.get(0).getClass());
        throw new Exception("数据类型与传入的集合数据类型不一致!");
      } else {
        ExcelWriter writer = ExcelUtil.getWriter();
        // 获取当前类字段
        Field[] fields = clazz.getDeclaredFields();
        // 字段名称集合
        List < String > fieldNames = new ArrayList < > ();
        // 字段中文名称集合(获取实体中@ApiModelProperty注解value的值)
        List < String > cnNames = new ArrayList < > ();
        for(Field field: fields) {
          if(!field.isAccessible()) {
            // 关闭反射访问安全检查,为了提高速度
            field.setAccessible(true);
          }
          String fieldName = field.getName();
          // 排除ID和序号
          if(!"sid".equals(fieldName) && !"serialVersionUID".equals(fieldName) && !"ordernum".equals(fieldName)) {
            fieldNames.add(fieldName);
          }
          // 判断是否有@ApiModelProperty注解
          boolean annotationPresent = field.isAnnotationPresent(ApiModelProperty.class);
          if(annotationPresent && !"sid".equals(fieldName)) {
            ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
            String name = annotation.value();
            cnNames.add(name);
          }
        }
        String[] fs = fieldNames.toArray(new String[0]);
        String[] ns = cnNames.toArray(new String[0]);
        for(int i = 0; i < ns.length; i++) {
          // 设置表头及字段名
          writer.addHeaderAlias(fs[i], ns[i]);
        }
        // 自动换行
        Workbook workbook = writer.getWorkbook();
        StyleSet styleSet = new StyleSet(workbook);
        styleSet.setWrapText();
        writer.setStyleSet(styleSet);
        writer.write(list, true);
        ServletOutputStream out = response.getOutputStream();;
        try {
          for(int i = 0; i < fieldNames.size(); i++) {
            writer.setColumnWidth(i, 23);
          }
          response.setContentType("application/x-msdownload;charset=utf-8");
          String ecodeFileName = URLEncoder.encode("excel", "UTF-8");
          response.setHeader("Content-Disposition", "attachment;filename=" + ecodeFileName + ".xls");
          writer.flush(out, true);
          writer.close();
          IoUtil.close(out);
        } catch(IOException e) {
          logger.error(e.getMessage());
          e.printStackTrace();
        }
      }
    } else {
      logger.error("数据集合为空");
      throw new Exception("数据集合为空");
    }
  }
}
posted @ 2020-07-18 17:31  Gyyyang  阅读(5845)  评论(0编辑  收藏  举报