本文将讲解在Struts2框架下如何使用POI创建Office Excel文档并实现下载功能。
Apache POI ,操作微软文档的Java API,简单来说就是可以用来操作Office文档的API.
Apache POI官网:http://poi.apache.org/
Apache POI Jar包官方下载:http://poi.apache.org/download.html
效果截图:
1.创建一个Java Web工程并配置Struts2环境,详细步骤省略。。。
注意:引入POI jar包,此工程中只需要poi-3.13.jar即可。
jar包:
2.修改index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <title>首页</title> </head> <body> <a href="exportExcel.action">下载</a> </body> </html>
3.修改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 <!--配置包时必须指定name属性,该属性值可以任意取名,但必须唯一。 --> 7 <package name="aynu" extends="struts-default"> 8 9 <!--测试Excel下载 --> 10 <action name="exportExcel" class="com.xingyun.action.ExcelExportAction" 11 method="exportExcel"> 12 <result name="success" type="stream"> 13 <!-- 下载文件的类型,文件类型对照表参看 http://tool.oschina.net/commons --> 14 <param name="contentType">application/vnd.ms-excel</param> 15 <!-- 返回流 excelStream为action中的流变量名称 --> 16 <param name="inputName">excelStream</param> 17 <!-- 文件下载的处理方式,包括内联(inline)和附件(attachment) 提示下载 --> 18 <param name="contentDisposition">attachment;filename=${excelFileName}</param> 19 <param name="bufferSize">2048</param> 20 </result> 21 </action> 22 </package> 23 24 </struts>
4.创建Action类
1 package com.xingyun.action; 2 import org.apache.poi.hssf.usermodel.*; 3 4 import com.opensymphony.xwork2.ActionSupport; 5 6 import java.io.ByteArrayInputStream; 7 import java.io.ByteArrayOutputStream; 8 import java.io.InputStream; 9 import java.text.SimpleDateFormat; 10 import java.util.Date; 11 12 public class ExcelExportAction extends ActionSupport { 13 14 /** 导出Excel测试 */ 15 public String exportExcel() { 16 try { 17 //第一步,创建一个webbook,对应一个Excel文件 18 HSSFWorkbook wb = new HSSFWorkbook(); 19 //第二步,在webbook中添加一个sheet,对应Excel文件中的 sheet 20 HSSFSheet sheet = wb.createSheet("测试表格1"); 21 //第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制 22 HSSFRow row = sheet.createRow(0); 23 //第四步,创建单元格样式:居中 24 HSSFCellStyle style = wb.createCellStyle(); 25 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); 26 //第五步,创建表头单元格,并设置样式 27 HSSFCell cell; 28 29 cell = row.createCell(0); 30 cell.setCellValue("员工工号"); 31 cell.setCellStyle(style); 32 33 cell = row.createCell(1); 34 cell.setCellValue("员工姓名"); 35 cell.setCellStyle(style); 36 37 cell = row.createCell(2); 38 cell.setCellValue("所属部门"); 39 cell.setCellStyle(style); 40 41 cell = row.createCell(3); 42 cell.setCellValue("职位"); 43 cell.setCellStyle(style); 44 45 cell = row.createCell(4); 46 cell.setCellValue("入职日期"); 47 cell.setCellStyle(style); 48 49 cell = row.createCell(5); 50 cell.setCellValue("备注"); 51 cell.setCellStyle(style); 52 53 //第六步,写入实体数据,实际应用中这些数据从数据库得到 54 Date today = new Date(); 55 long aDay = 1000L*60*60*24; 56 SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); 57 for (int i = 1; i <= 10; i++) { 58 row = sheet.createRow(i); 59 row.createCell(0).setCellValue(i); 60 row.createCell(1).setCellValue("员工" + i); 61 row.createCell(2).setCellValue("总公司"); 62 row.createCell(3).setCellValue("普通员工"); 63 row.createCell(4).setCellValue(fmt.format(new Date(today.getTime() + i * aDay))); 64 row.createCell(5).setCellValue("员工备注"); 65 } 66 67 //第七步,将文件存到流中 68 ByteArrayOutputStream os = new ByteArrayOutputStream(); 69 wb.write(os); 70 byte[] fileContent = os.toByteArray(); 71 ByteArrayInputStream is = new ByteArrayInputStream(fileContent); 72 73 excelStream = is; //文件流 74 excelFileName = "report.xls"; //设置下载的文件名 75 } 76 catch(Exception e) { 77 e.printStackTrace(); 78 } 79 80 return "success"; 81 } 82 83 84 //------------------------------------------------------------- 85 private InputStream excelStream; //输出流变量 86 private String excelFileName; //下载文件名 87 88 public InputStream getExcelStream() { 89 return excelStream; 90 } 91 public void setExcelStream(InputStream excelStream) { 92 this.excelStream = excelStream; 93 } 94 public String getExcelFileName() { 95 return excelFileName; 96 } 97 public void setExcelFileName(String excelFileName) { 98 this.excelFileName = excelFileName; 99 } 100 }
5.启动Tomcat并发布后打开浏览器
http://127.0.0.1:8080/Struts2POIDownLoadExcelDemo/
效果图如下:
下载后打开文件效果图:
Struts2使用POI创建Excel并下载Demo.ZIP
源码下载:http://pan.baidu.com/s/1i3TXa8t 密码:3mmw
原文:http://www.cnblogs.com/lovecode521/p/5043802.html