天天看點

C#操作Word完全功略!

導入COM庫:Microsoft word 11.0 Object Library.

引用裡面就增加了:

  原http://support.microsoft.com/kb/316384/zh-cn#top

建立新Word

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

打開文檔:

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

object fileName = @"E:\CCCXCXX\TestDoc.doc";

C#操作Word完全功略!

oDoc = oWord.Documents.Open( ref fileName,

C#操作Word完全功略!

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

導入模闆

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

object fileName = @"E:\XXXCCX\Test.doc";

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref fileName, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

.添加新表

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

object start = 0;

C#操作Word完全功略!

object end = 0;

C#操作Word完全功略!

Word.Range tableLocation = oDoc.Range( ref start, ref end);

C#操作Word完全功略!

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

.表插入行

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

object start = 0;

C#操作Word完全功略!

object end = 0;

C#操作Word完全功略!

Word.Range tableLocation = oDoc.Range( ref start, ref end);

C#操作Word完全功略!

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

Word.Table newTable = oDoc.Tables[1];

C#操作Word完全功略!

object beforeRow = newTable.Rows[1];

C#操作Word完全功略!

newTable.Rows.Add( ref beforeRow);

.單元格合并

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

object start = 0;

C#操作Word完全功略!

object end = 0;

C#操作Word完全功略!

Word.Range tableLocation = oDoc.Range( ref start, ref end);

C#操作Word完全功略!

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

Word.Table newTable = oDoc.Tables[1];

C#操作Word完全功略!

object beforeRow = newTable.Rows[1];

C#操作Word完全功略!

newTable.Rows.Add( ref beforeRow);

C#操作Word完全功略!
C#操作Word完全功略!

Word.Cell cell = newTable.Cell(1, 1);

C#操作Word完全功略!

cell.Merge(newTable.Cell(1, 2));

.單元格分離

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

object start = 0;

C#操作Word完全功略!

object end = 0;

C#操作Word完全功略!

Word.Range tableLocation = oDoc.Range( ref start, ref end);

C#操作Word完全功略!

oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

Word.Table newTable = oDoc.Tables[1];

C#操作Word完全功略!

object beforeRow = newTable.Rows[1];

C#操作Word完全功略!

newTable.Rows.Add( ref beforeRow);

C#操作Word完全功略!
C#操作Word完全功略!

Word.Cell cell = newTable.Cell(1, 1);

C#操作Word完全功略!

cell.Merge(newTable.Cell(1, 2));

C#操作Word完全功略!
C#操作Word完全功略!

object Rownum = 2;

C#操作Word完全功略!

object Columnnum = 2;

C#操作Word完全功略!

cell.Split( ref Rownum, ref Columnnum);

C#操作Word完全功略!

通過段落控制插入

C#操作Word完全功略!

object oMissing = System.Reflection.Missing.Value;

C#操作Word完全功略!
C#操作Word完全功略!

object oEndOfDoc = "\\endofdoc";

C#操作Word完全功略!
C#操作Word完全功略!

// Start Word and create a new document.

C#操作Word完全功略!

Word._Application oWord;

C#操作Word完全功略!

Word._Document oDoc;

C#操作Word完全功略!

oWord = new Word.Application();

C#操作Word完全功略!

oWord.Visible = true;

C#操作Word完全功略!

oDoc = oWord.Documents.Add( ref oMissing, ref oMissing,

C#操作Word完全功略!

ref oMissing, ref oMissing);

C#操作Word完全功略!
C#操作Word完全功略!

// Insert a paragraph at the beginning of the document.

C#操作Word完全功略!

Word.Paragraph oPara1;

C#操作Word完全功略!

oPara1 = oDoc.Content.Paragraphs.Add( ref oMissing);

C#操作Word完全功略!

oPara1.Range.Text = "Heading 1";

C#操作Word完全功略!

oPara1.Range.Font.Bold = 1;

C#操作Word完全功略!

oPara1.Format.SpaceAfter = 24; // 24 pt spacing after paragraph.

C#操作Word完全功略!

oPara1.Range.InsertParagraphAfter();

繼續閱讀