在.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文件

Code
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.WebControls;
8
using System.Web.UI.WebControls.WebParts;
9
using System.Web.UI.HtmlControls;
10
using Microsoft.Office.Core;
11
using Microsoft.Office.Interop.Word;
12
13

/// <summary>
14
/// ClassWord 的摘要说明
15
/// </summary>
16
public class ClassWord
17

{
18
Word.Application wApp = null;
19
Word.Document wDoc = null, oDoc = null;
20
Word.Documents Docs = null;
21
private object strTemplate = "";
22
private object oEndOfDoc = "//endofdoc";
23
private object oMissing = System.Reflection.Missing.Value;//System.Reflection.Missing.Value;
24
Word.Range range = null;
25
Word.Table oTable = null;
26
27
创建一个空word文档#region 创建一个空word文档
28
public void AddDocuments()
29
{
30
//创建一个word文档
31
wApp = new Word.Application();
32
wDoc = new Word.Document();
33
wApp.Caption = "我的Word练习";//文档副标题
34
wApp.Visible = true;//显示word文档
35
wDoc = wApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
36
Docs = wApp.Documents;
37
range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
38
39
object page=Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
40
object firstPage = true;
41
wApp.Selection.Sections[1].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref page, ref firstPage);
42
43
}
44
#endregion
45
46
添加标题文字#region 添加标题文字
47
public void AddTextTitle(string Str, float size)
48
{
49
range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
50
range.Text = Str;
51
range.Font.Size = size;//字体大小
52
range.Font.Bold = 1;//粗体
53
range.Font.Color = Word.WdColor.wdColorBlack;//所选字体颜色
54
range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置段落居中
55
}
56
#endregion
57
58
插入图片#region 插入图片
59
//向文档中插入图片
60
public void AddImage(string FileName)
61
{
62
range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
63
object LinkToFile = false;
64
object SaveWithDocument = true;
65
object Anchor = range;
66
Docs.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
67
range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置段落居中
68
//oDoc.Application.ActiveDocument.InlineShapes[1].Width = 150f;//图片宽度
69
//oDoc.Application.ActiveDocument.InlineShapes[1].Height = 60f;//图片高度
70
}
71
#endregion
72
73
/// <summary>
74
/// 下标
75
/// </summary>
76
public void AddParagraph(string Str, float size, int bold, int Sub)
77
{
78
range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
79
range.Text = Str;
80
range.Font.Size = size;//字体大小
81
range.Font.Bold = bold;//粗体
82
range.Font.Subscript = Sub;//是否为下标1为下标
83
range.Font.Color = Word.WdColor.wdColorBlack;//所选字体颜色
84
range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置段落居左
85
range.ParagraphFormat.LineSpacing = 2f;
86
}
87
/// <summary>
88
/// 所选字体颜色(红色)
89
/// </summary>
90
public void AddTextColorRed(string Str, float size, int bold)
91
{
92
range = wDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
93
range.Text = Str;
94
range.Font.Size = size;//字体大小
95
range.Font.Bold = bold;//粗体
96
range.Font.Color = Word.WdColor.wdColorRed;//所选字体颜色
97
range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置段落居左
98
range.ParagraphFormat.LineSpacing = 2f;
99
}
100
101
102
103
104
}
105
接下来添加一个Web项目,拖一个Button,在其.cs文件中写入往word文档输入的内容,下面给出代码:

Code
1
protected void Button1_Click(object sender, EventArgs e)
2

{
3
ClassWord word = new ClassWord();
4
word.AddDocuments();
5
word.AddTextTitle("2008,中国加油!/n", 18f);
6
string str = " 可视化设计工具(如 Microsoft Visual Studio 2005)可以简化控件的开发过程,但并不是创建或生成自定义控件的必不可少的工具。";
7
word.AddParagraph(str, 16, 0, 0);
8
}
此时就可以直接浏览页面点击按钮,即可创建一个word文档,
标题为" 2008,中国加油",小二,加粗字体
内容为
可视化设计工具(如 Microsoft Visual Studio 2005 )可以简化控件的开发过程,但并不是创建或生成自定义控件的必不可少的工具。
这样就完成对word中写入内容,还有其他的内容如插入图片,表格(自己设计表格的样式,合并单元格以及文字的
上下标等等,在以后分别介绍).