• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ASP.NET操作Word文档(转)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
操作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 [email protected];
//
创建一个名为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 >!
VB.net:
Public Class WordClass
    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:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + title + ".doc"
            '创建一个名为WordApp的组件对象
            WordApp = New Word.ApplicationClass
            '创建一个名为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:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + 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:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + 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:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + 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:\Inetpub\wwwroot\SLOA_NET\document\DocManage\" + 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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETMVC数据传递进阶从数据库拿到数据后的三种方式发布时间:2022-07-10
下一篇:
认识ASP.NET3.5MVC路由创建自定义路由发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap