我用C#自动生成一个word文件,我的想法是按照先后的顺序 先写 标题"title" 然后写一行字"first ",然后写入一个表格,在表格之后 在写一段文字"next"。可是程序的结果和我想象不一样。 结果是先是 "title" 然后 "first" "last" 然后才是那个表格. 代码如下:
//创建Word object Nothing=System.Reflection.Missing.Value; //取得Word文件保存路径 object filename=@"c:\\testtable.doc"; object unit; object count; //移动数 object extend; object DefaultTableBehavior=Word.WdDefaultTableBehavior.wdWord9TableBehavior; object AutoFitBehavior=Word.WdAutoFitBehavior.wdAutoFitFixed; extend = Word.WdMovementType.wdExtend; unit = Word.WdUnits.wdLine; count=1; //创建一个名为WordApp的组件对象 Word.Application WordApp=new Word.ApplicationClass(); //创建一个名为WordDoc的文档对象 Word.Document WordDoc=WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing); //标题头 WordApp.Selection.Font.Bold=1;//StrToint((Word.WdConstants.wdToggle)); WordApp.Selection.Font.Size=16; WordApp.Selection.Font.Name="隶书"; WordApp.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter; WordApp.Selection.TypeText("title"); WordApp.Selection.TypeParagraph(); WordApp.Selection.TypeText("first"); WordApp.Selection.TypeParagraph(); //增加一表格 int i,j; i=60;j=3; Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,i,j,ref DefaultTableBehavior ,ref AutoFitBehavior);//行 列 for (i=1;i<=60 ;i++ ) { for(j=1;j<=3;j++) table.Cell(i,j).Range.Text=i.ToString()+j.ToString(); } WordApp.Selection.TypeParagraph(); WordApp.Selection.TypeText("last"); WordDoc.Paragraphs.Last.Range.Text="欢迎参加学习!"; |