日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
Struts2中POI在內存中生成文件并下載

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

專注于為中小企業(yè)提供成都網站設計、成都網站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)九龍坡免費做網站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網行業(yè)人才,有力地推動了近千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網站建設實現規(guī)模擴充和轉變。

本文結合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. < struts>
  6.     < package name="default" extends="struts-default">
  7.         < action name="excel" class="ExcelDownloadAction">
  8.             < result name="success" type="stream">
  9.                 < param name="contentType">application/vnd.ms-excel< /param>
  10.                 < param name="contentDisposition">attachment;filename="AllUsers.xls"< /param>
  11.                 < param name="inputName">excelFile< /param>
  12.             < /result>
  13.         < /action>
  14.     < /package>
  15.     
  16. < /struts>

然后是action文件

文件名:ExcelDownloadAction.java

文件內容:

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

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

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

點擊下載鏈接

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

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

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


網頁名稱:Struts2中POI在內存中生成文件并下載
本文網址:http://www.dlmjj.cn/article/djdchge.html