struts2导出excel java 导出excel

来源:互联网 发布:windows双机热备方案 编辑:程序博客网 时间:2024/04/30 09:50

l例子很简单,就一个Action类和一个Service类

 

1.所需jar包

struts2需要jar包:commons-logging.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.1.jar、xwork-2.0.4.jar

excel导出:jxl.jar

2.配置

Web.xml代码  收藏代码
  1.   .....            
  2.              <filter>  
  3.     <filter-name>struts2</filter-name>  
  4.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  5. </filter>  
  6. <filter-mapping>  
  7.     <filter-name>struts2</filter-name>  
  8.     <url-pattern>/*</url-pattern>  
  9. </filter-mapping>  
  10.  ......  
 
Struts.xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >  
  3. <struts>  
  4.     <package name="platform-default" extends="struts-default">  
  5.         <action name="excel" class="action.ExcelAction">  
  6.             <result name="excel" type="stream">  
  7.                 <param name="contentType">  
  8.                     application/vnd.ms-excel  
  9.                 </param>  
  10.                 <param name="inputName">excelStream</param>  
  11.                 <param name="contentDisposition">  
  12.                     filename="export.xls"  
  13.                 </param>  
  14.                 <param name="bufferSize">1024</param>  
  15.             </result>  
  16.         </action>  
  17.     </package>  
  18. </struts>  

3.Action实现

 

Excelaction代码  收藏代码
  1. package action;  
  2.   
  3. import java.io.InputStream;  
  4. import service.IExcelService;  
  5. import service.impl.ExcelServiceImpl;  
  6. public class ExcelAction {  
  7.       
  8.     InputStream excelStream;  
  9.       
  10.     public String execute(){  
  11.         IExcelService es = new ExcelServiceImpl();  
  12.         excelStream = es.getExcelInputStream();  
  13.         return "excel";  
  14.     }  
  15.     //get set...  
  16. }  

4.Service实现

a.接口

 

Iexcelservice代码  收藏代码
  1. package service;  
  2. import java.io.InputStream;  
  3. public interface IExcelService {  
  4.     InputStream getExcelInputStream();  
  5. }  

b.实现类

 

Excelserviceimpl.java代码  收藏代码
  1. package service.impl;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7.   
  8. import jxl.Workbook;  
  9. import jxl.write.WritableSheet;  
  10. import jxl.write.WritableWorkbook;  
  11. import service.IExcelService;  
  12.   
  13. public class ExcelServiceImpl implements IExcelService {  
  14.   
  15.     @Override  
  16.     public InputStream getExcelInputStream() {  
  17.         //将OutputStream转化为InputStream  
  18.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  19.         putDataOnOutputStream(out);  
  20.         return new ByteArrayInputStream(out.toByteArray());  
  21.     }  
  22.   
  23.     private void putDataOnOutputStream(OutputStream os) {  
  24.         jxl.write.Label label;  
  25.         WritableWorkbook workbook;  
  26.         try {  
  27.             workbook = Workbook.createWorkbook(os);  
  28.             WritableSheet sheet = workbook.createSheet("Sheet1"0);  
  29.   
  30.             label = new jxl.write.Label(00"struts2导出excel");  
  31.             sheet.addCell(label);  
  32.               
  33.             workbook.write();  
  34.             workbook.close();  
  35.         } catch (Exception e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39. }  

 

 这样例子就完成了:

直接访问:http://127.0.0.1:8080/JXL_excel/excel.action 就可以导出excel

 

说明:

1、例子中OutputStream转InputStream是一种简单的实现,但是需要内存比较多,可以参考:
http://ostermiller.org/convert_java_outputstream_inputstream.html

2、参考实现:http://merrygrass.iteye.com/blog/558274

3、java报表JXL和POI打印设置

 4、源码下载(例子和jar包 在eclipse下 导入可直接运行):http://download.csdn.net/detail/zljjava/4284947

原创粉丝点击