Struts2中POI在內(nèi)存中生成文件并下載
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)容:
- < %@ page contentType="text/html; charset=gbk" %>
- < %@ taglib uri="/struts-tags" prefix="s"%>
- < html>
- < a href="excel.action">下載文件< /a>
- < /html>
struts.xml文件
文件內(nèi)容:
- < ?xml version="1.0" encoding="UTF-8" ?>
- < !DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- < struts>
- < package name="default" extends="struts-default">
- < action name="excel" class="ExcelDownloadAction">
- < result name="success" type="stream">
- < param name="contentType">application/vnd.ms-excel< /param>
- < param name="contentDisposition">attachment;filename="AllUsers.xls"< /param>
- < param name="inputName">excelFile< /param>
- < /result>
- < /action>
- < /package>
- < /struts>
然后是action文件
文件名:ExcelDownloadAction.java
文件內(nèi)容:
- 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");
- {
- // 創(chuàng)建表頭
- 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("年齡");
- // 創(chuàng)建數(shù)據(jù)
- // 第一行
- 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();
- }
- }
藍(lán)色的代碼使用poi生成一個excel格式的內(nèi)容,紅色的代碼通過字節(jié)數(shù)組的輸入輸出流的轉(zhuǎn)換提供給客戶端最終的輸入流。
好,代碼完成后,運行一下,如圖,
點擊下載鏈接
可以下載也可以打開,我們選擇打開,如圖
最后聲明,本文的poi生成和下載部分的代碼實例,部分參考了網(wǎng)上教程實踐而來。
本文出自 “點點滴滴” 博客。
【編輯推薦】