当前位置:   article > 正文

前端传json,后端转成exceil并在前端下载_下载execl,前端使用json传参

下载execl,前端使用json传参

前端:

1.post的方法里要加responseType: 'arraybuffer’参数,不然下载的excel会乱码

2.使用{type: “application/vnd.ms-excel”}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx

3.返回结果为下载excel文档链接,使用window.open(result)即可

4.使用增加节点调用click方法,而不使用window.open(objectUrl)方法,是防止被浏览器当插件屏蔽弹出连接

5.给文件设定名字,直接在a标签的download属性中设置即可

    resultexport () {
      this.$http
        .post(
          '/votehoutai/export/resultexport',
          this.CEtableData,
          {
            headers: { 'content-type': 'application/json' },
            //
            responseType: 'arraybuffer' }
        ).then((res) => {
          console.log(res)
          var blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })
          var objectUrl = URL.createObjectURL(blob)
          var a = document.createElement('a')
          document.body.appendChild(a)
          a.style = 'display: none'
          a.href = objectUrl
          a.click()
          document.body.removeChild(a)
        })
    },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

后端:

Controller:(ResultVo 是前端显示的Vo对象)

    @PostMapping("resultexport")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void resultexport(@RequestBody List<ResultVo> obj, HttpServletResponse response) throws Exception {
    	
    	ExcelUtils.exportExcelToTarget(response, null, obj, ResultExcel.class);
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Excelutils:

public class ExcelUtils {

    /**
     * Excel导出
     *
     * @param response      response
     * @param fileName      文件名
     * @param list          数据List
     * @param pojoClass     对象Class
     */
    public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list,
                                     Class<?> pojoClass) throws IOException {
        if(StringUtils.isBlank(fileName)){
            //当前日期
            fileName = DateUtils.format(new Date());
        }

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, list);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
    }

    /**
     * Excel导出,先sourceList转换成List<targetClass>,再导出
     *
     * @param response      response
     * @param fileName      文件名
     * @param sourceList    原数据List
     * @param targetClass   目标对象Class
     */
    public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList,
                                     Class<?> targetClass) throws Exception {
        List targetList = new ArrayList<>(sourceList.size());
        for(Object source : sourceList){
            Object target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
            targetList.add(target);
        }
        
        exportExcel(response, fileName, targetList, targetClass);
        
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

ReesultExcel:

public class ResultExcel {
	@Excel(name="得票数")
	private int sum;//得到的票数和
	@Excel(name="排名")
	private int rank;//排名
	@Excel(name="姓名")
	private String name;
	@Excel(name="院系")
	private String faculty;//院系
	@Excel(name="职位")
	private String position;//职位
	@Excel(name="竞争职位")
	private String wantposition;//竞争的职位
	@Excel(name="推荐类型")
	private int recommendtype;//推荐类型

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

ResultVo:

public class ResultVo extends BaseEntity {
	private int cuserid;
	private int sum;//得到的票数和
	private int rank;//排名
	private String name;
	private String faculty;//院系
	private String position;//职位
	private String wantposition;//竞争的职位
	private int recommendtype;//推荐类型
	private Boolean group;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/770349
推荐阅读
相关标签
  

闽ICP备14008679号