struts2 導出excel

1.所需jar包java

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.jarapache

excel導出:jxl.jarapp

2.配置ide

Web.xml代碼
 url

            <filter>  spa

    <filter-name>struts2</filter-name>  .net

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  excel

</filter>  orm

<filter-mapping>  xml

    <filter-name>struts2</filter-name>  

    <url-pattern>/*</url-pattern>  

</filter-mapping>  

 

  

Struts.xml代碼
<?xml version="1.0" encoding="UTF-8"?>  

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >  

<struts>  

    <package name="platform-default" extends="struts-default">  

        <action name="excel" class="action.ExcelAction">  

            <result name="excel" type="stream">  

                <param name="contentType">                       application/vnd.ms-excel  

                </param>  

               <param name="inputName">excelStream</param>  

                <param name="contentDisposition">  

                    filename="export.xls" 

                </param>  

                <param name="bufferSize">1024</param>  

            </result>  

        </action>  

    </package>  

</struts> 

 3.Action實現

Excelaction代碼
package action;  

 

import java.io.InputStream;  

import service.IExcelService;  

import service.impl.ExcelServiceImpl;  

public class ExcelAction {  

      

    InputStream excelStream;  

      

    public String execute(){  

        IExcelService es = new ExcelServiceImpl();  

        excelStream = es.getExcelInputStream();  

        return "excel";  

    }  

    //get set...  

4.Service實現

a.接口

Iexcelservice代碼
package service;  

import java.io.InputStream;  

public interface IExcelService {  

    InputStream getExcelInputStream();  

}  b.實現類

Excelserviceimpl.java代碼
package service.impl;  
import java.io.ByteArrayInputStream;  

import java.io.ByteArrayOutputStream;  

import java.io.InputStream;  

import java.io.OutputStream;  

import jxl.Workbook;  

import jxl.write.WritableSheet;  

import jxl.write.WritableWorkbook;  

import service.IExcelService;  

 

public class ExcelServiceImpl implements IExcelService {  

   @Override  

public InputStream getExcelInputStream() {  

   //將OutputStream轉化爲InputStream  

        ByteArrayOutputStream out = new ByteArrayOutputStream();  

        putDataOnOutputStream(out);  

        return new ByteArrayInputStream(out.toByteArray());  

    }  

 

    private void putDataOnOutputStream(OutputStream os) {  

        jxl.write.Label label;  

        WritableWorkbook workbook;  

        try {  

            workbook = Workbook.createWorkbook(os);  

            WritableSheet sheet = workbook.createSheet("Sheet1", 0);  

 

            label = new jxl.write.Label(0, 0, "struts2導出excel");  

            sheet.addCell(label);  

              

            workbook.write();  

            workbook.close();  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

    }  

 

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/hao1314/archive/2010/04/16/5493454.aspx

相關文章
相關標籤/搜索