springboot+vue+easyExcel 导出excel

easyexcel版本

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.10</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>poi</artifactId>
                    <groupId>org.apache.poi</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

POST方式

后台:

@RequestMapping("/excel")
public class TestController {
	@PostMapping(value = "/export")
    public void export(HttpServletResponse response) throws Exception {
	
        List<User> userList = getUserList();
		
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
        response.setCharacterEncoding("utf-8");

        try{
            EasyExcel.write(response.getOutputStream(), BaseProductClassDto.class)
                    .sheet("sheet1")
                    .doWrite(baseProductClassList);
        }catch (Exception e) {
            e.printStackTrace();
        }
		
    }
	
	private List<User> getUserList(){
		List<User> userList = new ArrayList<>();
		User user = new User();
		user.setUserName("张三");
		user.setSex("男");
		user.setAge("10");
		userList.add(user);
		return userList;
	}
    
}

前端vue:

<sd-button-group>
      <a-button
        type="primary"
        v-action:export
        @click="exportData"
      >数据导出</a-button>
    </sd-button-group>
exportData() {
        // const headers = { 'Content-Type': 'application/octet-stream' }
        axios.post('/excel/export',{},
          { responseType: 'blob' }).then((res) => {
          const link = document.createElement('a')
          const blob = new Blob([res], { type: 'application/vnd.ms-excel'})
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          link.download = '用户列表.xlsx'
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        })
     }

导出结果:

GET方式

后台:

@RequestMapping("/excel")
public class TestController {
	@GetMapping(value = "/export")
    public void export(HttpServletResponse response) throws Exception {
	
        List<User> userList = getUserList();
		
		response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + System.currentTimeMillis() + ".xlsx");
        response.setCharacterEncoding("utf-8");

        try{
            EasyExcel.write(response.getOutputStream(), BaseProductClassDto.class)
                    .sheet("sheet1")
                    .doWrite(baseProductClassList);
        }catch (Exception e) {
            e.printStackTrace();
        }
		
    }
	
	private List<User> getUserList(){
		List<User> userList = new ArrayList<>();
		User user = new User();
		user.setUserName("张三");
		user.setSex("男");
		user.setAge("10");
		userList.add(user);
		return userList;
	}
    
}

前端vue:

axios({url: '/excel/export',method: 'get',responseType: 'blob'})
     .then((res) => {
          const link = document.createElement('a')
          const blob = new Blob([res], { type: 'application/vnd.ms-excel'})
          link.style.display = 'none'
        link.href = URL.createObjectURL(blob)
        link.download = '用户列表.xlsx'
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
      })

导出结果:

参考资料:

vue+axios 与spring boot EasyExcel实现后台导出excel并下载_feng2036的博客-CSDN博客

springboot+vue+easyexcel导出_Desire-C的博客-CSDN博客

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值