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

  
一般而言 ASP ASP.Net 中透过 CreateObject 建构函数建立 Word 对象
会有安全性及使用权限上的问题 因此若 虚拟目录 不使用 整合 Windows 验证
将无法存取 Word doc ,更不用说虚拟目录以外的目录  好比说 C:/ 根目录下的 Word 文件。
 
底下介绍个方式,给大家参考看看 :
 
l             使用 VB6
n             建立项目,选择 ActiveX DLL
n             将项目的 Name 属性设定成 Ax Class 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) 组件登录注册 >>
 
 
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")
%>
 

 
 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值