zoukankan      html  css  js  c++  java
  • 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);
    }

  • 相关阅读:
    Next.js文档自定义App、Document,getInitialProps翻译
    使用fetch进行数据请求时报json错误
    菜单制作:ul li横向排列
    Django Auth组件->扩展用户
    001.Django_Model.整理
    PyCharm中的django项目的引入
    Css3 里的弹性盒的比例关系
    vue的组件通讯 父传子 -- 子传父-- 兄弟组件的传值 vue的组件传值
    新手如何创建一个vue项目 ---vue---新手创建第一个项目
    如何自学计算机前端开发?精细的自学步骤是什么样的?
  • 原文地址:https://www.cnblogs.com/guangxiang/p/11384289.html
Copyright © 2011-2022 走看看