springboot 导出execl 出现Could not find acceptable representation with root cause 的解决

原来的代码是:

response.setContentType("application/vnd.ms-excel;charset=gb2312");

解决方法一: 注掉此行代码

解决方法二:把上面的代码换成

response.setContentType("application/json;charset=gb2312");

ok了。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
1.添加依赖 在pom.xml文件中添加以下依赖: ``` <!-- Excel导出依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version> </dependency> ``` 2.编写控制器 在控制器中编写导出Excel的方法: ``` @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { // 1.创建工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); // 2.创建工作表 XSSFSheet sheet = workbook.createSheet("用户信息"); // 3.创建行 XSSFRow row = sheet.createRow(0); // 4.创建单元格 XSSFCell cell = row.createCell(0); cell.setCellValue("用户名"); cell = row.createCell(1); cell.setCellValue("年龄"); cell = row.createCell(2); cell.setCellValue("性别"); // 5.写入数据 List<User> userList = userService.getUserList(); for (int i = 0; i < userList.size(); i++) { row = sheet.createRow(i + 1); User user = userList.get(i); row.createCell(0).setCellValue(user.getName()); row.createCell(1).setCellValue(user.getAge()); row.createCell(2).setCellValue(user.getGender()); } // 6.设置响应头 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=userInfo.xlsx"); // 7.输出Excel OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } ``` 3.测试 启动SpringBoot应用,访问导出Excel的接口,即可下载Excel文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值