Vue+Springboot,通过EasyExcel工具实现Excel导出

1.前端Vue

(注意:调用后端接口时需指定{responseType:'blob'},否则导出的excel表格打不开)

//Vue导出方法 
exportExcel(){
            //searchParam:为前端需传到后端的所需参数
            var searchParam =  {
                test:this.data.test,


                };
           //调用后端接口,传入参数searchParam
           //注意:调用后端接口时需指定{responseType:'blob'},否则导出的excel表格打不开
            request.post("/exceltest/exportExcel",searchParam,{responseType:'blob'}).then((res) => {
                const aLink = document.createElement('a')
                //为Blob指定导出的文件类型,此处为xlsx[{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}]
                var blob = new Blob([res.data], {type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
                // //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
                var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
                var contentDisposition = decodeURI(res.headers['content-disposition'])
                var result = patt.exec(contentDisposition)
                var fileName = result[1]
                fileName=fileName = fileName.replace(/\"/g, '')
                aLink.href = URL.createObjectURL(blob)
                aLink.setAttribute('download', fileName) // 设置下载文件名称
                document.body.appendChild(aLink)
                aLink.click()
                document.body.appendChild(aLink)
            })
        },

2.后端Spring boot

2.1Rest层

@RestController
@RequestMapping("/exceltest")
public class ExcelExportRest {

    @Autowired
    private ExcelExportDao excelExportDao;


    
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response,@RequestBody HashMap queryParam) {
        try {
            //定义文件名字
            String filename = URLEncoder.encode(System.currentTimeMillis() + ".xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            //设定输出文件头
            response.setHeader("Content-disposition", "attachment; filename=" + filename);
            // 定义输出类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            //从数据库查询需写入excel的结果集[queryParam前端所传参数]
            //注意返回类型需指定为定义的实体:SupportingDetailsDto
            List<SupportingDetailsDto> hashMaps = excelExportDao.querySendMaterial(queryParam);
             //将实体以流的方式写入
            EasyExcel.write(response.getOutputStream(), SupportingDetailsDto.class)
                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                    //指定sheet名字
                    .autoCloseStream(Boolean.FALSE).sheet("配套明细")
                     //写入数据库返回的参数到excel
                    .doWrite(hashMaps);
        

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
        }
    }

}

2.2Dto层

@Data
@EqualsAndHashCode
public class SupportingDetailsDto {

    //数据类型需和数据库返回类型一致
    @ExcelProperty("test字段名称")
    private String test;

   

3.pom.xml

       <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/poi/poi -->
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

3.excel导出效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用EasyExcel导出数据前端需要进行的操作包括: 1. 下载模板:首先,需要提供一个可以下载的模板文件,用户可以根据该模板文件进行数据的填写。用户可以点击相应的按钮或链接,触发模板文件的下载。 2. 编写Excel数据文件:用户根据下载的模板文件,将需要导出的数据填写在相应的单元格中,确保数据的准确性和完整性。 3. 触发导出数据:用户在完成数据的填写后,可以点击导出数据的按钮,触发导出数据的操作。通常使用axios库发送请求到后端进行数据的处理和导出。 4. 后端接收数据:后端接收到前端发送的导出请求后,根据请求参数获取前端填写的数据,进行相应的数据处理操作。 5. 数据持久化:后端对数据进行处理后,可以进行数据的持久化,如保存到数据库中或生成Excel文件并返回给前端。 6. 前端接收数据:前端接收到后端返回的数据流,可以通过相应的处理操作将数据展示给用户,如下载生成的Excel文件或在页面展示数据等。 总结起来,前端在使用EasyExcel导出数据时需要下载模板、填写数据、触发导出、接收后端返回的数据流并进行相应处理操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [EasyExcel快速实现前后端导入导出以及相关注意事项](https://blog.csdn.net/whitehats/article/details/111671444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [vue中后端做Excel导出功能返回数据流前端的处理操作](https://download.csdn.net/download/weixin_38623819/12923483)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值