ASP.NET操作Word文档(转)
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET操作Word文档(转)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ASP.NET操作Word文檔(轉(zhuǎn)) 操作WORD配置說明 引入:Word的對象庫文件“MSWORD.OLB”(word 2000為MSWORD9.OLB) 1.運行Dcomcnfg.exe
2.組件服務(wù)――計算機――我的電腦――DCOM配置――找到microsoft word 文檔
3.點擊屬性
4.選擇“安全性”
5.選定“使用自定義訪問權(quán)限”和“使用自定義啟動權(quán)限”
6.分別編輯權(quán)限,添加Everyone(ASPNET,VS Developers,Debugger User)
7.選擇“身份標識”,在選定“交互式用戶” 即可
8.在Web.config里加 <identity impersonate="true"/> ? C#: ASP.NET操作Word文檔一直是一個大家比較關(guān)心的話題,其實在ASP.NET里操作Word文檔一點也不難,大家只需按本文提示,就能輕輕松松操作Word文檔!
一、準備工作
? ?首先請確認服務(wù)端已經(jīng)安裝了Office Word(以下將以Office XP為例),操作系統(tǒng)為win2000或XP,并且已配置好.NET的運行環(huán)境及安裝VS.NET C#開發(fā)環(huán)境后,我們就可以打開VS.NET,并新建一個Visual C#項目>ASP.NET Web應(yīng)用程序,位置為“http://localhost/word”。(如圖一)
二、引用Word對象庫文件
? ?要操作Word,我們就需要Word的對象庫文件“MSWORD.OLB”(word 2000為MSWORD9.OLB),通常安裝了Office Word后,你就可以在office安裝目錄的Office10文件夾下面找到這個文件,當(dāng)我們將這個文件引入到項目后,我們就可以在源碼中使用各種操作函數(shù)來操作Word。具體做法是打開菜單欄中的項目>添加引用>瀏覽,在打開的“選擇組件”對話框中找到MSWORD.OLB后按確定即可引入此對象庫文件,vs.net將會自動將庫文件轉(zhuǎn)化為DLL組件,這樣我們只要在源碼中創(chuàng)建該組件對象即可達到操作Word的目的!
三、Webform1.aspx.cs代碼
? ?完成添加引用后,MSWORD.OLB已經(jīng)轉(zhuǎn)化為相關(guān)DLL文件并放置于項目的BIN目錄下了,這樣我們只需在源碼中創(chuàng)建該對象,并使用word庫文件內(nèi)置的操作函數(shù)即可輕松實現(xiàn)操作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:該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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;
//創(chuàng)建一個名為WordApp的組件對象
Word.Application WordApp=new Word.ApplicationClass();
//創(chuàng)建一個名為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);
//在表格第一單元格中添加自定義的文字內(nèi)容
table.Cell(1,1).Range.Text=wordText.Text;
//在文檔空白地方添加文字內(nèi)容
WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";
//將WordDoc文檔對象的內(nèi)容保存為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);
//關(guān)閉WordDoc文檔對象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//關(guān)閉WordApp組件對象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回結(jié)果
result.Text="文檔路徑:<a href="/"+SaveAs.Text+"'>"+SaveAs.Text+"</a>(點擊鏈接查看)<br>生成結(jié)果:成功!";
}
private void Page_Load(object sender, System.EventArgs e)
{
}
}
}
四、Webform1.aspx代碼
? ?完成CS源碼后,我們就可以設(shè)計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文件內(nèi)容:</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設(shè)置
? ?web.config文件還需添加一句 <identity impersonate="true"/>以啟用模擬身份,因為默認ASPNET這個用戶是沒有權(quán)限訪問Word.ApplicationClass(),當(dāng)啟用模擬身份后所有頁面將會使用匿名Internet用戶帳戶(IUSR_machinename)這個用戶名的權(quán)限執(zhí)行,這樣我們就能成功訪問Word.ApplicationClass()并在ASP.NET中操作Word!
2.組件服務(wù)――計算機――我的電腦――DCOM配置――找到microsoft word 文檔
3.點擊屬性
4.選擇“安全性”
5.選定“使用自定義訪問權(quán)限”和“使用自定義啟動權(quán)限”
6.分別編輯權(quán)限,添加Everyone(ASPNET,VS Developers,Debugger User)
7.選擇“身份標識”,在選定“交互式用戶” 即可
8.在Web.config里加 <identity impersonate="true"/> ? C#: ASP.NET操作Word文檔一直是一個大家比較關(guān)心的話題,其實在ASP.NET里操作Word文檔一點也不難,大家只需按本文提示,就能輕輕松松操作Word文檔!
一、準備工作
? ?首先請確認服務(wù)端已經(jīng)安裝了Office Word(以下將以Office XP為例),操作系統(tǒng)為win2000或XP,并且已配置好.NET的運行環(huán)境及安裝VS.NET C#開發(fā)環(huán)境后,我們就可以打開VS.NET,并新建一個Visual C#項目>ASP.NET Web應(yīng)用程序,位置為“http://localhost/word”。(如圖一)
二、引用Word對象庫文件
? ?要操作Word,我們就需要Word的對象庫文件“MSWORD.OLB”(word 2000為MSWORD9.OLB),通常安裝了Office Word后,你就可以在office安裝目錄的Office10文件夾下面找到這個文件,當(dāng)我們將這個文件引入到項目后,我們就可以在源碼中使用各種操作函數(shù)來操作Word。具體做法是打開菜單欄中的項目>添加引用>瀏覽,在打開的“選擇組件”對話框中找到MSWORD.OLB后按確定即可引入此對象庫文件,vs.net將會自動將庫文件轉(zhuǎn)化為DLL組件,這樣我們只要在源碼中創(chuàng)建該組件對象即可達到操作Word的目的!
三、Webform1.aspx.cs代碼
? ?完成添加引用后,MSWORD.OLB已經(jīng)轉(zhuǎn)化為相關(guān)DLL文件并放置于項目的BIN目錄下了,這樣我們只需在源碼中創(chuàng)建該對象,并使用word庫文件內(nèi)置的操作函數(shù)即可輕松實現(xiàn)操作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:該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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;
//創(chuàng)建一個名為WordApp的組件對象
Word.Application WordApp=new Word.ApplicationClass();
//創(chuàng)建一個名為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);
//在表格第一單元格中添加自定義的文字內(nèi)容
table.Cell(1,1).Range.Text=wordText.Text;
//在文檔空白地方添加文字內(nèi)容
WordDoc.Paragraphs.Last.Range.Text="Wellcome To Aspxcn.Com";
//將WordDoc文檔對象的內(nèi)容保存為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);
//關(guān)閉WordDoc文檔對象
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
//關(guān)閉WordApp組件對象
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
//返回結(jié)果
result.Text="文檔路徑:<a href="/"+SaveAs.Text+"'>"+SaveAs.Text+"</a>(點擊鏈接查看)<br>生成結(jié)果:成功!";
}
private void Page_Load(object sender, System.EventArgs e)
{
}
}
}
四、Webform1.aspx代碼
? ?完成CS源碼后,我們就可以設(shè)計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文件內(nèi)容:</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設(shè)置
? ?web.config文件還需添加一句 <identity impersonate="true"/>以啟用模擬身份,因為默認ASPNET這個用戶是沒有權(quán)限訪問Word.ApplicationClass(),當(dāng)啟用模擬身份后所有頁面將會使用匿名Internet用戶帳戶(IUSR_machinename)這個用戶名的權(quán)限執(zhí)行,這樣我們就能成功訪問Word.ApplicationClass()并在ASP.NET中操作Word!
轉(zhuǎn)載于:https://www.cnblogs.com/yingpp/archive/2009/03/31/1425688.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET操作Word文档(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频与图像RGB/YUV格式详解
- 下一篇: 让IIS建立的站点默认是.net 2.0