在线实时生成excel文件下载

本文介绍了一种在线实时生成Excel文件并供客户端下载的方法,避免了在服务器上预先生成文件的性能和空间问题。通过Apache POI库,实现了在服务器端创建文件输出流,并通过HttpServletResponse设置响应头,实现在不生成实际文件的情况下完成下载。在WebWork框架下,通过定义接口和基类,实现了Excel模型的创建和下载操作。
摘要由CSDN通过智能技术生成

在线实时生成excel文件下载

我正在的一个项目,需要实现在线实时生成 Excel文件供客户端下载的需求,最初考虑的是先在服务器端生成真实的文件,然后在客户端下载该文件。后来发现这样做不但性能不够好、速度较慢,而且还 要占用服务器空间。所以找到网上的例子采取了在服务器端生成文件输出流(ServletOutputStream),通过HttpServletResponse对象设 置相应的响应头,然后将此输出流传往客户端的方法实现。在实现过程中,用到了Apache组织的Jakarta开源组件POI,不过因为是用到webwork框架,而需求是大画面的其中一个按钮,目前只能考虑jsp来写画面,出现了输出流和jsp页面打开流出现冲突抛出异常。暂时的解决方法是return null,使它只是警告而不抛出异常。

一、首先,根据Excel表的特点,我编写了一个Excel表模型类ExcelModel,代码如下:

java 代码
 
  1. public class ExcelModel {  
  2.   
  3.     /** 
  4.      * 文件路径,这里是包含文件名的路径 
  5.      */  
  6.     protected String path;  
  7.   
  8.     /** 
  9.      * 工作表名 
  10.      */  
  11.     protected String sheetName;  
  12.   
  13.     /** 
  14.      * 表内数据,保存在二维的ArrayList对象中 
  15.      */  
  16.     protected ArrayList data;  
  17.   
  18.     /** 
  19.      * 数据表的标题内容 
  20.      */  
  21.     protected ArrayList header;  
  22.   
  23.     /** 
  24.      * 用于设置列宽的整型数组 这个方法在程序中暂未用到 适用于固定列数的表格 
  25.      */  
  26.     protected int[] width;  
  27.   
  28.     public ExcelModel() {  
  29.         path = "report.xls";  
  30.     }  
  31.   
  32.     /** 
  33.      * @return the data 
  34.      */  
  35.     public ArrayList getData() {  
  36.         return data;  
  37.     }  
  38.   
  39.     /** 
  40.      * @param data the data to set 
  41.      */  
  42.     public void setData(ArrayList data) {  
  43.         this.data = data;  
  44.     }  
  45.   
  46.     /** 
  47.      * @return the header 
  48.      */  
  49.     public ArrayList getHeader() {  
  50.         return header;  
  51.     }  
  52.   
  53.     /** 
  54.      * @param header the header to set 
  55.      */  
  56.     public void setHeader(ArrayList header) {  
  57.         this.header = header;  
  58.     }  
  59.   
  60.     /** 
  61.      * @return the path 
  62.      */  
  63.     public String getPath() {  
  64.         return path;  
  65.     }  
  66.   
  67.     /** 
  68.      * @param path the path to set 
  69.      */  
  70.     public void setPath(String path) {  
  71.         this.path = path;  
  72.     }  
  73.   
  74.     /** 
  75.      * @return the sheetName 
  76.      */  
  77.     public String getSheetName() {  
  78.         return sheetName;  
  79.     }  
  80.   
  81.     /** 
  82.      * @param sheetName the sheetName to set 
  83.      */  
  84.     public void setSheetName(String sheetName) {  
  85.         this.sheetName = sheetName;  
  86.     }  
  87.   
  88.     /** 
  89.      * @return the width 
  90.      */  
  91.     public int[] getWidth() {  
  92.         return width;  
  93.     }  
  94.   
  95.     /** 
  96.      * @param width the width to set 
  97.      */  
  98.     public void setWidth(int[] width) {  
  99.         this.width = width;  
  100.     }  
  101.   
  102. }  

二、编写一个下载接口ExcelDownLoad,定义基本的方法:

java 代码
 
  1. import java.io.IOException;  
  2. import java.util.List;  
  3.   
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6. /** 
  7.  * 初始化要生成的Excel的表模型 
  8.  *  
  9.  * @param list List 填充了 Excel表格数据的集合 
  10.  * @param form ActionForm及其子类 
  11.  * @param excel ExcelModel Excel表的对象模型 
  12.  * @see ExcelModel 
  13.  * @throws Exception 
  14.  */  
  15. public interface ExcelDownLoad {  
  16.   
  17.     /** 
  18.      * 在已文件已存在的情况下,采用读取文件流的方式实现左键点击下载功能 
  19.      *  
  20.      * @param inPutFileName 读出的文件名 
  21.      * @param outPutFileName 保存的文件名 
  22.      * @param HttpServletResponse 
  23.      * @see HttpServletResponse 
  24.      * @throws IOException 
  25.      */  
  26.     public void downLoad(String inPutFileName, String outPutFileName, HttpServletResponse response) throws IOException;  
  27.   
  28.     /** 
  29.      * 初始化要生成的Excel的表模型 
  30.      *  
  31.      * @param list List 填充了 Excel表格数据的集合 
  32.      * @param form ActionForm及其子类 
  33.      * @param excel ExcelModel Excel表的对象模型 
  34.      * @see ExcelModel 
  35.      * @throws Exception 
  36.      */  
  37.     public ExcelModel createDownLoadExcel(List list, ExcelModel excel) throws Exception;  
  38.   
  39.     /** 
  40.      * 在已文件不存在的情况下,采用生成输出流的方式实现左键点击下载功能。 
  41.      *  
  42.      * @param outPutFileName 保存的文件名 
  43.      * @param out ServletOutputStream对象 
  44.      * @param downExcel 填充了数据的ExcelModel 
  45.      * @param HttpServletResponse 
  46.      * @see HttpServletResponse 
  47.      * @throws Exception 
  48.      */  
  49.     public void downLoad(String outPutFileName, ExcelModel downExcel, HttpServletResponse response) throws Exception;  
  50.   
  51. }  



三  公共基类BaseExcelDownLoad,并提供downLoad()方法的公共实现

java 代码
 
  1. public abstract class BaseExcelDownLoad implements ExcelDownLoad {  
  2.   
  3.     /** 
  4.      * 初始化要生成的Excel的表模型 
  5.      *  
  6.      * @param list List 填充了 Excel表格数据的集合 
  7.      * @param form ActionForm及其子类 
  8.      * @param excel ExcelModel Excel表的对象模型 
  9.      * @see ExcelModel 
  10.      * @throws Exception 
  11.      */  
  12.     public abstract ExcelModel createDownLoadExcel(List list, ExcelModel excel) throws Exception;  
  13.   
  14.     /** 
  15.      * 在已文件已存在的情况下,采用读取文件流的方式实现左键点击下载功能 
  16.      *  
  17.      * @param inPutFileName 读出的文件名 
  18.      * @param outPutFileName 保存的文件名 
  19.      * @param HttpServletResponse 
  20.      * @see HttpServletResponse 
  21.      * @throws IOException 
  22.      */  
  23.     public void downLoad(String inPutFileName, String outPutFileName, HttpServletResponse response) throws IOException {  
  24.   
  25.         // 打开指定文件的流信息  
  26.         InputStream is = new FileInputStream(inPutFileName);  
  27.         // 写出流信息  
  28.         int data = -1;  
  29.         OutputStream outputstream = response.getOutputStream();  
  30.   
  31.         // 清空输出流  
  32.         response.reset();  
  33.         // 设置响应头和下载保存的文件名  
  34.         response.setHeader("content-disposition", "attachment;filename=" + outPutFileName);  
  35.         // 定义输出类型  
  36.         response.setContentType("APPLICATION/msexcel");  
  37.   
  38.         while ((data = is.read()) != -1)  
  39.             outputstream.write(data);  
  40.         is.close();  
  41.         outputstream.close();  
  42.         response.flushBuffer();  
  43.   
  44.     }  
  45.   
  46.     /** */  
  47.     /** 
  48.      * 在文件不存在的情况下,采用生成输出流的方式实现左键点击下载功能。 
  49.      *  
  50.      * @param outPutFileName 保存的文件名 
  51.      * @param out ServletOutputStream对象 
  52.      * @param downExcel 填充了数据的ExcelModel 
  53.      * @param HttpServletResponse 
  54.      * @see HttpServletResponse 
  55.      * @throws Exception 
  56.      */  
  57.     public void downLoad(String outPutFileName, ExcelModel downExcel, HttpServletResponse response) throws Exception {  
  58.         
  59.             // 取得输出流  
  60.             OutputStream out = response.getOutputStream();  
  61.             // 清空输出流  
  62.             response.reset();  
  63.   
  64.             // 设置响应头和下载保存的文件名  
  65.             response.setHeader("content-disposition", "attachment;filename=" + outPutFileName);  
  66.             // 定义输出类型  
  67.             response.setContentType("APPLICATION/msexcel");  
  68.   
  69.             ExcelOperator op = new ExcelOperator();  
  70.             // out:传入的输出流  
  71.             op.WriteExcel(downExcel, out);  
  72.   
  73.             out.close();  
  74. //            ServletOutputStream os = response.getOutputStream();   
  75.             // out.clear();  
  76.             // out = pageContext.pushBody();  
  77.   
  78.             // 强行将响应缓存中的内容发送到目的地  
  79.             resp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值