C#操作Word完全功略!

前提:
导入COM库:Microsoft word 11.0 Object Library.
引用里面就增加了:

创建新Word

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
oDoc=oWord.Documents.Add(
refoMissing,refoMissing,
refoMissing,refoMissing);


打开文档:

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
objectfileName=@"E:\CCCXCXX\TestDoc.doc";
oDoc=oWord.Documents.Open(
reffileName,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing,
refoMissing,refoMissing,refoMissing,refoMissing,refoMissing);


导入模板

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
objectfileName=@"E:\XXXCCX\Test.doc";
oDoc=oWord.Documents.Add(
reffileName,refoMissing,
refoMissing,refoMissing);



.
添加新表

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
oDoc=oWord.Documents.Add(
refoMissing,refoMissing,
refoMissing,refoMissing);

objectstart=0;
objectend=0;
Word.RangetableLocation=oDoc.Range(
refstart,refend);
oDoc.Tables.Add(tableLocation,3,4,
refoMissing,refoMissing);


.
表插入行

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
oDoc=oWord.Documents.Add(
refoMissing,refoMissing,
refoMissing,refoMissing);

objectstart=0;
objectend=0;
Word.RangetableLocation=oDoc.Range(
refstart,refend);
oDoc.Tables.Add(tableLocation,3,4,
refoMissing,refoMissing);

Word.TablenewTable=oDoc.Tables[1];
objectbeforeRow=newTable.Rows[1];
newTable.Rows.Add(
refbeforeRow);


.
单元格合并

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
oDoc=oWord.Documents.Add(
refoMissing,refoMissing,
refoMissing,refoMissing);

objectstart=0;
objectend=0;
Word.RangetableLocation=oDoc.Range(
refstart,refend);
oDoc.Tables.Add(tableLocation,3,4,
refoMissing,refoMissing);

Word.TablenewTable=oDoc.Tables[1];
objectbeforeRow=newTable.Rows[1];
newTable.Rows.Add(
refbeforeRow);

Word.Cellcell=newTable.Cell(1,1);
cell.Merge(newTable.Cell(1,2));


.
单元格分离

objectoMissing=System.Reflection.Missing.Value;
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
oDoc=oWord.Documents.Add(\\endofdoc";
/*\endofdocisapredefinedbookmark*/

//StartWordandcreateanewdocument.
Word._ApplicationoWord;
Word._DocumentoDoc;
oWord=
newWord.Application();
oWord.Visible=
true;
oDoc=oWord.Documents.Add(
refoMissing,refoMissing,
refoMissing,refoMissing);

//Insertaparagraphatthebeginningofthedocument.
Word.ParagraphoPara1;
oPara1=oDoc.Content.Paragraphs.Add(
refoMissing);
oPara1.Range.Text="Heading1";
oPara1.Range.Font.Bold=1;
oPara1.Format.SpaceAfter=24;
//24ptspacingafterparagraph.
oPara1.Range.InsertParagraphAfter();

你可能感兴趣的:(C++,c,C#,Microsoft)