• java使用HSSFWorkbook下载Excel表格



    @RequestMapping(value = "/exportVectorExcelN", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出载体列表", httpMethod = "GET", notes = "导出载体列表,需要授权")
    public ReponseResult<Boolean> exportExcel(
    @ApiParam(value = "原文编号,参数形式7564cb720a0000151bcbee13209899a0,75522b6b0a000015160873e6fa5dec29", required = true) @RequestParam String[] ids,
    @ApiParam(value = "token", required = true) @RequestParam String auth,
    HttpServletRequest request,
    HttpServletResponse response) throws IOException {
    if (ids == null || ids.length == 0) {
    return ReponseResult.error(CodeMsg.PARAMETER_ISNULL);
    }

    List<String> idsn = java.util.Arrays.asList(ids);
    List<VectorBriefModel> list = null;
    try {
    list = vectorService.exportDataList(idsn);
    } catch (Exception e) {
    e.printStackTrace();
    }
    if(list==null||list.size()==0){
    return ReponseResult.error(new CodeMsg(-1, "列表为空!"));
    }
    // 导出表格
    HSSFWorkbook wb = vectorService.exportBatch(list);
    OutputStream os = null;
    try {

    // 创建一个普通输出流
    os = response.getOutputStream();
    String fileName = "file.xls";
    // 请求浏览器打开下载窗口
    response.reset();
    response.setCharacterEncoding("UTF-8");
    // Content-disposition 告诉浏览器以下载的形式打开
    String header = request.getHeader("User-Agent").toUpperCase();
    if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
    fileName = URLEncoder.encode(fileName, "utf-8");
    fileName = fileName.replace("+", "%20"); // IE下载文件名空格变+号问题
    } else {
    fileName = new String(fileName.getBytes(), "ISO8859-1");
    }
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);// 要保存的文件名
    response.setContentType("application/vnd.ms-excel");
    // 直接用数组缓冲输出流输出
    wb.write(os);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return ReponseResult.success(true);
    }

  • 相关阅读:
    Linux压缩和解压类指令
    Linux 搜索查找类指令
    Linux时间日期类指令
    Linux文件目录类指令
    Linux帮助指令
    Linux运行级别
    微信授权获取code/openid
    微信公众配置
    MySQL规范
    PHP7搭建项目遇到的坑
  • 原文地址:https://www.cnblogs.com/guangxiang/p/11384289.html
Copyright © 2020-2023  润新知