<%@ 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
分享到:
相关推荐
NULL 博文链接:https://fengyunxiao.iteye.com/blog/469943
struts2中利用poi导出excel的简单例子
struts2+poi实现excel文件的导出,虽然有很多jxl库实现的方案,而本方案是使用struts2 mvc, apache poi库实现excel的导入导出,文档很详细
Web开发技术,excel导出的详解,对POI导出EXCEL代码实现的详细解释
利用struts2和poi把数据导出到excel
自己做的一个完整导出功能,struts2+poi导出excel 经过自己严格测试通过的。
利用纯struts2.0+poi插件 实现导出excel
struts2+poi实现excel表格导出
POI和JFreeChart框架实现生成Excel文件(生成 统计图)
本人文章《使用poi根据模版生成word文档并转换成PDF文件》所需要用到的文件
struts2 poi 导入xls xlsx绝对兼容,直接导入myeclipse 使用
项目:Struts2UpAndDownLoadFile 1.实现文件的上传和下载 项目:Struts2HandleExcelFile 1.实现文件的上传和下载 2.利用apache的poi-3.5-FINAL-20090928.jar组件实现创建Excel表格。
struts2、poi.jar、jxl.jar
Struts2 poi 实现从数据库中导出excel表
使用poi在excel中生成各种常见的图像,比如饼状图(普通饼状图、三D图,环状体、折线图、柱状图、等~~)
项目内有个excel 拿出来 在index页面点击选择导入直接运行
在struts中利用poi组件导出excel
struts2+poi 实现Excel导出,带jar包,适合初学者
使用java poi进行模版文件的上传,生成表单,重新生成文件。同时包括表单中含有下拉框等选项的处理。