<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 定义逻辑视图与物理视图之间的联系 -->
<package name="excel" extends="struts-default" namespace="/excel">
<action name="ExcelAction" class="com.hangyuan.excel.ExcelAction">
<result name="excel" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">is</param>
<param name="contentDisposition">filename="export.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
1. 先 struts.xml
2.action
package com.hangyuan.excel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.opensymphony.xwork2.ActionSupport;
/**
* http://127.0.0.1:8080/ccc/excel/ExcelAction.action
* @author Administrator
* 这个可以用
*/
public class ExcelAction extends ActionSupport {
InputStream is;
public String execute(){
Label label;
WritableWorkbook workbook;
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("Sheet1", 0);
label = new jxl.write.Label(0, 0, "标题");
sheet.addCell(label);
label = new jxl.write.Label(0,1, "数据");
sheet.addCell(label);
workbook.write();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
is = new ByteArrayInputStream(os.toByteArray());
return "excel";
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
}
3. 访问 http://127.0.0.1:8080/ccc/excel/ExcelAction.action
注意 : 第1 部中的 <param name="inputName">is</param>
和 第二部中的InputStream is; 是一一对应的