c#写word文档基础操作(自己控制样式)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

c#写word⽂档基础操作(⾃⼰控制样式)
下⾯⼀个函数,建⽴⼀个Word ⽂档,添加页眉、页脚,在内容中两个不同字体的Hello
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.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;
="仿宋_GB2312";
oWordApp.Selection.Font.Size =8;
oWordApp.Selection.TypeText(foottext1);
//添加正⽂
oWordDoc.ActiveWindow.ActivePane.View.SeekView =Word.WdSeekView.wdSeekMainDocument;//激活页⾯内容的编辑 ="宋体";
oWordApp.Selection.Font.Size =10.5f;
oWordApp.Selection.Font.Scaling = 200;
oWordApp.Selection.TypeText("Hello");
oWordApp.Selection.TypeParagraph();//另起⼀段
="⿊体";
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);
}。

相关文档
最新文档