操作WORD配置說明
引入:Word的對象庫文件“MSWORD.OLB”(word 2000爲MSWORD9.OLB)(這是針對老版本的情況,在用vs.net2005的時候,直接在引用對話框中,在com組件裏找到對word的庫文件的引用就可以了,文件名好像是一樣的.)
1.運行Dcomcnfg.exe
2.組件服務――計算機――我的電腦――DCOM配置――找到microsoft word 文檔
3.點擊屬性
4.選擇“安全性”
5.選定“使用自定義訪問權限”和“使用自定義啓動權限”
6.分別編輯權限,添加Everyone(ASPNET,VS Developers,Debugger User)(注:其實準確的應該是"ISUR_機器名"這個用戶纔對)
7.選擇“身份標識”,在選定“交互式用戶” 即可
8.在Web.config里加 <identity impersonate="true"/> (這句插在<system.web>段裏就可以了)
一、準備工作
首先請確認服務端已經安裝了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代碼(vs.net2003默認新建頁文件名都爲webform1,在vs.net2005裏已經換成了default1.aspx.cs了)
完成添加引用後,MSWORD.OLB已經轉化爲相關DLL文件並放置於項目的BIN目錄下了,這樣我們只需在源碼中創建該對象,並使用word庫文件內置的操作函數即可輕鬆實現操作Word,Webform1.aspx.cs源碼如下:
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;
///這些引用都可以不要了.在vs.net 2005裏的web.config已經添加了上述引用.
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;
Web form Designer generated code
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頁面了,完整的代碼如下:
<!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!
Public Function wirteWord(ByVal str As String, ByVal title As String) As Boolean
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
'取得Word文件保存路徑
Dim filename As Object = "C:InetpubwwwrootSLOA_NETdocumentDocManage" + title + ".doc"
'創建一個名爲WordApp的組件對象
WordApp = New Word.ApplicationClass
'vs.net 2005裏已經沒有applicationclass了,直接用application即可
'創建一個名爲WordDoc的文檔對象
WordDoc = WordApp.Documents.Add()
'在文檔空白地方添加文字內容
WordDoc.Paragraphs.Last.Range.Text = str
'保存
WordDoc.SaveAs(filename)
'關閉WordDoc文檔對象
WordDoc.Close()
'關閉WordApp組件對象
WordApp.Quit()
Return True
Catch ex As Exception
If Not WordDoc Is Nothing Then
WordDoc.Close()
End If
If Not WordApp Is Nothing Then
WordApp.Quit()
End If
Return False
End Try
End Function
Public Function readWord(ByVal title As String) As String
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:InetpubwwwrootSLOA_NETdocumentDocManage" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
Dim comment As String = WordDoc.Range.Text
WordDoc.Close()
WordApp.Quit()
Return comment
Catch ex As Exception
If Not WordDoc Is Nothing Then
WordDoc.Close()
End If
If Not WordApp Is Nothing Then
WordApp.Quit()
End If
Return Nothing
End Try
End Function
Public Function printWord(ByVal title As String) As Boolean
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:InetpubwwwrootSLOA_NETdocumentDocManage" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
WordApp.Visible = True
'WordApp.ActivePrinter = WordDoc.Range.Text
'WordDoc.Close()
'WordApp.Quit()
Return True
Catch ex As Exception
'If Not WordDoc Is Nothing Then
'WordDoc.Close()
'End If
'If Not WordApp Is Nothing Then
'WordApp.Quit()
'End If
'Return Nothing
End Try
Return False
End Function
Public Function printSetWord(ByVal title As String)
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:InetpubwwwrootSLOA_NETdocumentDocManage" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
WordApp.Visible = True
WordApp.PrintPreview = True
'WordDoc.Close()
'WordApp.Quit()
Catch ex As Exception
'If Not WordDoc Is Nothing Then
'WordDoc.Close()
'End If
'If Not WordApp Is Nothing Then
'WordApp.Quit()
'End If
'Return Nothing
End Try
End Function
Public Function viewWord(ByVal title As String)
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Try
Dim obj As Object = System.Reflection.Missing.Value
Dim filename As Object = "C:InetpubwwwrootSLOA_NETdocumentDocManage" + title + ".doc"
WordApp = New Word.ApplicationClass
WordDoc = WordApp.Documents.Open(filename)
WordApp.Visible = True
WordApp.PrintPreview = True
Catch ex As Exception
If Not WordDoc Is Nothing Then
WordDoc.Close()
End If
If Not WordApp Is Nothing Then
WordApp.Quit()
End If
Return Nothing
End Try
End Function
End Class