---
title: "struts2 poi 导出excel"
author: "truth99"
date: "2013-03-05T12:16:06Z"
tags: []
categories: ["编程语言"]
slug: "struts2-poi-excel"
draft: false
lastmod: "2013-03-05T12:18:33Z"
id: 1821663
url: "https://truth99.iteye.com/blog/1821663"
---

**<span style="color:blue">废话不多说
先把poi.jar文件引入到项目中，[下载链接](http://poi.apache.org/download.html#POI-3.9)

html页面
</span>**


```html

<a href="actions/export">下载execl</a>

```


<span style="color:blue">**struts.xml配置**</span>


```xml

<package name="struts2" namespace="/actions" extends="json-default">
       <action name="export" class="com.truth.action.ExportAction">
			<result type="stream">
				<param name="encode">true</param>
				<param name="contentType">application/vnd.ms-excelcharset=GBK</param>
				<param name="inputName">execlStream</param>
				<param name="contentDisposition">attachment;filename="${fileName}"</param>
				<param name="bufferSize">1024</param>
			</result>
		</action>
</package>

```


**<param name="inputName">execlStream</param>中的execlStream要和java文件中的get方法一致，如：getExeclStream，否则报错

fileName也要在java文件中对应，这个是excel文件名称
**

<span style="color:blue">**java文件**</span>


```java

package com.truth.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
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;

public class ExportAction extends ActionSupport {

	private static final long serialVersionUID = -7721390106400928567L;
	private static final Logger log = Logger.getLogger(ExportAction.class);
	
	private String fileName = "什sdsd.xls";
	



	@SuppressWarnings("deprecation")
	public InputStream getExeclStream() {
		try {
			fileName = new String(fileName.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		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.setCellValue("姓");
            cell = row.createCell((short) 2);  
            cell.setCellValue("名");
            cell = row.createCell((short) 3);  
            cell.setCellValue("年龄");  

            // 创建数据  
            // 第一行  
            row = sheet.createRow(1);  
            cell = row.createCell((short) 0);  
            cell.setCellValue("1");  
            cell = row.createCell((short) 1);  
            cell.setCellValue("张");  
            cell = row.createCell((short) 2);  
            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.setCellValue("李");  
            cell = row.createCell((short) 2);  
            cell.setCellValue("六");  
            cell = row.createCell((short) 3);  
            cell.setCellValue("30");  
        }  

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {  
            workbook.write(baos);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        byte[] ba = baos.toByteArray();  
        ByteArrayInputStream bais = new ByteArrayInputStream(ba);  
        return bais;  
	}



	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	public String getFileName() {
		return fileName;
	}
	
	
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
}


```