response Content-Type 设置(根据后缀)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用POI操作Excel文件时,可以通过设置Content-Type来指定生成的Excel文件的类型。具体步骤如下: 1. 导入相关包 ```java import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; ``` 2. 在Controller中设置Content-Type ```java @RequestMapping("/downloadExcel") public void downloadExcel(HttpServletResponse response) { try { //创建Workbook对象 Workbook workbook = new XSSFWorkbook(); //Workbook workbook = new HSSFWorkbook(); //设置Content-Type response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //response.setContentType("application/vnd.ms-excel"); //设置文件response.setHeader("Content-Disposition", "attachment;filename=test.xlsx"); //response.setHeader("Content-Disposition", "attachment;filename=test.xls"); //将Workbook写入OutputStream OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } ``` 其中,Content-Type分别为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet和application/vnd.ms-excel,分别对应xlsx和xls格式的Excel文件。 需要注意的是,在使用XSSFWorkbook时,Content-Type应该设置为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;在使用HSSFWorkbook时,Content-Type应该设置为application/vnd.ms-excel。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值