首页 > 其他分享 >下载Excel

下载Excel

时间:2022-11-13 20:25:10浏览次数:34  
标签:Excel inputStream file new data response 下载

先上代码:

File file = new File("C:\\Users\\71028\\Desktop\\excel.xlsx");
  if (file.exists()) {
    byte[] data = null;
    try {
        FileInputStream inputStream = new FileInputStream(file);
        data = new byte[inputStream.available()];
        inputStream.read(data);
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.getOutputStream().write(data);
    } catch (Exception e) {

    }
}

注意response要设置contentType

标签:Excel,inputStream,file,new,data,response,下载
From: https://www.cnblogs.com/zhangyibing/p/16886783.html

相关文章