struts2自带excel导出功能

struts2中有对导出excel表格的支持,所以开发起来比较容易,主要的步骤有三点:

[size=medium][color=red]1.struts.xml的action相关配置: [/color][/size]
<action name="ExportAction" class="com.huawei.nsm.workflow.action.ExportAction">
<result name="excel" type="stream">

<!-- 注意这里的ContentType -->
<param name="contentType">application/vnd.ms-excel</param>

<!-- 这里需要和Action里的变量名一致 -->

<param name="inputName">excelStream</param>

<param name="contentDisposition">filename="export.xls"</param>
<param name="bufferSize">1024</param>
</result>
</action>


[size=medium][color=red]2.JAVA的ExportAction类:[/color][/size]
public class ExportAction extends ActionSupport { 

private QueryDao dao = new QueryDao();

private InputStream excelStream;

public InputStream getExcelStream() {
return excelStream;
}

public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}

@Override
public String execute() throws Exception {
StringBuffer excelBuf = new StringBuffer();
excelBuf.append("编号").append("\t").append("国家").append("\t").append("\n");

List<Country> list1 = dao.getCountryData();
for (int i = 0; i < list1.size(); i++) {
Country country = (Country) list1.get(i);
excelBuf.append(country.getId()).append("\t")
.append(country.getName()).append("\t")
.append("\n");
}
String excelString = excelBuf.toString();
excelStream = new ByteArrayInputStream(excelString.getBytes(), 0,
excelString.length());
return "excel";
}

}


[size=medium][color=red]

3.页面提交: [/color][/size]
<form action="<%=path %>/ExportAction.action" enctype="MULTIPART/FORM-DATA" method="post">
<input type="submit" value="导出excel">
</form>


以上就是struts2自带的excel导出功能.其实如果想使导出的excel变得更漂亮,就使用apache的poi包,有以下几类:
poi-3.7-20101029.jar
poi-ooxml-3.7-20101029.jar
poi-ooxml-schemas-3.7-20101029.jar,
poi-scratchpad-3.7-20101029.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值