zoukankan      html  css  js  c++  java
  • windows 导出excel不会报错 ,部署到Liunx 导出excel报错

      注意:excel模板需要放在resources下面   

     

         

           // 读取文件路径要写成这个样子在liunx才不会报错

    ClassPathResource classPathResource = new ClassPathResource("static/sexcelTelemp.xls");
              InputStream   inputStream1 = classPathResource.getInputStream();
            //导出列表名
    String fileName = "aa.xls";

    //文件名称统一编码格式
    fileName = URLEncoder.encode(fileName, "utf-8");
    //生成的导出文件
    File destFile = File.createTempFile(fileName, ".xls");

    //transformer转到Excel
    XLSTransformer transformer = new XLSTransformer();
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
    // 传入流文件和数据
    Workbook workbook = transformer.transformXLS(inputStream1, beans);
    OutputStream os = new BufferedOutputStream(new FileOutputStream(destFile.getAbsoluteFile()));
    workbook.write(os);
    inputStream1.close();
    os.flush();
    os.close();
    //将文件输入
    InputStream inputStream = new FileInputStream(destFile);
    // 设置response参数,可以打开下载页面
    response.reset();
    //设置响应文本格式
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    response.setHeader("Content-Disposition",
    "attachment;filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
    //将文件输出到页面
    ServletOutputStream out = response.getOutputStream();
    bis = new BufferedInputStream(inputStream);
    bos = new BufferedOutputStream(out);
    byte[] buff = new byte[2048];
    int bytesRead;
    // 根据读取并写入
    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
    bos.write(buff, 0, bytesRead);
    }
    } catch (ParsePropertyException | InvalidFormatException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    //使用完成后关闭流
    try {
    if (bis != null)
    bis.close();
    if (bos != null)
    bos.close();
    } catch (IOException e) {
    }

    }
  • 相关阅读:
    10gen发布MongoDB增量备份服务
    JSON.NET 5中的架构变更
    Snowbox 2.0 发布,POP3 邮件服务器
    资源监控工具 glances
    Druid 0.2.18 发布,阿里巴巴数据库连接池
    Groovy 更新到 2.0.8 and 2.1.3
    Apache Libcloud 0.12.4 发布,统一云计算接口
    Go1.1性能测试报告(和C差距在10%以内)
    Apache Camel 2.11.0 发布,规则引擎
    2010年01月01日0时0分 总结我的2009
  • 原文地址:https://www.cnblogs.com/bt2882/p/11599975.html
Copyright © 2011-2022 走看看