从ASP.Net向Word文档添加页眉和页脚

时间:2009-04-30 08:56:34

标签: asp.net ms-word

有没有办法向Word添加页眉和页脚信息。我目前正在使用ASP.Net将HTML内容写入word文件。

1 个答案:

答案 0 :(得分:1)

下面的代码可以生成MS Word文档而不使用HTML格式 -

object oTrue = true;
object oFalse = false;
object oMissing = System.Reflection.Missing.Value;
object novalue = System.Reflection.Missing.Value;
object missing = System.Reflection.Missing.Value;
object fileName = "normal.dot";
object newTemplate = false;
object docType = 0;
object isVisible = true;

Microsoft.Office.Interop.Word.ApplicationClass questionClient = new Microsoft.Office.Interop.Word.ApplicationClass();

Microsoft.Office.Interop.Word.Document questionDoc = questionClient.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
questionDoc.Activate();

//adding header
string logoPath = Server.MapPath("~//photos//logica_logo_black.JPG");
Microsoft.Office.Interop.Word.Shape logoCustom = null;
questionClient.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
logoCustom = questionClient.Selection.HeaderFooter.Shapes.AddPicture(logoPath, ref oFalse, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
logoCustom.Select(ref oMissing);
logoCustom.Name = "CustomLogo";
logoCustom.Left = -47;
logoCustom.Top = -19;

//adding footer
questionClient.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
questionClient.ActiveWindow.Selection.Font.Name = "Verdana";
questionClient.ActiveWindow.Selection.Font.Size = 8;
Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
questionClient.ActiveWindow.Selection.TypeText("For Logica Internal Use Only");
questionClient.ActiveWindow.Selection.TypeText("                                             ");
questionClient.ActiveWindow.Selection.TypeText("Page ");
questionClient.ActiveWindow.Selection.Fields.Add(questionClient.Selection.Range, ref CurrentPage, ref oMissing, ref oMissing);
questionClient.ActiveWindow.Selection.TypeText("                                                     ");
questionClient.ActiveWindow.Selection.TypeText(DateTime.Today.ToString("MM/dd/yyyy"));

//saving and closing the document
questionClient.Documents.Save(ref oMissing, ref oMissing);
questionClient.Quit(ref oMissing, ref oMissing, ref oMissing);