vue+springboot 导出Excel表(已解决乱码)

看过很多以前端vue,后端以springboot为框架导出Excel表,要么导出失败,要么导出Excel乱码,还有些解决有乱码的文章还需要收费,最后还是要自己探索一番才能解决!

废话不多说,直接上代码!

前端vue代码:

 exportExcel() {
                axios({
                    method: 'POST',
                    url: '/order/exportExcel',
                    params: {
                        //order: "",
                    },
                    data: this.orderList,
                    responseType: 'blob' //解决乱码问题
                }).then(data => {

                    if (!data) {
                        this.$message.error('下载内容为空');
                        return
                    }
                    let url = window.URL.createObjectURL(new Blob([data], {type: 'application/xls'}));
                    let link = document.createElement('a');
                    link.style.display = 'none';
                    link.href = url;
                   

                    link.setAttribute('download', '订单列表.xls');
                    document.body.appendChild(link);
                    link.click();
                    //释放URL对象所占资源
                    window.URL.revokeObjectURL(url);
                    //用完即删
                    document.body.removeChild(link);

                }).catch(error => {
                    this.$message.error(error);

                })

            }

后端需要的依赖:

<!--导出 Excel 表-->
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>4.1.1</version>
</dependency>

后端controller层代码:

@PostMapping("/exportExcel")
    public void exportExcel(@RequestBody List<Order> orders, HttpServletRequest request, HttpServletResponse response) {//@RequestBody List<Order> orders,
        POIUtils.exportExcel(orders, request, response);
    }

POIUtils 中 exportExcel()方法如下:

 public static void exportExcel(List<Order> list, HttpServletRequest request, HttpServletResponse response) {
        //1. 创建一个 Excel 文档
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2. 创建文档摘要
        workbook.createInformationProperties();
        //3. 获取并配置文档信息
        DocumentSummaryInformation docInfo = workbook.getDocumentSummaryInformation();
        //文档类别
        docInfo.setCategory("订单信息");
        //文档管理员
        docInfo.setManager("xgm");
        //设置公司信息
        docInfo.setCompany("www.xxx.com");
        //4. 获取文档摘要信息
        SummaryInformation summInfo = workbook.getSummaryInformation();
        //文档标题
        summInfo.setTitle("订单信息表");
        //文档作者
        summInfo.setAuthor("xgm");
        // 文档备注
        summInfo.setComments("本文档由 xgm 提供");
        //5. 创建样式
        //创建标题行的样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFCellStyle dateCellStyle = workbook.createCellStyle();
        dateCellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
        HSSFSheet sheet = workbook.createSheet("订单信息表");
        //设置列的宽度
        sheet.setColumnWidth(0, 5 * 256);
        //逻辑省略些。。。。。,需要多少列,自己后面续上呗
        

        //6. 创建标题行
        HSSFRow r0 = sheet.createRow(0);
        HSSFCell c0 = r0.createCell(0);
        c0.setCellValue("订单号");
        c0.setCellStyle(headerStyle);
        //逻辑省略些。。。。。

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (int i = 0; i < list.size(); i++) {
            Order order = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(order.getOrderId());
            //逻辑省略些。。。。。
            
 
        }

        try {
            FileOutputStream out = new FileOutputStream(new                 
            File("C:\\token\\ITouch\\excel\\订单表.xls"));
            workbook.write(out);

            response.reset(); // 非常重要

            response.setHeader("Content-disposition", "attachment;fileName=" + "订单表" + 
            ".xls");
            response.setContentType("application/octet-stream;charset=utf-8");
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            out.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

}

 

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值