`
truth99
  • 浏览: 61365 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

struts2 poi 导出excel

阅读更多
废话不多说
先把poi.jar文件引入到项目中,下载链接

html页面


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


struts.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文件名称


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;
	}
}

0
1
分享到:
评论
1 楼 sysuzzq 2013-04-19  
你好,我有一段跟你基本一样的代码,当数据量比较多时,发现生成完Excel后Tomcat的内存没有被释放。我在Action里面添加baos.flush();baos.close();后也没用。想请问一下你有没有好的解决办法

相关推荐

Global site tag (gtag.js) - Google Analytics