{转载:http://developer.51cto.com/art/200906/130122.htm}
本文介绍了如何Struts2中POI在内存中生成文件并下载。POI是一个JAVA的实用jar包,可以生成excel文件。本文结合struts2和poi,说明如何在内存中生成一个excel文件并下载到客户端。
AD:
POI是一个JAVA的实用jar包,可以生成excel文件,通常在web开发用于把数据库的数据生成excel文件,然后通过下载提供给用户。
本文结合struts2和poi,说明如何在内存中生成一个excel文件并下载到客户端。
首先进行jsp文件,struts.xml文件和action文件的内容说明,对于struts.xml文件的下载配置和action文件中的对应的方法名的设定还不熟悉的朋友可以先看前面这篇文章struts2中下载文件的方法。
文件名:download.jsp
文件位置:网站根目录下的work目录下
文件内容:
下载文件 struts.xml文件
文件内容:
application/vnd.ms-excel attachment;filename="AllUsers.xls" excelFile 然后是action文件
文件名:ExcelDownloadAction.java
文件内容:
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(); } }
蓝色的代码使用poi生成一个excel格式的内容,红色的代码通过字节数组的输入输出流的转换提供给客户端最终的输入流。
好,代码完成后,运行一下,如图,
点击下载链接
可以下载也可以打开,我们选择打开,如图
最后声明,本文的poi生成和下载部分的代码实例,部分参考了网上教程实践而来。
分享到:
相关推荐
NULL 博文链接:https://fengyunxiao.iteye.com/blog/469943
struts2中利用poi导出excel的简单例子
struts2+poi实现excel文件的导出,虽然有很多jxl库实现的方案,而本方案是使用struts2 mvc, apache poi库实现excel的导入导出,文档很详细
利用struts2和poi把数据导出到excel
自己做的一个完整导出功能,struts2+poi导出excel 经过自己严格测试通过的。
Web开发技术,excel导出的详解,对POI导出EXCEL代码实现的详细解释
将assets中.xml中的doc文件中,就会生成word文档和excel文档
利用纯struts2.0+poi插件 实现导出excel
struts2+poi实现excel表格导出
struts2 poi 导入xls xlsx绝对兼容,直接导入myeclipse 使用
使用poi在excel中生成各种常见的图像,比如饼状图(普通饼状图、三D图,环状体、折线图、柱状图、等~~)
struts2、poi.jar、jxl.jar
Struts2 poi 实现从数据库中导出excel表
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
本人文章《使用poi根据模版生成word文档并转换成PDF文件》所需要用到的文件
struts2+poi 实现Excel导出,带jar包,适合初学者
在struts中利用poi组件导出excel
项目内有个excel 拿出来 在index页面点击选择导入直接运行
POI和JFreeChart框架实现生成Excel文件(生成 统计图)