1. <li id="3se62"></li>
      <th id="3se62"></th>

      <progress id="3se62"></progress>
         你好,歡迎來到電腦編程技巧與維護雜志社! 雜志社簡介廣告服務讀者反饋編程社區  
        合訂本訂閱
         
         
        您的位置:雜志經典 / 網絡與通信
        在線生成Word文檔的實現與應用(三)
         

        1 開發過程

        1.1 Web端在線文檔生成樁模塊

        1.1.1 在線文檔生成樁模塊頭部定義

        在線文檔生成的樁模塊是JSP頁面,其需要調用后臺的文檔生成器對象,所以需要在JSP頭部對所包含的庫進行定義,JSP頁面的頭部定義如代碼1所示。

        代碼1 在線文檔生成樁模塊的頭部定義

        文件名:stub.jsp

        1

        2

        3

        4

        5

        6

        7

        <%@ page language="java" import="java.io.*,foolstudio.bi.*" pageEncoding="gb2312"%>

        <%

               final String path = request.getContextPath();

               final String basePath = request.getScheme()+"://"+request.getServerName()+

                                                  ":"+request.getServerPort()+path+"/";

               final String baseRealPath = this.getServletContext().getRealPath("/");

        %>

            需要注意的是,包含代碼1中“foolstudio.bi”包的Jar文檔需要分發在當前域的“WEB-INF\lib”文件夾中,另外JACOB相關的庫文件也必須分發在該文件夾中。

        1.1.2 在線文檔生成樁模塊的主體

        代碼2中是在線文檔生成樁模塊的主體代碼,其中主要是調用后臺的文檔生成器對象,生成目標文檔,并將其URL推送到前端頁面。

        代碼2 在線文檔生成樁模塊的主體代碼

        文件名:stub.jsp

        1

        2

        3

        4

        5

        6

        7

        8

        9

        10

        11

        12

        13

        14

        15

        16

        17

        18

        19

        20

        21

        22

        23

        24

        25

        26

        27

        28

        29

        30

        31

        <%

               String template = request.getParameter("template");

         

               if(template == null) {

                      out.println("<p>無效的文檔模板!</p>");

                      return;

               }

              

               response.setCharacterEncoding("gb2312");

               response.setContentType("text/html");

               //

               String templatePath = baseRealPath + "templates/";

               String outputPath = baseRealPath + "temp/";

              

               DocBuilder builder = new DocBuilder(template, baseRealPath);

               builder.saveAs(templatePath, outputPath);

              

               String outputRealFile = outputPath+builder.getOutput();

               String outputFile = basePath+"temp/"+builder.getOutput();

              

               File aFile = new File(outputRealFile);

               if(aFile.exists() ) {

                      out.println("<p>Word報告生成成功!</p>");

                      out.println("<p><a href=\"" + outputFile + "\">查看或下載</a></p>");

               out.println("<p><a href=\"#\" onclick=\"window.close();\">關閉本窗體</a></p>");

               }

               else {

                      out.println("<p>抱歉!報告生成失敗,請與管理員聯系!</p>");

               out.println("<p><a href=\"#\" onclick=\"window.close();\">關閉本窗體</a></p>");

               }

        %>

         

        1.2 文檔生成器

        1.2.1 配置文件

        代碼3是文檔生成器所依賴的配置文件,其中定義了數據庫的連接信息、文檔模板與數據源的對應關系和數據源的定義。

        代碼3 配置文件

        文件名:db.properties

        1

        2

        3

        4

        5

        6

        7

        URL=jdbc:oracle:thin:@localhost:1521/FOO

        USER=foo

        PASSWD=********

        staff_ds=ds1

        ds1=select STA_ID,STA_XM,STA_XB,to_char(STA_SR,'yyyy-mm-dd') STA_SR,

        to_char(STA_RZRQ,'yyyy-mm-dd') STA_RZRQ,

        STA_ZGXL,STA_BYXX,STA_BM,STA_GW from tab_staff

         

        1.2.2 文檔生成器初始化

        代碼4是文檔生成器初始化代碼,其主要內容是根據文檔模板獲取對應的數據源列表,并將所有數據源中的數據讀出并存放到數據集容器中。

        代碼4 初始化文檔生成器

        文件名:DocBuilder.java

        1

        2

        3

        4

        5

        6

        7

        8

        9

        10

        11

        12

        13

        14

        15

        16

        17

        18

        19

        20

        21

        22

        23

        24

        25

        26

        27

        28

        29

        30

        31

        public DocBuilder(String templateId, String ctxPath) {

               this.mTemplateId = templateId;

              

               Properties props = new Properties();

               String dsList = null;

               try {

                      props.load(new FileInputStream(ctxPath+DB_SETTING));

                      dsList = props.getProperty(templateId+"_"+DS_PREFIX);

               } catch (FileNotFoundException e) {

                      FooDebug.getInstance().println(e.getMessage());

                      return;

               } catch (IOException e) {

                      FooDebug.getInstance().println(e.getMessage());

                      return;

               }     

              

               if(dsList == null) {

                      FooDebug.getInstance().println("無效的文檔模板!");

                      return;

               }            

              

               //構造結果集容器

               this.mResuleSet = new HashMap<String,ArrayList<ArrayList<String>>>();

              

               try {

                      //初始化結果集容器

                      initResultSet(dsList, props);

               } catch (SQLException e) {

                      e.printStackTrace();

               }

        }

            代碼4中,存放結果集的容器是一個以結果集名為鍵,以二維數組為值的散列容器(第23行),主要是考慮到一個文檔模板中可能包含多個結果集。

        1.2.3 初始化結果集容器

        初始化結果集容器主要內容是根據數據源的定義從后臺數據庫中提取數據并填充到結果集容器中,代碼5是初始化結果集容器的主要代碼。

        代碼5 初始化結果集容器

        文件名:DocBuilder.java

        1

        2

        3

        4

        5

        6

        7

        8

        9

        10

        11

        12

        13

        14

        15

        16

        17

        18

        19

        20

        21

        22

        23

        24

        25

        26

        27

        28

        29

        30

        31

        32

        33

        34

        35

        36

        37

        38

        39

        40

        41

        42

        43

        44

        45

        46

        47

        48

        49

        50

        51

        52

        //初始化結果集

        private void initResultSet(String dsList, Properties props) throws SQLException {

               //獲取數據庫配置

               final String url = props.getProperty("URL");

               final String user = props.getProperty("USER");

               final String passwd = props.getProperty("PASSWD");

              

               if(url==null || user==null || passwd==null) {

                      return;

               }            

               //判斷Oracle驅動是否存在

               if(FooDb.getInstance().isFoundOracleDriver()==false) {

                      return;

               }            

              

               Connection conn = FooDb.getInstance().getConnector(url, user, passwd);

               Statement stat = FooDb.getInstance().getQueryStat(conn);

               //分割所有數據源

               final String[] dss = dsList.split(",");      

               //遍歷所有數據源

               for(int i = 0; i < dss.length; ++i) {

                      String sql = props.getProperty(dss[i]);

                     

                      ResultSet rs = FooDb.getInstance().execQuery(stat, sql);

                      int colsCount = FooDb.getInstance().getColsCount(rs);

                      //定義單數據源的記錄行

                      ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();

                      //表頭各列

                      /*

                      ArrayList<String> header = new ArrayList<String>();

                      for(int i = 1; i <= colsCount; ++i) {

                             header.add(FooDb.getInstance().getColName(rs, i));

                      }

                      rows.add(header);

                      */

                      //遍歷各行

                      while(rs.next()) {

                             ArrayList<String> row = new ArrayList<String>();

                             for(int j = 1; j <= colsCount; ++j) {

                                    row.add(rs.getString(j));

                             }

                             rows.add(row);

                      }

                      FooDb.getInstance().closeRs(rs);

                     

                      //將單個結果集存入到結果集容器

                      this.mResuleSet.put(dss[i], rows);              

               }

               //關閉語句對象和數據庫連接

               FooDb.getInstance().closeStat(stat);

               FooDb.getInstance().closeConn(conn);

        }

               代碼5中,文檔解析器分割指定文檔模板所對應的數據源列表(第19行),然后逐一讀取數據源中的記錄行(從第21行到第48行),最后將單個結果集存入到結果集容器中(第47行)。

               這種首先將所有數據源的數據加載到內存的方式可能會對系統內存的占用造成一定的壓力,但是對于后面的數據檢索而言,會顯著提高檢索效率。對于此處需要開發者按照實際的情況進行權衡。

         

         

          推薦精品文章

        ·2023年7月目錄
        ·2023年6月目錄 
        ·2023年5月目錄
        ·2023年4月目錄 
        ·2023年3月目錄 
        ·2023年2月目錄 
        ·2023年1月目錄 
        ·2022年12月目錄 
        ·2022年11月目錄 
        ·2022年10月目錄 
        ·2022年9月目錄 
        ·2022年8月目錄 
        ·2022年7月目錄 
        ·2022年6月目錄 

          聯系方式
        TEL:010-82561037
        Fax: 010-82561614
        QQ: 100164630
        Mail:gaojian@comprg.com.cn

          友情鏈接
         
        Copyright 2001-2010, www.1wcdp.top, All Rights Reserved
        京ICP備14022230號-1,電話/傳真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
        地址:北京市海淀區遠大路20號寶藍大廈E座704,郵編:100089 
        1. <li id="3se62"></li>
          <th id="3se62"></th>

          <progress id="3se62"></progress>
            操美女小骚逼