C#创建Word文档
1、添加引用->COM->Microsoft Word 11.0 Object Library
using Word;private void CreateDoc(){Word.Application docApp = new Word.Application(); Word.Document doc;object miss = System.Reflection.Missing.Value ;string strContext; //文檔內(nèi)容doc = docApp.Documents.Add(ref miss,ref miss,ref miss,ref miss);docApp.Selection.ParagraphFormat.LineSpacing = 15;//頁(yè)眉 docApp.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView; docApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader ; docApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁(yè)眉內(nèi)容]"); docApp.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphRight;docApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; //跳出頁(yè)眉設(shè)置 //頁(yè)尾 docApp.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView; docApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter ;docApp.ActiveWindow.ActivePane.Selection.InsertAfter("[頁(yè)尾內(nèi)容]") ;docApp.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; docApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;strContext = " VB.NET 創(chuàng)建WORD文檔/r/nhttp://blog.csdn.net/jiftlixu/archive/2011/01/02/6112405.aspx";doc.Paragraphs.Last.Range.Text = strContext;//保存文件 object strFile;strFile = "c://世界多美妙.doc";doc.SaveAs(ref strFile,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss,ref miss) ;doc.Close(ref miss, ref miss, ref miss);docApp.Quit(ref miss, ref miss, ref miss);}?
總結(jié)
以上是生活随笔為你收集整理的C#创建Word文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在控制台程序中隐藏控制台窗口
- 下一篇: C#中值类型和引用类型