ASP 如何读取 Word 档案内容并显示于网页

来源:互联网 发布:桔子瑜伽怎么样 知乎 编辑:程序博客网 时间:2024/04/27 18:00
  
一般而言 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 -> [确定]
 
 
n            COM+应用程序 -> 鼠标右键 -> 新增 -> 应用程序
 
 
n            [下一步] -> 建立空的应用程序



 
n            输入应用程序名称 -> 伺服应用程序 -> [下一步]
 
 
 
n            使用下列使用者 -> 使用者 -> 密码 / 确认密码 -> [下一步] -> [完成]
u          输入 Administrator 及密码
 
 
n            AxWord -> 组件 -> 鼠标右键 -> 新增 -> 组件 -> [下一步]
 
 
n            [安装新组件]
 
 
n            选取先前用 VB6 编译制成的 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")
%>