日期:2014-05-17 浏览次数:20781 次
<action name="toExcel" class="boss-action" method="toExcel"> <result name="excel" type="stream"> <param name="contentType">application/vnd.ms-excel</param> <param name="inputName">excelStream</param> <param name="contentDisposition">filename="export.xls"</param> <param name="bufferSize">1024</param> </result> </action>
jar全了没 1.所需jar包 struts2需要jar包:commons-logging.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.1.jar、xwork-2.0.4.jar excel导出:jxl.jar struts.xml; 3.<struts> 4. <package name="platform-default" extends="struts-default"> 5. <action name="excel" class="action.ExcelAction"> 6. <result name="excel" type="stream"> 7. <param name="contentType"> 8. application/vnd.ms-excel 9. </param> 10. <param name="inputName">excelStream</param> 11. <param name="contentDisposition"> 12. filename="export.xls" 13. </param> 14. <param name="bufferSize">1024</param> 15. </result> 16. </action> 17. </package> 18.</struts>
------解决方案--------------------
导出excel不用那么麻烦配置,直接在action中写一个方法就可以实现这个功能
代码:
public String action_executeExcel(){
try{
List<JWJX_JXdajbxx> list = teachDossierService.findAll();
getDmWjlxdmAll();
getDmBgqxAll();
//设置导出头部
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
response.setContentType("application/vnd.ms-excel");
String fileName = java.net.URLEncoder.encode("教学档案.xls", "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("UTF-8"), "GBK"));
//组装Excel
OutputStream os = response.getOutputStream();
WritableWorkbook book = Workbook.createWorkbook(os);
WritableSheet sheet = book.createSheet("教学档案", 0);
String titles[] = new String[] { "分类编号", "文件类型", "文件名称","归档范围","保管期限"};
String properties[] = new String[] { "flbh", "wjlx", "wjmc","gdfw", "bgqx"};
for (int i = 0; i < titles.length; i++) {
sheet.addCell(new Label(i, 0, titles[i]));
}
if(list != null && list.size()>0){
Iterator i = list.iterator();
int line = 1;
while (i.hasNext()) {
JWJX_JXdajbxx tsc = (JWJX_JXdajbxx) i.next();
for (int j = 0; j < properties.length; j++) {
Object value = PropertyUtils.getSimpleProperty(tsc,properti