您的位置:首页 > 编程语言 > C#

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: