日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

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

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

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

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

首先進(jìn)行jsp文件,struts.xml文件和action文件的內(nèi)容說(shuō)明,對(duì)于struts.xml文件的下載配置和action文件中的對(duì)應(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. < 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

文件內(nèi)容:

 
 
 
 
  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)建數(shù)據(jù)
  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. }

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

好,代碼完成后,運(yùn)行一下,如圖,

點(diǎn)擊下載鏈接

可以下載也可以打開(kāi),我們選擇打開(kāi),如圖

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

本文出自 “點(diǎn)點(diǎn)滴滴” 博客。


本文名稱:Struts2中POI在內(nèi)存中生成文件并下載
網(wǎng)址分享:http://www.5511xx.com/article/djdchge.html