下面一个函数,建立一个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);
}