日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET操作Word文档(转)

發布時間:2025/7/14 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET操作Word文档(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
ASP.NET操作Word文檔(轉) 操作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為例),操作系統為win2000XP,并且已配置好.NET的運行環境及安裝VS.NET C#開發環境后,我們就可以打開VS.NET,并新建一個Visual C#項目>ASP.NET Web應用程序,位置為“http://localhost/word”。(如圖一)
二、引用Word對象庫文件
? ?
要操作Word,我們就需要Word的對象庫文件“MSWORD.OLB(word 2000MSWORD9.OLB),通常安裝了Office Word后,你就可以在office安裝目錄的Office10文件夾下面找到這個文件,當我們將這個文件引入到項目后,我們就可以在源碼中使用各種操作函數來操作Word。具體做法是打開菜單欄中的項目>添加引用>瀏覽,在打開的“選擇組件”對話框中找到MSWORD.OLB后按確定即可引入此對象庫文件,vs.net將會自動將庫文件轉化為DLL組件,這樣我們只要在源碼中創建該組件對象即可達到操作Word的目的!
三、Webform1.aspx.cs代碼
? ?
完成添加引用后,MSWORD.OLB已經轉化為相關DLL文件并放置于項目的BIN目錄下了,這樣我們只需在源碼中創建該對象,并使用word庫文件內置的操作函數即可輕松實現操作WordWebform1.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 窗體設計器所必需的。
//
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

轉載于:https://www.cnblogs.com/yingpp/archive/2009/03/31/1425688.html

總結

以上是生活随笔為你收集整理的ASP.NET操作Word文档(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。