struts2中POI在内存中生成文件并上载

struts2中POI在内存中生成文件并下载
Java代码
1.<%@ page contentType="text/html; charset=gbk" %>  
2.<%@ taglib uri="/struts-tags" prefix="s"%>  
3.<html>  
4.<a href="excel.action">下载文件</a>  
5.</html> 
<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<a href="excel.action">下载文件</a>
</html>


struts.xml文件
文件内容:

Xml代码
1.<?xml version="1.0" encoding="UTF-8" ?> 
2.<!DOCTYPE struts PUBLIC  
3.    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
4.    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
5. 
6.<struts> 
7. 
8.    <package name="default" extends="struts-default"> 
9.        <action name="excel" class="ExcelDownloadAction"> 
10.            <result name="success" type="stream"> 
11.                <param name="contentType">application/vnd.ms-excel</param> 
12.                <param name="contentDisposition">attachment;filename="AllUsers.xls"</param> 
13.                <param name="inputName">excelFile</param> 
14.            </result> 
15.        </action> 
16.    </package> 
17.</struts> 
<?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="default" extends="struts-default">
        <action name="excel" class="ExcelDownloadAction">
            <result name="success" type="stream">
                <param name="contentType">application/vnd.ms-excel</param>
                <param name="contentDisposition">attachment;filename="AllUsers.xls"</param>
                <param name="inputName">excelFile</param>
            </result>
        </action>
    </package>
</struts>



Java代码
1.import java.io.ByteArrayInputStream;  
2.import java.io.ByteArrayOutputStream;  
3.import java.io.IOException;  
4.import java.io.InputStream;  
5. 
6.import org.apache.poi.hssf.usermodel.HSSFCell;  
7.import org.apache.poi.hssf.usermodel.HSSFRow;  
8.import org.apache.poi.hssf.usermodel.HSSFSheet;  
9.import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
10. 
11.import com.opensymphony.xwork2.ActionSupport;  
12. 
13.@SuppressWarnings("serial")  
14.public class ExcelDownloadAction extends ActionSupport {  
15. 
16.    public InputStream getExcelFile() {  
17.        HSSFWorkbook workbook = new HSSFWorkbook();  
18.        HSSFSheet sheet = workbook.createSheet("sheet1");  
19.        {  
20.            // 创建表头  
21.            HSSFRow row = sheet.createRow(0);  
22.            HSSFCell cell = row.createCell((short) 0);  
23.            cell.setCellValue("id");  
24.            cell = row.createCell((short) 1);  
25.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
26.            cell.setCellValue("姓");  
27.            cell = row.createCell((short) 2);  
28.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
29.            cell.setCellValue("名");  
30.            cell = row.createCell((short) 3);  
31.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
32.            cell.setCellValue("年龄");  
33. 
34.            // 创建数据  
35.            // 第一行  
36.            row = sheet.createRow(1);  
37.            cell = row.createCell((short) 0);  
38.            cell.setCellValue("1");  
39.            cell = row.createCell((short) 1);  
40.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
41.            cell.setCellValue("张");  
42.            cell = row.createCell((short) 2);  
43.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
44.            cell.setCellValue("四");  
45.            cell = row.createCell((short) 3);  
46.            cell.setCellValue("23");  
47. 
48.            // 第二行  
49.            row = sheet.createRow(2);  
50.            cell = row.createCell((short) 0);  
51.            cell.setCellValue("2");  
52.            cell = row.createCell((short) 1);  
53.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
54.            cell.setCellValue("李");  
55.            cell = row.createCell((short) 2);  
56.            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
57.            cell.setCellValue("六");  
58.            cell = row.createCell((short) 3);  
59.            cell.setCellValue("30");  
60.        }  
61. 
62.        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
63.        try {  
64.            workbook.write(baos);  
65.        } catch (IOException e) {  
66.            // TODO Auto-generated catch block  
67.            e.printStackTrace();  
68.        }  
69.        byte[] ba = baos.toByteArray();  
70.        ByteArrayInputStream bais = new ByteArrayInputStream(ba);  
71.        return bais;  
72. 
73.    }  
74. 
75.    @Override 
76.    public String execute() throws Exception {  
77.        // TODO Auto-generated method stub  
78.        return super.execute();  
79.    }  
80. 
81.} 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ExcelDownloadAction extends ActionSupport {

    public InputStream getExcelFile() {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet1");
        {
            // 创建表头
            HSSFRow row = sheet.createRow(0);
            HSSFCell cell = row.createCell((short) 0);
            cell.setCellValue("id");
            cell = row.createCell((short) 1);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("姓");
            cell = row.createCell((short) 2);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("名");
            cell = row.createCell((short) 3);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("年龄");

            // 创建数据
            // 第一行
            row = sheet.createRow(1);
            cell = row.createCell((short) 0);
            cell.setCellValue("1");
            cell = row.createCell((short) 1);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("张");
            cell = row.createCell((short) 2);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("四");
            cell = row.createCell((short) 3);
            cell.setCellValue("23");

            // 第二行
            row = sheet.createRow(2);
            cell = row.createCell((short) 0);
            cell.setCellValue("2");
            cell = row.createCell((short) 1);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("李");
            cell = row.createCell((short) 2);
            cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
            cell.setCellValue("六");
            cell = row.createCell((short) 3);
            cell.setCellValue("30");
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            workbook.write(baos);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] ba = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(ba);
        return bais;

    }

    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return super.execute();
    }

}


本文出自:http://kin111.blog.51cto.com/738881/167727