天天看点

net对word的操作

在.net环境里对word的操作主要包括:

1.创建word文档

2.往word中写入内容

3.保存

主要是往word写入的内容,包括标题,表格等等.

在word的版本中.net支持2003版本,

在创建word之前要做的工作:

1>创建一个.net的项目

2>添加引用:选择.Net->选择Microsoft Word 11.0 Object Library

3>添加引用:在office安装程序中找到Office.dll和Microsoft.Office.Interop.Word.dll。

下面就可以编写代码了

以下是给出例子加以讲解,对word的操作就变得非常简单了,网上的许多资料都很繁琐

在设计的时候,可以先创建一个操作类这样就可以节省很多代码,而且用起来非常的方便

下面是ClassWord.cs文件

net对word的操作
net对word的操作

Code

  1

net对word的操作

using System;

  2

net对word的操作

using System.Data;

  3

net对word的操作

using System.Configuration;

  4

net对word的操作

using System.Web;

  5

net对word的操作

using System.Web.Security;

  6

net对word的操作

using System.Web.UI;

  7

net对word的操作

using System.Web.UI.WebControls;

  8

net对word的操作

using System.Web.UI.WebControls.WebParts;

  9

net对word的操作

using System.Web.UI.HtmlControls;

 10

net对word的操作

using Microsoft.Office.Core;

 11

net对word的操作

using Microsoft.Office.Interop.Word;

 12

net对word的操作

 13

net对word的操作
net对word的操作

/// <summary>

 14

net对word的操作

/// ClassWord 的摘要说明

 15

net对word的操作

/// </summary>

 16

net对word的操作

public class ClassWord

 17

net对word的操作
net对word的操作
net对word的操作

{

 18

net对word的操作

    Word.Application wApp = null;

 19

net对word的操作

    Word.Document wDoc = null, oDoc = null;

 20

net对word的操作

    Word.Documents Docs = null;

 21

net对word的操作

    private object strTemplate = "";

 22

net对word的操作

    private object oEndOfDoc = "//endofdoc";

 23

net对word的操作

    private object oMissing = System.Reflection.Missing.Value;//System.Reflection.Missing.Value;

 24

net对word的操作

    Word.Range range = null;

 25

net对word的操作

    Word.Table oTable = null;

 26

net对word的操作

 27

net对word的操作
net对word的操作

    创建一个空word文档#region 创建一个空word文档

 28

net对word的操作

    public void AddDocuments()

 29

net对word的操作
net对word的操作
net对word的操作

{

 30

net对word的操作

        //创建一个word文档

 31

net对word的操作

        wApp = new Word.Application();

 32

net对word的操作

        wDoc = new Word.Document();

 33

net对word的操作

        wApp.Caption = "我的Word练习";//文档副标题

 34

net对word的操作

        wApp.Visible = true;//显示word文档          

 35

net对word的操作

        wDoc = wApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

 36

net对word的操作

        Docs = wApp.Documents;

 37

net对word的操作

        range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

 38

net对word的操作

 39

net对word的操作

        object page=Word.WdPageNumberAlignment.wdAlignPageNumberCenter;

 40

net对word的操作

        object firstPage = true;

 41

net对word的操作

        wApp.Selection.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref page, ref firstPage);

 42

net对word的操作

 43

net对word的操作

    }

 44

net对word的操作

    #endregion

 45

net对word的操作

 46

net对word的操作
net对word的操作

    添加标题文字#region 添加标题文字

 47

net对word的操作

    public void AddTextTitle(string Str, float size)

 48

net对word的操作
net对word的操作
net对word的操作

{

 49

net对word的操作

        range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

 50

net对word的操作

        range.Text = Str;

 51

net对word的操作

        range.Font.Size = size;//字体大小

 52

net对word的操作

        range.Font.Bold = 1;//粗体            

 53

net对word的操作

        range.Font.Color = Word.WdColor.wdColorBlack;//所选字体颜色

 54

net对word的操作

        range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置段落居中

 55

net对word的操作

    }

 56

net对word的操作

    #endregion

 57

net对word的操作

 58

net对word的操作
net对word的操作

    插入图片#region 插入图片

 59

net对word的操作

    //向文档中插入图片

 60

net对word的操作

    public void AddImage(string FileName)

 61

net对word的操作
net对word的操作
net对word的操作

{

 62

net对word的操作

        range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

 63

net对word的操作

        object LinkToFile = false;

 64

net对word的操作

        object SaveWithDocument = true;

 65

net对word的操作

        object Anchor = range;

 66

net对word的操作

        Docs.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);

 67

net对word的操作

        range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置段落居中

 68

net对word的操作

        //oDoc.Application.ActiveDocument.InlineShapes[1].Width = 150f;//图片宽度

 69

net对word的操作

        //oDoc.Application.ActiveDocument.InlineShapes[1].Height = 60f;//图片高度  

 70

net对word的操作

    }

 71

net对word的操作

    #endregion

 72

net对word的操作

 73

net对word的操作
net对word的操作

    /// <summary>

 74

net对word的操作

    /// 下标

 75

net对word的操作

    /// </summary>

 76

net对word的操作

    public void AddParagraph(string Str, float size, int bold, int Sub)

 77

net对word的操作
net对word的操作
net对word的操作

{

 78

net对word的操作

        range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

 79

net对word的操作

        range.Text = Str;

 80

net对word的操作

        range.Font.Size = size;//字体大小

 81

net对word的操作

        range.Font.Bold = bold;//粗体            

 82

net对word的操作

        range.Font.Subscript = Sub;//是否为下标1为下标

 83

net对word的操作

        range.Font.Color = Word.WdColor.wdColorBlack;//所选字体颜色

 84

net对word的操作

        range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置段落居左

 85

net对word的操作

        range.ParagraphFormat.LineSpacing = 2f;

 86

net对word的操作

    }

 87

net对word的操作
net对word的操作

    /// <summary>

 88

net对word的操作

    /// 所选字体颜色(红色)

 89

net对word的操作

    /// </summary>

 90

net对word的操作

    public void AddTextColorRed(string Str, float size, int bold)

 91

net对word的操作
net对word的操作
net对word的操作

{

 92

net对word的操作

        range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;

 93

net对word的操作

        range.Text = Str;

 94

net对word的操作

        range.Font.Size = size;//字体大小

 95

net对word的操作

        range.Font.Bold = bold;//粗体            

 96

net对word的操作

        range.Font.Color = Word.WdColor.wdColorRed;//所选字体颜色

 97

net对word的操作

        range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置段落居左

 98

net对word的操作

        range.ParagraphFormat.LineSpacing = 2f;

 99

net对word的操作

    }

100

net对word的操作

101

net对word的操作

102

net对word的操作

103

net对word的操作

104

net对word的操作

}

105

net对word的操作

接下来添加一个Web项目,拖一个Button,在其.cs文件中写入往word文档输入的内容,下面给出代码:

net对word的操作
net对word的操作

Code

1

net对word的操作

 protected void Button1_Click(object sender, EventArgs e)

2

net对word的操作
net对word的操作
net对word的操作

{

3

net对word的操作

        ClassWord word = new ClassWord();

4

net对word的操作

        word.AddDocuments();

5

net对word的操作

        word.AddTextTitle("2008,中国加油!/n", 18f);

6

net对word的操作

        string str = "    可视化设计工具(如 Microsoft Visual Studio 2005)可以简化控件的开发过程,但并不是创建或生成自定义控件的必不可少的工具。";

7

net对word的操作

        word.AddParagraph(str, 16, 0, 0);      

8

net对word的操作

    }

此时就可以直接浏览页面点击按钮,即可创建一个word文档,

标题为" 2008,中国加油",小二,加粗字体

内容为

 可视化设计工具(如 Microsoft Visual Studio  2005 )可以简化控件的开发过程,但并不是创建或生成自定义控件的必不可少的工具。

这样就完成对word中写入内容,还有其他的内容如插入图片,表格(自己设计表格的样式,合并单元格以及文字的

上下标等等,在以后分别介绍).

继续阅读