zoukankan      html  css  js  c++  java
  • java通过HSSFWorkbook导出xls文件

    使用swgger2、Restlet等接口工具有bug导致导出失败,测试直接使用浏览器
    //导出列表-新
    @UserRoleJudgment(authpos = SystemControllerLog.AUTH_PARAM, paraname = "auth")
    @SystemControllerLog(module = "管理", description = "导出列表",authpos = SystemControllerLog.AUTH_PARAM, paraname = "auth")
    @RequestMapping(value = "/exportContractExcelN", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出列表", httpMethod = "GET", notes = "导出列表,需要授权")
    public ReponseResult<Boolean> exportContractExcelN(
    @ApiParam(value = "编号,参数形式7564cb720a0000151bcbee13209899a0,75522b6b0a000015160873e6fa5dec29", required = true) @RequestParam String[] ids,
    HttpServletRequest request,
    HttpServletResponse response) throws IOException {
    if (ids == null || ids.length == 0||auth==null) {
    return ReponseResult.error(CodeMsg.PARAMETER_ISNULL);
    }
    List<String> idsn = java.util.Arrays.asList(ids);
    List<ContractModel> list = null;
    try {
    list = contractService.exportDataList1(idsn);
    } catch (Exception e) {
    e.printStackTrace();
    }
    if(list==null||list.size()==0){
    return ReponseResult.error(new CodeMsg(-1, "列表为空!"));
    }
    // 导出表格
    HSSFWorkbook wb = contractService.exportBatch1(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);
    }

  • 相关阅读:
    Windows性能调优: Perfomn.exe 和Perfmon /res
    WPF:逻辑树和视觉树
    AD FS 概述
    SQL Server : TRUSTWORTHY 数据库属性
    WCF:在开发期间应该注意的问题
    ASP.NET MVC 2中的数据验证
    SQL Server:如何在Service Broker发送消息验证失败后获取源消息
    GDI+:自定义控件时如何使用Region来输出特定区域
    LINQ to XML:如何替换XCData的内容
    javascript是否真的就不能实现跨站请求呢?
  • 原文地址:https://www.cnblogs.com/guangxiang/p/11429237.html
Copyright © 2011-2022 走看看