下载Excel

先上代码:

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

posted @ 2022-11-13 20:08  天门道人  阅读(64)  评论(0编辑  收藏  举报