Chinaunix首页 | 论坛 | 博客
  • 博客访问: 534742
  • 博文数量: 855
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 19:08
文章分类

全部博文(855)

文章存档

2011年(1)

2008年(854)

我的朋友

分类:

2008-10-16 19:20:16

    用网页展示查询结果,经常会遇到要求导出成Excel的需求。采用这种方法可以定制输出的格式和内容(还不支持合并单元格和公式),生成真正的Excel格式(不是csv)的Excel。一、struts.xml    
       

                                                       application/vnd.ms-excel                         excelStream                                        filename="standard.xls"                     1024                              

    二、Struts2的 Action

    package demo; public class ExcelAction {     private InputStream excelStream; // 需要生成getter和setter

        public String execute() throws Exception {         StringBuffer excelBuf = new StringBuffer();         excelBuf.append("BookName").append("\t").append("Year").append("\t").append("author").append("\n");         excelBuf.append("Thinking in ").append("\t").append("2001").append("\t").append("Eckel").append("\n");         excelBuf.append("Spring in action").append("\t").append("2005").append("\t").append("Rod").append("\n");         String excelString = excelBuf.toString();         logger.debug("result excel String: " + excelString);         excelStream = new ByteArrayInputStream(excelString.getBytes(), 0, excelString.length());         return "excel";     }

        // getter and setter     ... }

    三、Jsp页面

    <%@ taglib prefix="s" uri="/struts-tags"%>       

    

                      

【责编:landy】

--------------------next---------------------

阅读(177) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~