自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Struts2中POI在內(nèi)存中生成文件并下載

開發(fā) 后端
本文介紹了如何Struts2中POI在內(nèi)存中生成文件并下載。POI是一個JAVA的實用jar包,可以生成excel文件。本文結(jié)合struts2和poi,說明如何在內(nèi)存中生成一個excel文件并下載到客戶端。

POI是一個JAVA的實用jar包,可以生成excel文件,通常在web開發(fā)用于把數(shù)據(jù)庫的數(shù)據(jù)生成excel文件,然后通過下載提供給用戶。

本文結(jié)合struts2和poi,說明如何在內(nèi)存中生成一個excel文件并下載到客戶端。

首先進(jìn)行jsp文件,struts.xml文件和action文件的內(nèi)容說明,對于struts.xml文件的下載配置和action文件中的對應(yīng)的方法名的設(shè)定還不熟悉的朋友可以先看前面這篇文章struts2中下載文件的方法。

文件名:download.jsp

文件位置:網(wǎng)站根目錄下的work目錄下

文件內(nèi)容:

  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文件

文件內(nèi)容:

  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

文件內(nèi)容:

  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.             // 創(chuàng)建表頭  
  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.             // 創(chuàng)建數(shù)據(jù)  
  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.  

藍(lán)色的代碼使用poi生成一個excel格式的內(nèi)容,紅色的代碼通過字節(jié)數(shù)組的輸入輸出流的轉(zhuǎn)換提供給客戶端最終的輸入流。

好,代碼完成后,運行一下,如圖,

代碼完成后運行

點擊下載鏈接

點擊下載鏈接

可以下載也可以打開,我們選擇打開,如圖

下載也可以打開

最后聲明,本文的poi生成和下載部分的代碼實例,部分參考了網(wǎng)上教程實踐而來。

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

【編輯推薦】

  1. Struts2深入詳解properties配置文件
  2. Struts2 iterator介紹及功能詳解
  3. Struts2 checkbox適用場景及實例分析
  4. Struts2 國際化與防止刷新重復(fù)提交表單
  5. Struts2中Form提交的Javascript實現(xiàn)兩例
責(zé)任編輯:yangsai 來源: 51CTO博客
相關(guān)推薦

2009-06-04 08:34:24

Struts2配置struts.xml

2009-06-04 08:45:01

Struts2下載

2009-07-29 09:54:34

struts2和str

2009-06-25 15:50:03

Struts2教程上傳任意多個文件

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 10:51:07

2009-06-05 10:52:45

struts2深入詳解配置文件

2009-02-04 14:00:59

2009-06-04 09:41:50

struts2上傳文件

2009-07-03 09:35:57

Struts2 JSP

2009-06-08 16:44:00

Struts2文件上傳

2009-06-05 10:55:07

struts2 web

2009-06-05 09:40:59

2010-05-10 15:06:37

Oracle stru

2009-06-25 15:26:25

Struts2教程struts.xml常

2011-08-19 13:13:14

struts2Java

2012-05-10 14:00:06

StrutsjsonJava

2013-07-19 09:36:04

struts2struts2漏洞

2011-07-18 14:43:40

JSON模擬加載初析

2009-06-03 14:19:34

Struts2Guice
點贊
收藏

51CTO技術(shù)棧公眾號