C# 完美控制 Word
2008-09-15 19:38
375 查看
首先步骤是:导入COM库:Microsoft word 11.0 Object Library.
创建新Word
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
打开文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
添加新表
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
表插入行
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
单元格合并
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "/endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
常见操作
1bool saveChange = false;
2 object missing = System.Reflection.Missing.Value;
3 object template = (object)templateFilePath;
4 object filename = (object)saveFilePath;
5 object isVisible = missing;
6 object readOnly = missing;
7 object breakType = Word.WdBreakType.wdSectionBreakNextPage;
8 object isSaveChange = (object)saveChange;
9 Word.Document doc = null;
10
11 //定义一个Word.Application 对象
12 Word.Application WordApp = new Word.ApplicationClass();
13//打开文档
14 doc = WordApp.Documents.Open(
15 ref template, ref missing,ref readOnly,ref missing, ref missing, ref missing,
16 ref missing, ref missing, ref missing,ref missing, ref missing, ref isVisible,
17 ref missing, ref missing, ref missing, ref missing);
18
19 //设置页眉文本
20 WordApp.ActiveWindow.ActivePane.View.SeekView
21 = Word.WdSeekView.wdSeekCurrentPageHeader;
22 WordApp.Selection.WholeStory();
23 WordApp.Selection.TypeText( this.m_titleText );
24 WordApp.ActiveWindow.ActivePane.View.SeekView
25 = Word.WdSeekView.wdSeekMainDocument;
26
27 //页面设置,设置页面为纵向布局,设置纸张类型为A4纸
28 doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
29 doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
30 doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
31
32 //创建表格及设置表格和单元格属性
33 object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
34 doc.Content.Tables.Add(
35 WordApp.Selection.Range,
36 totalCount + 1,
37 totalField - keyCount_1,
38 ref missing,
39 ref autoFitBehavior);
40 //合并单元隔
41 doc.Content.Tables[1].Cell(i+1,j).Select();
42 object moveUnit = Word.WdUnits.wdLine;
43 object moveCount = 1;
44 object moveExtend = Word.WdMovementType.wdExtend;
45 WordApp.Selection.MoveUp(ref moveUnit, ref moveCount, ref moveExtend);
46 WordApp.Selection.Cells.Merge();
47 WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
48
49 doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”
50
51//添加表格行
52doc.Content.Tables[0].Rows.Add(ref beforeRow);
53//添加表格列
54doc.Content.Tables[0].Columns.Add(ref beforeColumn);
55//文本居中
56WordApp.Selection.ParagraphFormat.Alignment =
57 Word.WdParagraphAlignment.wdAlignParagraphCenter;
58WordApp.Selection.Cells.VerticalAlignment =
59 Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//选中单元格文字垂直居中
60
创建新Word
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
打开文档:
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:CCCXCXXTestDoc.doc";
oDoc = oWord.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
导入模板
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object fileName = @"E:XXXCCXTest.doc";
oDoc = oWord.Documents.Add(ref fileName, ref oMissing,
ref oMissing, ref oMissing);
添加新表
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
表插入行
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
单元格合并
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
单元格分离
object oMissing = System.Reflection.Missing.Value;
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
object start = 0;
object end = 0;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);
Word.Table newTable = oDoc.Tables[1];
object beforeRow = newTable.Rows[1];
newTable.Rows.Add(ref beforeRow);
Word.Cell cell = newTable.Cell(1, 1);
cell.Merge(newTable.Cell(1, 2));
object Rownum = 2;
object Columnnum = 2;
cell.Split(ref Rownum, ref Columnnum);
通过段落控制插入
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "/endofdoc"; /**//* endofdoc is a predefined bookmark */
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
//Insert a paragraph at the beginning of the document.
Word.Paragraph oPara1;
oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = "Heading 1";
oPara1.Range.Font.Bold = 1;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter();
常见操作
1bool saveChange = false;
2 object missing = System.Reflection.Missing.Value;
3 object template = (object)templateFilePath;
4 object filename = (object)saveFilePath;
5 object isVisible = missing;
6 object readOnly = missing;
7 object breakType = Word.WdBreakType.wdSectionBreakNextPage;
8 object isSaveChange = (object)saveChange;
9 Word.Document doc = null;
10
11 //定义一个Word.Application 对象
12 Word.Application WordApp = new Word.ApplicationClass();
13//打开文档
14 doc = WordApp.Documents.Open(
15 ref template, ref missing,ref readOnly,ref missing, ref missing, ref missing,
16 ref missing, ref missing, ref missing,ref missing, ref missing, ref isVisible,
17 ref missing, ref missing, ref missing, ref missing);
18
19 //设置页眉文本
20 WordApp.ActiveWindow.ActivePane.View.SeekView
21 = Word.WdSeekView.wdSeekCurrentPageHeader;
22 WordApp.Selection.WholeStory();
23 WordApp.Selection.TypeText( this.m_titleText );
24 WordApp.ActiveWindow.ActivePane.View.SeekView
25 = Word.WdSeekView.wdSeekMainDocument;
26
27 //页面设置,设置页面为纵向布局,设置纸张类型为A4纸
28 doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
29 doc.PageSetup.PageWidth = WordApp.CentimetersToPoints(29.7F);
30 doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
31
32 //创建表格及设置表格和单元格属性
33 object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
34 doc.Content.Tables.Add(
35 WordApp.Selection.Range,
36 totalCount + 1,
37 totalField - keyCount_1,
38 ref missing,
39 ref autoFitBehavior);
40 //合并单元隔
41 doc.Content.Tables[1].Cell(i+1,j).Select();
42 object moveUnit = Word.WdUnits.wdLine;
43 object moveCount = 1;
44 object moveExtend = Word.WdMovementType.wdExtend;
45 WordApp.Selection.MoveUp(ref moveUnit, ref moveCount, ref moveExtend);
46 WordApp.Selection.Cells.Merge();
47 WordApp.Selection.Cells.VerticalAlignment = Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
48
49 doc.Content.Tables[1].Cell(i+1,1).Range.Text = “单元格内容填充”
50
51//添加表格行
52doc.Content.Tables[0].Rows.Add(ref beforeRow);
53//添加表格列
54doc.Content.Tables[0].Columns.Add(ref beforeColumn);
55//文本居中
56WordApp.Selection.ParagraphFormat.Alignment =
57 Word.WdParagraphAlignment.wdAlignParagraphCenter;
58WordApp.Selection.Cells.VerticalAlignment =
59 Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//选中单元格文字垂直居中
60
相关文章推荐
- unity。。。完美简单解决靠鼠标在屏幕上的移动来控制相机水平,垂直旋转的c#脚本
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#中怎樣利用VBA控制word中的某張圖片使 之從左上角移動到右下角(或說從原來的位置向左移動200個單位)
- 发布 c#控制Word的类库。对com操作进行了封装
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- 黄聪:C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作
- C#应用程序控制Word文档操作-基本
- C#控制Word打开文档后,如何设置文档不可编辑
- c#生成的word文档,怎么控制选定的内容加下划线而不是整行加下划线
- C#对 WORD 的控制集锦
- 用C#将XML中的文字数据写如WORD文档,并控制其格式(包括字体的样式和字体大小)
- C#控制Word,Excel修改(复制、更改内容)、打印、保存权限
- c#写word文档基础操作(自己控制样式)
- 今天开始研究c#控制word
- C#控制定位Word光标移动到任意行或者最后一行,取得光标位置等操作