<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Kela's Blog

                前面的路很坎坷,但畢竟是條路.也許走過這一段就會發現,走過去就是夢想中的地方.因此堅持成為此刻唯一能做且必須去做的事情.
    posts - 9, comments - 27, trackbacks - 0, articles - 15

           在最近的一個項目中需要將一段字符類型的文本存為word,html并要將word的內容保存在數據庫中,于是就有了如下的一個工具類,希望能對碰到這樣需求的朋友提供點幫助。
           匆匆忙忙的就copy上來了,沒有做一些刪減,有一些多余的東西,有興趣的朋友可以自行略去。我的注釋相對比較清楚,可以按照自己的需求進行組合。
          在操作word的地方使用了jacob(jacob_1.9),這個工具網上很容易找到,將jacob.dll放置系統Path中,直接放在system32下也可以,jacob.jar放置在classPath中。


    代碼如下:WordBridge.java

    /**
     * WordBridge.java
     */
    package com.kela.util;

    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.Variant;
    import com.kela.db.PoolingDataSource;

    /**
     * 說明: 對word的操作 <p>
     *
     * @author   kela.kf@gmail.com
     */
    public class WordBridge {
     
       Log log = LogFactory.getLog("WordBridgt");
     
       private ActiveXComponent MsWordApp = null; 
       private Dispatch document = null;
       
        /**
         * 打開word
         * @param makeVisible, true顯示word, false不顯示word
         */
        public void openWord(boolean makeVisible) {
           if (MsWordApp == null) {
             MsWordApp = new ActiveXComponent("Word.Application");
           }
      
           Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible));
        }

        /**
         * 創建新的文檔
         *
         */
        public void createNewDocument() {
           Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
           document = Dispatch.call(documents, "Add").toDispatch();
        }

        /**
         * 關閉文檔
         */
        public void closeDocument() {
          // 0 = wdDoNotSaveChanges
          // -1 = wdSaveChanges
          // -2 = wdPromptToSaveChanges
          Dispatch.call(document, "Close", new Variant(0));
          document = null;
        }

        /**
         * 關閉word
         *
         */
        public void closeWord() {
           Dispatch.call(MsWordApp, "Quit");
           MsWordApp = null;
           document = null;
        }
     
        /**
         * 插入文本
         * @param textToInsert 文本內容
         */
        public void insertText(String textToInsert) {
           Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
           Dispatch.put(selection, "Text", textToInsert);
        }

        /**
         * 保存文件
         * @param filename
         */
        public void saveFileAs(String filename) {
          Dispatch.call(document, "SaveAs", filename);
        }

        /**
         * 將word轉換成html
         * @param htmlFilePath
         */
        public void wordToHtml(String htmlFilePath) {
             Dispatch.invoke(document,"SaveAs", Dispatch.Method, new Object[]{htmlFilePath,new Variant(8)}, new int[1]);
         }

        /**
         * 保存word的同時,保存一個html
         * @param text 需要保存的內容
         * @param wordFilePath word的路徑
         * @param htmlFilePath html的路徑
         * @throws LTOAException
         */
        public void wordAsDbOrToHtml(String text, String wordFilePath, String htmlFilePath) throws LTOAException {
      
          try {
             openWord(false);
             createNewDocument();
             insertText(text);
             saveFileAs(wordFilePath);
             wordToHtml(htmlFilePath);
         } catch (Exception ex) {
             log.error("錯誤 - 對word的操作發生錯誤");
             log.error("原因 - " + ex.getMessage());
             throw new LTOAException(LTOAException.ERR_UNKNOWN, "對word的操作發生錯誤("
                        + this.getClass().getName() + ".wordAsDbOrToHtml())", ex);
         } finally {
             closeDocument();
             closeWord();
         }
      
       }

       /**
        * 將word保存至數據庫
        * @param wordFilePath
        * @param RecordID
        * @throws LTOAException
        */
        public void wordAsDatabase(String wordFilePath, String RecordID) throws LTOAException {

           Connection conn = null;
           PreparedStatement pstmt = null;
           PoolingDataSource pool = null;
           
           File file = null;
        
           String sql = "";
           try {
               sql = " UPDATE Document_File SET FileBody = ? WHERE RecordID = ? ";
                
               pool = new PoolingDataSource();
               conn = pool.getConnection();
            
               file = new File(wordFilePath);
               InputStream is = new FileInputStream(file);
               byte[] blobByte = new byte[is.available()];
               is.read(blobByte);
               is.close();

              pstmt = conn.prepareStatement(sql);
              pstmt.setBinaryStream(1,(new ByteArrayInputStream(blobByte)), blobByte.length);
              pstmt.setString(2, RecordID); 
              pstmt.executeUpdate();
         
            } catch (Exception ex) {
                log.error("錯誤 - 表 Document_File 更新數據發生意外錯誤");
                log.error("原因 - " + ex.getMessage());
                throw new LTOAException(LTOAException.ERR_UNKNOWN,
                       "表Document_File插入數據發生意外錯誤("
                        + this.getClass().getName() + ".wordAsDatabase())", ex);
             } finally {
                 pool.closePrepStmt(pstmt);
                 pool.closeConnection(conn);
            }
        }
     
       /**
        * 得到一個唯一的編號
        * @return 編號
        */
       public String getRecordID() {
      
         String sRecordID = "";
      
         java.util.Date dt=new java.util.Date();
            long lg=dt.getTime();
            Long ld=new Long(lg);
            sRecordID =ld.toString();
           
            return sRecordID;
        }
     
        /**
         * 得到保存word和html需要的路徑
         * @param systemType 模塊類型 givInfo, sw, fw
         * @param fileType 文件類型 doc, html
         * @param recID 文件編號
         * @return 路徑
         */
         public String getWordFilePath(String systemType, String fileType, String recID) {
      
           String filePath = "";
      
           File file = new File(this.getClass().getResource("/").getPath());

           filePath = file.getPath().substring(0, file.getPath().length() - 15);
           
           if(systemType.equalsIgnoreCase("govInfo")) {
       
               if(fileType.equalsIgnoreCase("doc"))
                   filePath = filePath + "/uploadFiles/govInfo/document/" + recID + ".doc";
                else if(fileType.equalsIgnoreCase("htm"))
                   filePath = filePath + "/HTML/govInfo/" + recID + ".htm";
            } else if(systemType.equalsIgnoreCase("sw")){
                if(fileType.equalsIgnoreCase("doc"))
                  filePath = filePath + "/uploadFiles/sw/document/" + recID + ".doc";
                else if(fileType.equalsIgnoreCase("htm"))
                  filePath = filePath + "/HTML/sw/" + recID + ".htm";
             } else if(systemType.equalsIgnoreCase("fw")) {
                  if(fileType.equalsIgnoreCase("doc"))
                     filePath = filePath + "/uploadFiles/fw/document/" + recID + ".doc";
                  else if(fileType.equalsIgnoreCase("htm"))
                     filePath = filePath + "/HTML/fw/" + recID + ".htm";
             }
      
            return filePath;
        }
    }

    Feedback

    # re: java生成word,html文件并將內容保存至數據庫  回復  更多評論   

    2006-03-02 14:01 by chinyon
    你的文章給了我很大的啟發。但我下載不到1.9的版本,用1.7的時候出現很多問題,請問能否發給我一份。
    qy@mail.nwpu.edu.cn
    qq:27551283
    十分感謝。

    # re: java生成word,html文件并將內容保存至數據庫  回復  更多評論   

    2006-10-09 10:58 by 小徐
    thanks

    # re: java生成word,html文件并將內容保存至數據庫  回復  更多評論   

    2007-07-30 08:33 by 安利
    您的博客頂強,以后得多來學習下!

    # re: java生成word,html文件并將內容保存至數據庫  回復  更多評論   

    2008-01-18 19:07 by 浪子
    哥們,要是把模板中圖片也生成出來呢?不顯示而是一個空的框框,請教下,有誰知道怎么辦呢?謝謝!我的郵件是 wangl@95068.com

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    分享到:
    主站蜘蛛池模板: 成年免费a级毛片免费看无码| 亚洲日韩一区二区三区| 九一在线完整视频免费观看| 免费在线观看日韩| 自拍偷自拍亚洲精品偷一| 精品国产免费一区二区| 亚洲成AV人影片在线观看| 99久久综合国产精品免费| 亚洲性无码一区二区三区| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 国产亚洲一卡2卡3卡4卡新区| 成人免费在线视频| 黄人成a动漫片免费网站| 亚洲午夜成人精品电影在线观看| yy一级毛片免费视频| 亚洲精品无码mv在线观看网站| 一个人免费视频观看在线www | 亚洲午夜成人精品电影在线观看| 国产免费黄色无码视频| 国产精品亚洲аv无码播放| 67194成手机免费观看| 亚洲va久久久久| 国产免费av一区二区三区| 九九热久久免费视频| 亚洲一区二区三区四区在线观看| 国产人在线成免费视频| 久久亚洲精品无码av| 亚洲开心婷婷中文字幕| 免费观看黄色的网站| 亚洲AV无码一区二区一二区| 中文字幕亚洲第一| 国产92成人精品视频免费| 亚洲AV永久无码精品放毛片| 中文字幕无码精品亚洲资源网| 国产福利视精品永久免费| 激情吃奶吻胸免费视频xxxx| 亚洲av午夜成人片精品网站 | 国产大片线上免费看| 国产一区二区三区免费观看在线| 亚洲人成人77777在线播放| 亚洲精品无码AV中文字幕电影网站|