文章出處
文章列表
參考下面代碼,在Action中加一個方法:
1 // 導出excel 2 public String excel() throws Exception { 3 StringBuffer excelBuf = new StringBuffer(); 4 excelBuf.append("運單號").append("\t").append("始發站").append("\t").append("目的站").append("\n"); 5 excelBuf.append("112-00100100").append("\t").append("PEK").append("\t").append("SHA").append("\n"); 6 excelBuf.append("112-00100111").append("\t").append("PVG").append("\t").append("XIY").append("\n"); 7 excelBuf.append("112-00100122").append("\t").append("SHA").append("\t").append("HHY").append("\n"); 8 String excelString = excelBuf.toString(); 9 excelStream = new ByteArrayInputStream(excelString.getBytes(), 0, excelString.getBytes().length); 10 return "excel"; 11 }
實質上是一個格式化的cvs文本文件,但是所有的excel/wps都能識別這種格式,導出的數據量不大,且沒有復雜的線框格式要求時,這種處理方式最為方便
struts2的配置文件:
1 <package name="cba_index" ...> 2 ... 3 <action name="index_*" method="{1}" class="CbaAction"> 4 <result name="success">/mu-reservation/cba/index.jsp</result> 5 <!-- 導出excel --> 6 <result name="excel" type="stream"> 7 <param name="contentType">application/vnd.ms-excel</param> <!-- 注意這里的ContentType --> 8 <param name="inputName">excelStream</param> <!-- 這里需要和Action里的變量名一致 --> 9 <param name="contentDisposition">filename="download.xls"</param> <!-- 下載文件的名字 --> 10 <param name="bufferSize">10240</param> <!-- 下載文件的大小 10485760=10M --> 11 </result> 12 </action> 13 ... 14 </package>
頁面上
1 <a href="index_excel.do" target="_blank">導出excel示例</a>
導出后的文件打開效果:
文章列表
全站熱搜