Struts2中POI在内存中生成文件并下载

开发 后端
本文介绍了如何Struts2中POI在内存中生成文件并下载。POI是一个JAVA的实用jar包,可以生成excel文件。本文结合struts2和poi,说明如何在内存中生成一个excel文件并下载到客户端。

POI是一个JAVA的实用jar包,可以生成excel文件,通常在web开发用于把数据库的数据生成excel文件,然后通过下载提供给用户。

本文结合struts2和poi,说明如何在内存中生成一个excel文件并下载到客户端。

首先进行jsp文件,struts.xml文件和action文件的内容说明,对于struts.xml文件的下载配置和action文件中的对应的方法名的设定还不熟悉的朋友可以先看前面这篇文章struts2中下载文件的方法。

文件名:download.jsp

文件位置:网站根目录下的work目录下

文件内容:

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

struts.xml文件

文件内容:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2. < !DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.  
  6. < struts> 
  7.  
  8.     < package name="default" extends="struts-default"> 
  9.         < action name="excel" class="ExcelDownloadAction"> 
  10.             < result name="success" type="stream"> 
  11.                 < param name="contentType">application/vnd.ms-excel< /param> 
  12.                 < param name="contentDisposition">attachment;filename="AllUsers.xls"< /param> 
  13.                 < param name="inputName">excelFile< /param> 
  14.             < /result> 
  15.         < /action> 
  16.     < /package> 
  17.       
  18. < /struts> 
  19.  

然后是action文件

文件名:ExcelDownloadAction.java

文件内容:

  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5.  
  6. import org.apache.poi.hssf.usermodel.HSSFCell;  
  7. import org.apache.poi.hssf.usermodel.HSSFRow;  
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  10.  
  11. import com.opensymphony.xwork2.ActionSupport;  
  12.  
  13. @SuppressWarnings("serial")  
  14. public class ExcelDownloadAction extends ActionSupport {  
  15.  
  16.     public InputStream getExcelFile() {  
  17.         HSSFWorkbook workbook = new HSSFWorkbook();  
  18.         HSSFSheet sheet = workbook.createSheet("sheet1");  
  19.         {  
  20.             // 创建表头  
  21.             HSSFRow row = sheet.createRow(0);  
  22.             HSSFCell cell = row.createCell((short0);  
  23.             cell.setCellValue("id");  
  24.             cell = row.createCell((short1);  
  25.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  26.             cell.setCellValue("姓");  
  27.             cell = row.createCell((short2);  
  28.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  29.             cell.setCellValue("名");  
  30.             cell = row.createCell((short3);  
  31.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  32.             cell.setCellValue("年龄");  
  33.  
  34.             // 创建数据  
  35.             // 第一行  
  36.             row = sheet.createRow(1);  
  37.             cell = row.createCell((short0);  
  38.             cell.setCellValue("1");  
  39.             cell = row.createCell((short1);  
  40.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  41.             cell.setCellValue("张");  
  42.             cell = row.createCell((short2);  
  43.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  44.             cell.setCellValue("四");  
  45.             cell = row.createCell((short3);  
  46.             cell.setCellValue("23");  
  47.  
  48.             // 第二行  
  49.             row = sheet.createRow(2);  
  50.             cell = row.createCell((short0);  
  51.             cell.setCellValue("2");  
  52.             cell = row.createCell((short1);  
  53.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  54.             cell.setCellValue("李");  
  55.             cell = row.createCell((short2);  
  56.             cell.setEncoding(HSSFWorkbook.ENCODING_UTF_16);  
  57.             cell.setCellValue("六");  
  58.             cell = row.createCell((short3);  
  59.             cell.setCellValue("30");  
  60.         }  
  61.  
  62.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  63.         try {  
  64.             workbook.write(baos);  
  65.         } catch (IOException e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         }  
  69.         byte[] ba = baos.toByteArray();  
  70.         ByteArrayInputStream bais = new ByteArrayInputStream(ba);  
  71.         return bais;  
  72.  
  73.     }  
  74.  
  75.     @Override 
  76.     public String execute() throws Exception {  
  77.         // TODO Auto-generated method stub  
  78.         return super.execute();  
  79.     }  
  80.  
  81. }  
  82.  

蓝色的代码使用poi生成一个excel格式的内容,红色的代码通过字节数组的输入输出流的转换提供给客户端最终的输入流。

好,代码完成后,运行一下,如图,

代码完成后运行

点击下载链接

点击下载链接

可以下载也可以打开,我们选择打开,如图

下载也可以打开

最后声明,本文的poi生成和下载部分的代码实例,部分参考了网上教程实践而来。

本文出自 “点点滴滴” 博客。

【编辑推荐】

  1. Struts2深入详解properties配置文件
  2. Struts2 iterator介绍及功能详解
  3. Struts2 checkbox适用场景及实例分析
  4. Struts2 国际化与防止刷新重复提交表单
  5. Struts2中Form提交的Javascript实现两例
责任编辑:yangsai 来源: 51CTO博客
相关推荐

2009-06-04 08:45:01

Struts2下载

2009-06-04 08:34:24

Struts2配置struts.xml

2009-07-29 09:54:34

struts2和str

2009-06-25 15:50:03

Struts2教程上传任意多个文件

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-06-04 09:41:50

struts2上传文件

2009-02-04 10:51:07

2009-06-05 10:52:45

struts2深入详解配置文件

2009-02-04 14:00:59

2009-07-03 09:35:57

Struts2 JSP

2009-06-05 09:40:59

2009-06-08 16:44:00

Struts2文件上传

2009-06-05 10:55:07

struts2 web

2011-07-18 14:43:40

JSON模拟加载初析

2010-05-10 15:06:37

Oracle stru

2009-07-14 17:10:44

struts2webwork

2009-06-03 14:19:34

Struts2Guice

2009-06-04 09:20:19

struts2 if标使用

2013-07-18 15:09:27

2009-06-25 16:04:30

点赞
收藏

51CTO技术栈公众号