Login  

Blog Stats

News


Visual Developer - Visual Basic MVP

隨筆分類

文章分類

每月文章

優質好站連結


強力鎯頭 の VB 部落

歡迎蒞臨 Power Hammer の VB 部落 ! J 裡會放一些 VB .Net Oracle Crystal Report 等相關資訊分享給大家囉..

 

ASP 如何讀取 Word 檔案內容並顯示於網頁

 

一般而言 ASP ASP.Net 中透過 CreateObject 建構函數建立 Word 物件

會有安全性及使用權限上的問題 因此若 虛擬目錄 不使用 整合 Windows 驗證

將無法存取 Word doc 更不用說虛擬目錄以外的目錄  好比說 C:\ 根目錄下的 Word 檔。

 

底下介紹個方式,給大家參考看看:

 

l            使用 VB6

n            建立專案,選擇 ActiveX DLL

n            將專案的 Name屬性設定成 AxClass Name 屬性設為 Word

n            編輯程式碼如下 :

n           

Public Function GetDocContent(strFile As String) As String

    Dim wdObj As Object ' 宣告

    Set wdObj = CreateObject("Word.Application") ' 個體化 Word 物件

    With wdObj

        .Documents.Open strFile ' 開啟 Word

        GetDocContent = .ActiveDocument.Content ' 讀出 Word 內容囉

        ' 底下關掉 Word , 釋放資源

        On Error Resume Next

        .ActiveDocument.Close

        .ActiveWindow.Close

        .Quit

    End With

    Set wdObj = Nothing

End Function

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

n            編譯 製成 DLL

 

Compiler 完成後請使用 RegSvr32.exe 將該 Dll 元件 "反註冊" , 如

    RegSvr32 /u "路徑+檔名.dll"

 

 RegSvr32.exe 工具使用 可參考:

<< 關於 ActiveX (OLE) 元件登錄註冊 >>

http://blog.blueshop.com.tw/hammerchou/archive/2006/04/06/20787.aspx 

 

 

l            執行 DCOMCNFG.EXE -> [確定]

DCOM Config 圖

 

 

n            COM+應用程式 -> 滑鼠右鍵 -> 新增 -> 應用程式

COM+應用程式 圖

 

 

n            [下一步] -> 建立空的應用程式

應用程式 圖

 

n            輸入應用程式名稱 -> 伺服應用程式 -> [下一步]

 

伺服器應用程式 圖

 

 

n            使用下列使用者 -> 使用者 -> 密碼 / 確認密碼 -> [下一步] -> [完成]

u          輸入 Administrator 及密碼

指定使用者 圖

 

 

n            AxWord -> 元件 -> 滑鼠右鍵 -> 新增 -> 元件 -> [下一步]

新增元件 圖

 

 

n            [安裝新元件]

安裝新元件 圖

 

 

n            選取 先前用 VB6 編譯製成的 DLL

選 DLL 檔 圖

 

 

n            [下一步] -> [完成]

完成 圖

 

 

l            ASP Code 如下:

n           

<%

' 宣告

Dim wd

' 建立先前寫的 DLL 物件 , 個體化

Set wd = Server.CreateObject("AX.Word")

' 執行 Dll 中的 GetDocContent 方法讀 Word 內容

Response.Write wd.GetDocContent("C:\1.doc")

%>

 

 

 

 

 

 

 

 

 

 

 

================================================================

 

 

以上方式是使用 VB6 Word 物件 作動 的部份 寫成 ActiveX Dll

在放到元件服務裡的 COM+ 中,並指定 Administrator 去執行,以避開安全性上的權限問題;

倘若手邊沒有 VB6開發工具呢 底下介紹 WSC 的方式 ,只要文字檔不需 VB6 囉 !

 

 

WSC ( Windows Script Component )

 

l            建立一新 文字檔

n            編輯程式碼如下 :

n           

<?xml version="1.0"?>

<component>

    <registration

        description="PH ActiveX Word Windows Script Component"

        progid="AxWsc.Word"

        version="1.00"

        classid="{5F644CD7-E1D4-4D54-A260-B4CCC2F540FC}">

    </registration>

    <public>

        <method name="GetDocContent">          

        </method>

    </public>

    <script language="VBScript">

        <![CDATA[

            Function GetDocContent(strFile)

                Dim wdObj

                Set wdObj = CreateObject("Word.Application")

                With wdObj

                    .Documents.Open strFile

                    GetDocContent = .ActiveDocument.Content

                    On Error Resume Next

                    .ActiveDocument.Close

                    .ActiveWindow.Close

                    .Quit

                End With

                Set wdObj = Nothing

            End Function

        ]]>

    </script>

</component>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

n            存檔 命名為 AxWord.wsc ( 注意副檔名為 WSC )

 

 

n            選取檔案 -> 滑鼠右鍵 -> 註冊 -> 出現註冊 是否成功 的訊息 -> [確定]

註冊 圖

 

 

n            選取檔案 -> 滑鼠右鍵 -> 建立型態程式庫

u          ( 會產生一 ScriptLet.tlb Type Library 檔案 )

型態程式庫 圖

 

 

n            之後如同 ActiveX Dll 安裝於元件服務中的動作

 

n            直到 [安裝新元件] 時,請選擇 ScriptLet.tlb 檔案

 

n            完成後畫面如下 :

完成 圖

 

 

  

l            ASP Code 如下:

n           

<%

' 宣告

Dim wd

' 建立先前寫的 DLL 物件 , 個體化

Set wd = Server.CreateObject("AxWsc.Word")

' 執行 Dll 中的 GetDocContent 方法讀 Word 內容

Response.Write wd.GetDocContent("C:\1.doc")

%>

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on Friday, November 03, 2006 2:55 PM

What People Are Saying About This Post...

# re: ASP 如何讀取 Word 內容顯示於網頁 12/10/2006 11:09 AM 窃听器
OK

# re: ASP 如何讀取 Word 內容顯示於網頁 12/14/2006 10:28 AM 硬盘数据恢复
GOOD

# re: ASP 如何讀取 Word 內容顯示於網頁 3/6/2007 12:30 PM 数据恢复
zhouping7879@sohu.com

TODAY I LOOK A GOOD ARCHIVES!!

# re: ASP 如何讀取 Word 內容顯示於網頁 3/17/2007 3:45 PM 数据恢复

zhouping7879@sohu.com

TODAY I LOOK A GOOD ARCHIVES!!

# re: ASP 如何讀取 Word 內容顯示於網頁 5/11/2007 3:07 PM 硬盘数据恢复
建立先前寫的 DLL 物件 , 個體化


# re: ASP 如何讀取 Word 內容顯示於網頁 7/13/2007 11:03 AM Eric
請問,
一般不是註冊完元件就可以使用了,
後續登錄COM的作用是什麼?

# re: ASP 如何讀取 Word 內容顯示於網頁 8/31/2007 3:53 PM 租車
TODAY I LOOK A GOOD ARCHIVES!!

# re: ASP 如何讀取 Word 內容顯示於網頁 9/26/2007 12:01 PM Power Hammer
Eric 您好
本篇文章主要是要使用 Word.Application
如果直接在 ASP 中 CreateObject("Word.Applicatioin")
就沒辦法指定權限,會有安全性的問題囉


# 经济酒店 1/9/2008 5:36 PM 经济酒店
经济酒店

# re: ASP 如何讀取 Word 內容顯示於網頁 3/31/2008 5:52 PM 包裝公司
Good articles!

# re: ASP 如何讀取 Word 內容顯示於網頁 6/17/2008 12:22 AM egg
請問大大~
我成功讀取內容了~
可是發現內容中無法讀取到word裡的項目符號
請問有什麼方法可以解決嗎?
因為要原先想用前面的數字符號來判斷是否為下一題目
謝謝大大的分享^^

情況如下:
◎原word的內容:
1. 在catch區塊的執行順序上,下列何者應是最後才執行? (A)IOException (B)EOFException (C)Exception (D)FileNotFoundException
2. try{
int x=10 / 0;
System.out.println(“x=” + x);
}
Catch(Exception e){
System.out.println(“In Exception: “ + e.getMessage() );
}
以上的程式片斷之結果為:(A)In Exception: / by zero (B)x=0; (C)x=null; (D)編譯錯誤



◎讀取出來的內容:
在catch區塊的執行順序上,下列何者應是最後才執行? (A)IOException (B)EOFException (C)Exception (D)FileNotFoundException
try{
int x=10 / 0;
System.out.println(“x=” + x);
}
Catch(Exception e){
System.out.println(“In Exception: “ + e.getMessage() );
}
以上的程式片斷之結果為:(A)In Exception: / by zero (B)x=0; (C)x=null; (D)編譯錯誤


# re: ASP 如何讀取 Word 內容顯示於網頁 6/13/2009 5:15 PM liaoyizhi
请问如果服务器上没有安装OFFICE软件,是否无法运行?

# re: ASP 如何讀取 Word 內容顯示於網頁 6/25/2009 11:09 AM Hammer
是的,Server上需要有安裝Office Word

What do you have to say?

Title:
Name:
Url:
驗證碼  
Comments: