参考下面代码,在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>
导出后的文件打开效果:
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優(yōu)質文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦

