操作WORD配置說明
引入:Word的物件程式庫檔案“MSWORD.OLB”(word 2000為MSWORD9.OLB)
1.運行Dcomcnfg.exe
2.元件服務――電腦――我的電腦――DCOM配置――找到microsoft word 文檔
3.點擊屬性
4.選擇“安全性”
5.選定“使用自訂存取權限”和“使用自訂啟動許可權”
6.分別編輯許可權,添加Everyone(ASPNET,VS Developers,Debugger User)
7.選擇“身份標識”,在選定“互動式使用者” 即可
8.在Web.config裡加 <identity impersonate="true"/>
C#:
ASP.NET操作Word文檔一直是一個大家比較關心的話題,其實在ASP.NET裡操作Word文檔一點也不難,大家只需按本文提示,就能輕輕鬆鬆操作Word文檔!
一、準備工作
首先請確認服務端已經安裝了Office Word(以下將以Office XP為例),作業系統為win2000或XP,並且已配置好.NET的運行環境及安裝VS.NET C#開發環境後,我們就可以開啟VS.NET,並建立一個Visual C#項目>ASP.NET Web應用程式,位置為“http://localhost/word”。(一)
二、引用Word物件程式庫檔案
要操作Word,我們就需要Word的物件程式庫檔案“MSWORD.OLB”(word 2000為MSWORD9.OLB),通常安裝了Office Word後,你就可以在office安裝目錄的Office10檔案夾下面找到這個檔案,當我們將這個檔案引入到項目後,我們就可以在源碼中使用各種操作函數來操作Word。具體做法是開啟功能表列中的項目>添加引用>瀏覽,在開啟的“選擇組件”對話方塊中找到MSWORD.OLB後按確定即可引入此物件程式庫檔案,vs.net將會自動將庫檔案轉化為DLL組件,這樣我們只要在源碼中建立該組件對象即可達到操作Word的目的!
三、Webform1.aspx.cs代碼
完成添加引用後,MSWORD.OLB已經轉化為相關DLL檔案並放置於項目的BIN目錄下了,這樣我們只需在源碼中建立該對象,並使用word庫檔案內建的操作函數即可輕鬆實現操作Word,Webform1.aspx.cs源碼如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace word
{
/// <summary>
/// Webform1 的摘要說明。
/// </summary>
public class Webform1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox SaveAs;
protected System.Web.UI.WebControls.Button Button;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label result;
protected System.Web.UI.WebControls.TextBox wordText;
#region Web form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:該調用是 ASP.NET Web Form設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void Button_Click(object sender, System.EventArgs e)
{
Object Nothing=System.Reflection.Missing.value;
//取得Word檔案儲存路徑
object filename=@SaveAs.Text;
//建立一個名為WordApp的組件對象
Word.Application WordApp=new Word.ApplicationClass();
//建立一個名為WordDoc的文檔對象
Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//增加一表格
Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,1,1,ref Nothing,ref Nothing);
//在表格第一儲存格中添加自訂的文字內容
table.Cell(1,1).Range.Text=wordText.Text;
//在文檔空白地方添加文字內容
WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";
//將WordDoc文檔對象的內容儲存為DOC文檔
WordDoc.SaveAs(ref filename,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
//關閉WordDoc文檔對象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//關閉WordApp組件對象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回結果
result.Text="文檔路徑:<a href="/"+SaveAs.Text+"'>"+SaveAs.Text+"</a>(點選連結查看)<br/>產生結果:成功!";
}
private void Page_Load(object sender, System.EventArgs e)
{
}
}
}
四、Webform1.aspx代碼
完成CS源碼後,我們就可以設計Webform頁面了,完整的代碼如下:
<%@ Page language="c#" Codebehind="Webform1.aspx.cs" AutoEventWireup="false" Inherits="word.Webform1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>基於Webforms的操作Word</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="javascript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body ms_positioning="GridLayout">
<form id="form1" method="post" runat="server">
<FONT face="宋體">
<asp:TextBox id="wordText" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 129px" runat="server" Height="190px" Width="360px" TextMode="MultiLine"></asp:TextBox>
<asp:TextBox id="SaveAs" style="Z-INDEX: 102; LEFT: 143px; POSITION: absolute; TOP: 80px" runat="server" Width="360px">C:\myword.doc</asp:TextBox>
<asp:Button id="Button" style="Z-INDEX: 103; LEFT: 237px; POSITION: absolute; TOP: 340px" runat="server" Width="98px" on onClick="Button_Click" Text="產生Word文檔"></asp:Button>
<INPUT style="Z-INDEX: 104; LEFT: 361px; WIDTH: 49px; POSITION: absolute; TOP: 340px; HEIGHT: 24px" type="reset" value="重填" size="20"></FONT>
<FONT face="宋體">基於Webforms的操作Word(小寶.NET)</FONT>
<asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 143px; POSITION: absolute; TOP: 54px" runat="server" Width="187px" Height="18px">Word檔案儲存路徑:</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 142px; POSITION: absolute; TOP: 107px" runat="server" Width="159px" Height="12px">Word檔案內容:</asp:Label>
<asp:Label id="result" style="Z-INDEX: 107; LEFT: 148px; POSITION: absolute; TOP: 387px" runat="server" Width="352px" Height="18px" ForeColor="Red"></asp:Label>
</form>
</body>
</HTML>
五、web.config設定
web.config檔案還需添加一句 <identity impersonate="true"/>以啟用類比身份,因為預設ASPNET這個使用者是沒有許可權訪問Word.ApplicationClass(),當啟用類比身份後所有頁面將會使用匿名Internet使用者帳戶(IUSR_machinename)這個使用者名稱的許可權執行,這樣我們就能成功訪問Word.ApplicationClass()並在ASP.NET中操作Word!