新聞中心
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目錄下
文件內容:
- < %@ page contentType="text/html; charset=gbk" %>
- < %@ taglib uri="/struts-tags" prefix="s"%>
- < html>
- < a href="excel.action">下載文件< /a>
- < /html>
struts.xml文件
文件內容:
- < ?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
文件內容:
- 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)建數據
- // 第一行
- 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();
- }
- }
藍色的代碼使用poi生成一個excel格式的內容,紅色的代碼通過字節(jié)數組的輸入輸出流的轉換提供給客戶端最終的輸入流。
好,代碼完成后,運行一下,如圖,
點擊下載鏈接
可以下載也可以打開,我們選擇打開,如圖
最后聲明,本文的poi生成和下載部分的代碼實例,部分參考了網上教程實踐而來。
本文出自 “點點滴滴” 博客。
網頁名稱:Struts2中POI在內存中生成文件并下載
本文網址:http://www.dlmjj.cn/article/djdchge.html


咨詢
建站咨詢
