废话不多说
先把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;
}
}
分享到:
相关推荐
利用struts2和poi把数据导出到excel
struts2中利用poi导出excel的简单例子
此文档提供全面的poi导出excel,对于有这方面需要的朋友有很大的帮助。
自己做的一个完整导出功能,struts2+poi导出excel 经过自己严格测试通过的。
Web开发技术,excel导出的详解,对POI导出EXCEL代码实现的详细解释
利用纯struts2.0+poi插件 实现导出excel
struts2+poi实现excel表格导出
程序使用Struts2(SSH即Struts2+Hibernate+Spring)+POI+MySQL实现Excel动态数据导入和导出,程序源码文件附带运行说明
包含POI 的Jar包,利用反射机制写的导出报表类,报表实体类,测试类,struts 2配置文件及Action中调用
struts2+poi实现excel文件的导出,虽然有很多jxl库实现的方案,而本方案是使用struts2 mvc, apache poi库实现excel的导入导出,文档很详细
使用poi从数据库导出excel表的示例
Struts2 poi 实现从数据库中导出excel表
poi struts2 导出Excel 用jdbc连的sql server2000 非常简单 一看就能明白怎么个回事 就一张表 3个字段 适合第一次用poi的朋友
struts2+poi 实现Excel导出,带jar包,适合初学者
web中使用POI导入导出EXCEL文件的例子 struts1.x的例子,struts2.x可以参考自己修改 1.action的写法 import java.io.*; import java.sql.*; import java.util.ArrayList; import javax.servlet....
在struts中利用poi组件导出excel
POI + Struts2 实现Excel导出工具包,POI + Struts2 实现Excel导出工具包
1.本动态导入导出Excel工程导入Eclipse即可用行,支持所有版本的Excel导入导出。...2.程序使用Struts2+POI+SQLSever(SSH即Struts2+Hibernate+Spring)实现Excel动态数据导入和导出,压缩包内含程序源码文件。
基于struts2 spring ibatis 使用poi组建对查询出数据库表中的数据导出Excel中的源码及案例