c#寫word文檔基礎操作(自己控制樣式)


下面一個函數,建立一個Word 文檔,添加頁眉、頁腳,在內容中兩個不同字體的Hello!!!  

來自 <http://bbs.csdn.net/topics/340041961>

public void myFunction()

{

Word.ApplicationClass oWordApp = new Word.ApplicationClass();

//建立Word 對象,啟動word程序

object missing = System.Reflection.Missing.Value;

object oTemplate = System.Windows.Forms.Application.StartupPath+"\\mytemplate.dot";

Word.Document oWordDoc = oWordApp.Documents.Add( ref oTemplate,ref missing,ref missing, ref missing);//新建word文檔

oWordApp.Visible = true;//設置Word程序可見,如果為false 那么word 不可見

//頁面設置

oWordDoc.PageSetup.TopMargin = oWordApp.CentimetersToPoints(2.5f); //上

oWordDoc.PageSetup.BottomMargin = oWordApp.CentimetersToPoints(2f);//下

oWordDoc.PageSetup.LeftMargin=oWordApp.CentimetersToPoints(2.2f);//左

oWordDoc.PageSetup.RightMargin=oWordApp.CentimetersToPoints(2.2f);//右

//添加頁眉

oWordDoc.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader; //激活頁眉的編輯

oWordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; //設置對齊方式

string headtext1 ="Head Text";

oWordApp.Selection.Font.Name ="華文新魏"; //設置字體

oWordApp.Selection.Font.Size =10.5f;

oWordApp.Selection.Font.UnderlineColor = Word.WdColor.wdColorAutomatic;

oWordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle; //添加下划線

oWordApp.Selection.TypeText(headtext1);

oWordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineNone;

//添加頁腳

string foottext1 ="Foot Text";

oWordDoc.ActiveWindow.ActivePane.View.SeekView =Word.WdSeekView.wdSeekCurrentPageFooter; //激活頁腳的編輯

oWordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;

oWordApp.Selection.Font.Name ="仿宋_GB2312";

oWordApp.Selection.Font.Size =8;

oWordApp.Selection.TypeText(foottext1);

//添加正文

oWordDoc.ActiveWindow.ActivePane.View.SeekView =Word.WdSeekView.wdSeekMainDocument;//激活頁面內容的編輯

oWordApp.Selection.Font.Name ="宋體";

oWordApp.Selection.Font.Size =10.5f;

oWordApp.Selection.Font.Scaling = 200;

oWordApp.Selection.TypeText("Hello!!!");

oWordApp.Selection.TypeParagraph();//另起一段

oWordApp.Selection.Font.Name ="黑體";

oWordApp.Selection.Font.Size =10.5f;

oWordApp.Selection.Font.Scaling = 100;

oWordApp.Selection.TypeText("Hello!!!");

oWordApp.Selection.TypeParagraph();//另起一段

string strfilename = System.Windows.Forms.Application.StartupPath+"\\myfirst.doc";

object filename = strfilename ;

//保存文檔為word2000格式

oWordDoc.SaveAs2000(ref filename,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);

//保存文檔為word2003格式

//oWordDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing,

// ref missing, ref missing, ref missing, ref missing, ref missing,

// ref missing, ref missing, ref missing, ref missing, ref missing,

// ref missing) ;

//以下關閉Word程序

object nochanges = Word.WdSaveOptions.wdDoNotSaveChanges;

if(oWordApp.Documents!= null)

{

IEnumerator ie = oWordApp.Documents.GetEnumerator();

while( ie.MoveNext())

{

Word.Document closedoc = (Word.Document)ie.Current;

closedoc.Close(ref nochanges,ref missing,ref missing);

}

}

oWordApp.Quit(ref nochanges, ref missing, ref missing);

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM