zoukankan      html  css  js  c++  java
  • vue springboot 实现excel导出

    实现excel 导出

    一、需求

    实现 excel 的导出

    二、技术

      选用 easypoi 

    官网:

    https://gitee.com/lemur/easypoi#http://doc.wupaas.com/docs/easypoi/easypoi-1c0u4mo8p4ro8

    三、实现

      1、前端

    <el-button type="success" @click="handleExport">导出<i class="el-icon-upload el-icon--right"></i></el-button>
    handleExport() {
      let a = document.createElement('a')
      a.href = "/xxx/export"
      a.click()
    }         

      2、后端

      a、引入pom依赖

    <!-- 引入EasyPoi包 -->
    <dependency>
      <groupId>cn.afterturn</groupId>
      <artifactId>easypoi-spring-boot-starter</artifactId>
      <version>4.1.0</version>
    </dependency>
    
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.4</version>
    </dependency>

      b、实体注解

    @Data
    public class UnitMaterialRecommendVo {
        @Excel(name = "序号", width = 10)
        private Integer num;
        @Excel(name = "体系", width = 25, orderNum = "1")
        private String unitSystem;
        @Excel(name = "单位", width = 25, orderNum = "2")
        private String unitName;
        @Excel(name = "参评类型", width = 25, orderNum = "3", replace = { "参评单位申报推荐_10", "文明单位复查审核_20" })
        private Integer reportType;
        @Excel(name = "审核结果", width = 25, orderNum = "4", replace = { "审核中_0", "审核通过_1", "审核不通过_-1" })
        private Integer state;
        @Excel(name = "联系方式", width = 25, orderNum = "5")
        private String unitDirectorPhone;
        @Excel(name = "所属省市", width = 25, orderNum = "6")
        private String unitCityName;
        @Excel(name = "文明单位类型", width = 25, orderNum = "7", replace = { "四川省文明单位_10", "四川省最佳文明单位_20"})
        private Integer civilizedUnitType;
        @Excel(name = "备注", width = 30, orderNum = "8")
        private String remark;
    }

      c、导出方法

    List<UnitMaterialRecommend> list = this.list(wrapper);
    String fileName = new String("文件名称.xls".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
    response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
    response.setContentType("application/vnd.ms-excel;charset=gb2312");
    List<UnitMaterialRecommendVo> result = new ArrayList<>();
    AtomicInteger num = new AtomicInteger(1);
    // 设置序号
    list.forEach(item -> item.setNum(num.getAndIncrement()));
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), UnitMaterialRecommendVo.class, result);
    workbook.write(response.getOutputStream());

     三、备注

      通过模板批量导出,采用  ExcelExportUtil.exportExcelClone 方法,详见下面示例:

      private static final String SHEET_NAME = "sheetName";
        @Override
        public void multipleExport(HttpServletResponse response, UnitMaterialRecommend recommend) throws IOException {
    
            String fileName = new String("template.xls".getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=gb2312");
            TemplateExportParams params = new TemplateExportParams("template/materialTemplate.xls");
    
            List<UnitMaterialRecommend> models = this.getModels(recommend);
            List<Map<String, Object>> listMap = new ArrayList<>();
            for (int j = models.size() - 1; j >= 0; j--) {
                Map<String, Object> modelMap = this.getModelMap(models.get(j));
                modelMap.put(SHEET_NAME, "sheet" + j);
                listMap.add(modelMap);
            }
            Map<Integer, List<Map<String, Object>>> realMap = new HashMap<>();
            realMap.put(0, listMap);
            Workbook workbook = ExcelExportUtil.exportExcelClone(realMap, params);
            workbook.write(response.getOutputStream());
        }

    ~~~以上

    让每一天过的有意义!
  • 相关阅读:
    按钮一色三变化
    最小的k个数-Python版
    字符串的全排列-终于弄明白了
    键值offer-求逆序对
    Python小技巧
    面试题48-动态规划-最长不含重复字符的子字符串,剪绳子
    面试题47-礼物的最大价值(Python版)
    多元高斯分布
    torch_13_ProGan笔记
    pytorch-模型保存与加载自己训练的模型详解 
  • 原文地址:https://www.cnblogs.com/BillyYoung/p/14386308.html
Copyright © 2011-2022 走看看