栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot+vue+easyExcel 导出excel

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot+vue+easyExcel 导出excel

easyexcel版本


            com.alibaba
            easyexcel
            2.2.10
            
                
                    org.apache.poi
                    poi-ooxml
                
                
                    poi
                    org.apache.poi
                
            
        
        
            org.apache.poi
            poi-ooxml
            3.17
        
        
            org.apache.poi
            poi-ooxml-schemas
            3.17
        
        
            org.apache.poi
            poi
            3.17
        

POST方式

后台:

@RequestMapping("/excel")
public class TestController {
	@PostMapping(value = "/export")
    public void export(HttpServletResponse response) throws Exception {
	
        List 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 getUserList(){
		List userList = new ArrayList<>();
		User user = new User();
		user.setUserName("张三");
		user.setSex("男");
		user.setAge("10");
		userList.add(user);
		return userList;
	}
    
}

前端vue:


      数据导出
    
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 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 getUserList(){
		List 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博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490513.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号