zoukankan      html  css  js  c++  java
  • java导出excel 浏览器直接下载或者或以文件形式导出

     1 /**
     2  * excel表格直接下载
     3  */
     4 public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse httpServletResponse,String fileName) throws Exception {
     5     //响应类型为application/octet- stream情况下使用了这个头信息的话,那就意味着不想直接显示内容
     6     httpServletResponse.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
     7 
     8     //attachment为以附件方式下载
     9     httpServletResponse.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(
    10             fileName + ".xls",
    11             "utf-8"));
    12     /**
    13      * 代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。
    14      * response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作
    15      */
    16     httpServletResponse.setHeader("Cache-Control", "No-cache");
    17     httpServletResponse.flushBuffer();
    18 
    19     wb.write(httpServletResponse.getOutputStream());
    20     wb.close();
    21 }
    22 
    23 /**
    24  * excel以文件的形式导出
    25  * @throws Exception
    26  */
    27 public static void exportExcelByFile(HSSFWorkbook wb,String fileName,String path) throws Exception{
    28 
    29     ByteArrayOutputStream stream = new ByteArrayOutputStream();
    30     wb.write(stream);
    31     FileOutputStream outputStream = new FileOutputStream(path + fileName);
    32     outputStream.write(stream.toByteArray());
    33     stream.close();
    34     outputStream.close();
    35 
    36 }
  • 相关阅读:
    c++ --> #define中的三个特殊符号:#,##,#@
    网络通信 --> ZMQ安装和使用
    利用 mount 指令解决 Read-only file system的问题
    sed学习总结
    Verilog中锁存器与多路选择器
    Debian耳机声音问题
    MM32/STM32中断和事件梳理
    MM32 备份域学习(兼容STM32)
    有限状态机FSM(自动售报机Verilog实现)
    MM32 RTC学习(兼容STM32)
  • 原文地址:https://www.cnblogs.com/wang-yaz/p/10785647.html
Copyright © 2011-2022 走看看