【转】struts2中POI在内存中生成文件并下载


<%@ page contentType="text/html; charset=gbk" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<a href="excel.action">下载文件</a>
</html>


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="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>



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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值