日期:2014-05-16  浏览次数:20452 次

Struts + JSP导出Excel报表
据我所知 Java 导 Excel 报表有三种方法:
1, 在 Servlet 直接向客户端输出制表符,如下:


PrintWriter out = response.getWriter();

out.println("xxxx\t");

out.flush();
out.close();



这种方法最原始, 编起来最麻烦.

2, 利用JSP模板, 下面详述具体实现, 这种方式需要先利用office软件先生成Html模板,然后将其转化为JSP页面, 不依赖第三方实现, 比较轻量级, 适合简单应用.

3, 第三方库, jxl.jar, 这种方式最灵活,网上文章也多,不再赘述.

Struts 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>
  <form-beans />
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      path="/exportExcel"
      type="com.mypack.struts.action.ExportExcelAction"
      cancellable="true">
      <forward name="downloadPage" path="/jsp/downloadPage.jsp" />
    </action>

  </action-mappings>

  <message-resources parameter="com.mypack.struts.ApplicationResources" />
</struts-config>



Action实现:


/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.mypack.struts.action;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/** 
 * MyEclipse Struts
 * Creation date: 11-07-2010
 * 
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class ExportExcelAction extends Action {
	
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List list = new ArrayList();
		
		for (int i = 0; i < 100; i ++) {
			list.add(UUID.randomUUID().toString());
		}
		
		request.setAttribute("result", list);
		
		System.out.println(list);
		
		response.setContentType("application/vnd.ms-excel;charset=UTF-8");
		//response.setHeader("Content-Disposition" ,"attachment;filename=report");
		
		response.setHeader ("Content-Disposition","attachment;filename="+ UUID.randomUUID() +".xls");
		
		return mapping.findForward("downloadPage");
	}
	
}








JSP 报表页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 11">
<link rel=File-List href="Book2.files/filelist.xml">
<link rel=Edit-Time-Data href="Book2.files/editdata.mso">
<link rel=OLE-Object-Data href="Book2.files/oledata.mso">
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>
  <o:Author>微软用户</o:Author>
  <o:LastAuthor>微软用户</o:LastAuthor>
  <o:Created>2010-11-07T08:34:34Z</o:Created>
  <o:LastSaved>2010-11-07T08:38:14Z</o:LastSaved>
  <o:Company>微软中国</o:Company>
  <o:Version>11.9999</o:Version>
 </o:DocumentProperties>
</xml><![endif]-->
<style>
<!--table
	{mso-displayed-decimal-separator:"\.";
	mso-displayed-thousand-separator:"\,";}
@page
	{margin:1.0in .