Java uses Easyexcel to achieve excel export, with a front-end download method

backend code

add dependencies

First install dependencies

<!--添加easyExcel-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.1</version>
</dependency>

Set excel table header mapping file

Then add a VO class to declare the header fields in excel

package com.szx.edu.entity.vo;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

@Data
@ExcelIgnoreUnannotated
public class ExportEventVo {
    
    
    @ExcelProperty(value = "订单号")
    String orderid;

    @ExcelProperty(value = "客户名称")
    String userName;

    @ExcelProperty(value = "客户电话")
    String userPhone;
}

add controller

write controller

@GetMapping("excelTable")
@ApiOperation("导出数据")
public void exportTable(HttpServletResponse response) throws IOException {
    
    
    //1、设定响应类型
    response.setContentType("application/vnd.ms-excel");
    //2、设定附件的打开方法为:下载,并指定文件名称为category.xlsx
    response.setHeader("content-disposition","attachment;filename=eventlist.xlsx");
    //3、创建工作簿
    ExcelWriterBuilder writeWork = EasyExcel.write(response.getOutputStream(), ExportEventVo.class);;
    //4、创建表格
    ExcelWriterSheetBuilder sheet = writeWork.sheet();
    //5、创建数据
    ArrayList<ExportEventVo> eventVos = new ArrayList<>();
    //6、调用业务层获取list数据
    List<EventList> allEvent = eventListService.getAllEvent();
    //7、遍历list集和往excel中添加数据
    for (EventList eventList : allEvent) {
    
    
        ExportEventVo vo = new ExportEventVo();
        // 订单号
        vo.setOrderid(eventList.getOrderid());
        // 客户名称
        vo.setUserName(eventList.getUserName());
        // 客户电话
        vo.setUserPhone(eventList.getUserPhone());
        // 添加
        eventVos.add(vo);
    }

    //8、写入数据到表格中
    sheet.doWrite(eventVos);
}

front-end code

This method is a get request. If the browser directly requests this address, it will automatically trigger the download. The front end can also receive the interface return through the stream, and then process the file download. The method is as follows

Interceptor processing returns

First add the following judgment to the request interceptor

8558467.jpg

add request api

Add interface api request

// 导出事件
export function exportEventFun() {
    
    
  return request({
    
    
    method: 'get',
    url: `/edu/event-list/excelTable`,
    responseType: 'blob', // 使用blob下载
  })
}

call implementation

Click the button to call the interface to trigger the download

// 点击导出按钮触发导出操作
exportEvent() {
    
    
  exportEventFun().then((res) => {
    
    
    const objectUrl = URL.createObjectURL(
      new Blob([res.data], {
    
    
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      })
    )
    const link = document.createElement('a')
    // 设置导出的文件名称
    link.download = `案件列表` + '.xlsx'
    link.style.display = 'none'
    link.href = objectUrl
    link.click()
    document.body.appendChild(link)
  })
},

Guess you like

Origin blog.csdn.net/SongZhengxing_/article/details/128186404
Recommended