樂(lè)於JAVA
          幸福人生(J2EE)
          posts - 0,  comments - 5,  trackbacks - 0
          在利用網(wǎng)頁(yè)展示查詢結(jié)果時(shí),經(jīng)常會(huì)遇到要求導(dǎo)出成Excel的需求。采用這種方法可以定制輸出的格式和內(nèi)容(還不支持合并單元格和公式),生成真正的Excel格式(不是csv)的Excel。
          一、struts.xml
          <? xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE struts PUBLIC
              "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
              "http://struts.apache.org/dtds/struts-2.0.dtd">
            
          <struts>

              <constant name="struts.i18n.encoding" value="UTF-8"/>

              <package name="demo" extends="struts-default">
                  <action name="excel" method="execute" class="demo.ExcelAction">
                          <result name="excel" type="stream">
                              <param name="contentType">application/vnd.ms-excel</param>    <!-- 注意這里的ContentType -->
                              <param name="inputName">excelStream</param>                   <!-- 這里需要和Action里的變量名一致 -->
                              <param name="contentDisposition">filename="standard.xls"</param>
                              <param name="bufferSize">1024</param>
                          </result>
                  </action>
              </package>
          </struts>

          二、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 Java").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頁(yè)面

          <%@ taglib prefix="s" uri="/struts-tags"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
           <head>
            <s:head />
           </head>

           <body>

              <s:form action="" method="post">
                 <s:submit key="button.submit"/>
              </s:form>
           </body>
          </html>

          posted on 2008-09-19 09:57 李建軍 閱讀(414) 評(píng)論(0)  編輯  收藏 所屬分類: struts2

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(1)

          文章分類

          文章檔案

          搜索

          •  

          最新評(píng)論

          主站蜘蛛池模板: 阳谷县| 龙海市| 洛川县| 维西| 湘阴县| 洪洞县| 惠来县| 乌鲁木齐县| 会东县| 台北县| 汾西县| 锡林浩特市| 江西省| 濮阳县| 临沧市| 彰化县| 丹巴县| 天峨县| 宜昌市| 阜宁县| 宜兴市| 平乡县| 仪征市| 昭苏县| 凤冈县| 北宁市| 镇巴县| 城口县| 汪清县| 吴川市| 鸡西市| 昭平县| 广西| 乌恰县| 云和县| 武冈市| 贵港市| 晴隆县| 玉田县| 灵丘县| 安溪县|