作者:东区一匹狼

时间:2006-10-17

最近用户要求添加功能把数据导出成Excel的.xls到用户的PC,由于导出的Excel数据对于服务端没有用处,不需要在服务端保留,如果在服务端硬盘生成文件,然后在让用户下载,这样就需要定时清理服务端数据,自己查了一下POI和WebWork的资料,实现了该功能,直接把POI生成的Excel 流输出到浏览器,让用户下载,关闭窗口就消失,不在服务端留痕迹,写出来大家分享一下。

用过WebWork的人应该知道,在Webwork的Action中,execute()方法最后返回的是一个Result对象,如 return SUCCESS 。在这里我们需要实现WebWork的Result接口,定义自己的一个输出Excel的Result。前提是你需要下载POI包,代码如下:

package com.test.excel;

import com.opensymphony.xwork.Result;

import com.opensymphony.xwork.ActionInvocation;

import com.opensymphony.webwork.ServletActionContext;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;

import java.io.OutputStream;

public class ExcelResult implements Result{

private HSSFWorkbook workbook;

private String filename;

private String contenttype;

public void execute(ActionInvocation invocation) throws Exception {

if (contenttype == null)

contenttype = “application/msexcel”

if (workbook == null)

workbook = (HSSFWorkbook) invocation.getStack().findValue(“workbook”);

HttpServletResponse response = ServletActionContext.getResponse();

response.reset() ;

response.setContentType(contenttype);

response.setHeader(“Content-Disposition”, “attachment;Filename=” + filename + “.xls”);

OutputStream os = response.getOutputStream();

workbook.write(os);

os.flush();

os.close();

}

public void setWorkbook(HSSFWorkbook workbook) {

this.workbook = workbook;

}

public void setFilename(String filename) {

this.filename = filename;

}

public void setContenttype(String contenttype) {

this.contenttype = contenttype;

}

}

可以看到,在execute()方法中通过参数invocation的 invocation.getStack().findValue(“workbook”)得到action中的workbook对象,当然,在你的 action中,对生成好的workbook要有get()方法,在后面的代码中会看到。然后workbook.write到response对象就可以了,在这之前,你需要设置html的头部,见上面的代码。

OK,写完上面自定义的Result,我们就可以写Action了,代码如下:

package test.action;

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.xwork.ActionSupport;

public class ExcelTest extends ActionSupport {

private HSSFWorkbook workbook;

public String execute() throws Exception {

workbook = outputExcelFile();

return SUCCESS;

}

private HSSFWorkbook outputExcelFile() {

try {

HSSFWorkbook workbook = new HSSFWorkbook();

HSSFSheet sheet = workbook.createSheet();

workbook.setSheetName(0, “测试输出Excel”, HSSFWorkbook.ENCODING_UTF_16);

HSSFRow row = sheet.createRow((short) 0);

HSSFCell cell = row.createCell((short) 0);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“标题1″);

cell = row.createCell((short) 1);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“标题2″);

cell = row.createCell((short) 2);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“标题3″);

cell = row.createCell((short) 3);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“标题4″);

row = sheet.createRow(1);

cell = row.createCell((short) 0);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“内容1″);

cell = row.createCell((short) 1);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“内容2″);

cell = row.createCell((short) 2);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“内容3″);

cell = row.createCell((short) 3);

cell.setCellType(HSSFCell.CELL_TYPE_STRING);

cell.setEncoding(HSSFCell.ENCODING_UTF_16);

cell.setCellValue(“内容4″);

return workbook;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public HSSFWorkbook getWorkbook() {

return workbook;

}

}

然后在xwork.xml里面配置result类型

<!– 定义结果类型 –>

<result-types>

<result-type name=”dispatcher” class=”com.opensymphony.webwork.dispatcher.ServletDispatcherResult” default=”true”/>

<result-type name=”redirect” class=”com.opensymphony.webwork.dispatcher.ServletRedirectResult”/>

<result-type name=”velocity” class=”com.opensymphony.webwork.dispatcher.VelocityResult”/>

<result-type name=”chain” class=”com.opensymphony.xwork.ActionChainResult”/>

<result-type name=”xslt” class=”com.opensymphony.webwork.views.xslt.XSLTResult”/>

<result-type name=”jasper” class=”com.opensymphony.webwork.views.jasperreports.JasperReportsResult”/>

<result-type name=”freemarker” class=”com.opensymphony.webwork.views.freemarker.FreemarkerResult”/>

<result-type name=”httpheader” class=”com.opensymphony.webwork.dispatcher.HttpHeaderResult”/>

<result-type name=”stream” class=”com.opensymphony.webwork.dispatcher.StreamResult”/>

<result-type name=”plaintext” class=”com.opensymphony.webwork.dispatcher.PlainTextResult” />

<result-type name=”excel” class=”com.test.excel”/>

</result-types>

定义action

<action name=”output_excel” class=”test.action.ExcelTest.ExcelResult”>

<result name=”success” type=”excel”>

<param name=”filename”>ExcelTest</param>

</result>

</action>

大功告成!