Net 下启动Word
在服务器端需要做的配置操作
按如下方法做:
C# 动态生成word文档(我安装的是Office2007)。1,添加引用->.NET->Microsoft.Office.Interop.Word 12.0
2,添加如下类
public class BiultReportForm
    {
        /// <SUMMARY></SUMMARY>    
       /// word 应用对象    
       ///     
       private Microsoft.Office.Interop.Word.Application _wordApplication;
       /// <SUMMARY></SUMMARY>    
       /// word 文件对象    
       ///
       private Microsoft.Office.Interop.Word.Document _wordDocument;    
       /// <SUMMARY></SUMMARY>
       /// 创建文档    
       ///     
       public void CreateAWord()    
       {    
           //实例化word应用对象    
           this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();    
           Object myNothing = System.Reflection.Missing.Value;    
   
           this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);    
       }
       /// <SUMMARY></SUMMARY>
       /// 添加页眉    
       ///     
       /// <PARAM name="pPageHeader" />    
       public void SetPageHeader(string pPageHeader)    
       {    
           //添加页眉    
           this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;    
           this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;    
           this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
           //设置中间对齐    
           this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;    
           //跳出页眉设置    
           this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;    
       }    
       /// <SUMMARY></SUMMARY>    
       /// 插入文字    
       ///     
       /// <PARAM name="pText" />文本信息    
       /// <PARAM name="pFontSize" />字体打小    网管网bitsCN.com 
       /// <PARAM name="pFontColor" />字体颜色    
       /// <PARAM name="pFontBold" />字体粗体    
       /// <PARAM name="ptextAlignment" />方向    
       public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)    
       {    
           //设置字体样式以及方向    
           this._wordApplication.Application.Selection.Font.Size = pFontSize;    
           this._wordApplication.Application.Selection.Font.Bold = pFontBold;    
           this._wordApplication.Application.Selection.Font.Color= pFontColor;
           this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;    
           this._wordApplication.Application.Selection.TypeText(pText);    
       }
       /// <SUMMARY></SUMMARY>    
       /// 换行    
       ///     
       public void NewLine()    
       {    
           //换行    
           this._wordApplication.Application.Selection.TypeParagraph();    
       }
       /// <SUMMARY></SUMMARY>    
       /// 插入一个图片    
       ///     
       /// <PARAM name="pPictureFileName" />    
       public void InsertPicture(string pPictureFileName)    
       {    
           object myNothing = System.Reflection.Missing.Value;    
           //图片居中显示    
           this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;    
           this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
       } 
       /// <SUMMARY></SUMMARY>    
       /// 保存文件     
       ///     
       /// <PARAM name="pFileName" />保存的文件名    
       public void SaveWord(string pFileName)    
       {    
           object myNothing = System.Reflection.Missing.Value;    
           object myFileName = pFileName;    
           object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;
           object myLockd = false;
           object myPassword = "";    
           object myAddto = true;    
           try   
           {    
               this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,    
                   ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,     
                   ref myNothing, ref myNothing, ref myNothing);    
           }
           catch   
           {    
               throw new Exception("导出word文档失败!");    
           }    
       }
    }
3,使用方法如下:
            BiultReportForm wordoc = new BiultReportForm();
            wordoc.CreateAWord();
            wordoc.InsertText("TestData", 14, WdColor.wdColorBlack, 15, WdParagraphAlignment.wdAlignParagraphCenter);
            wordoc.SaveWord("c:\\test.doc");
===========================================================================
开始在代码:
//实例化word应用对象    
this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();   处抛出下列异常:COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005(异常①)。我采取了操作配置一解决了它。
配置操作一
控制面板-》管理工具-》组件服务-》计算机-》我的电脑-》DCom配置-》找到Microsoft Word文档之后单击属性打开此应用程序的属性对话框。  
单击"安全"选项卡,分别在"启动和激活权限"和"访问权限"组中选中"自定义",然后
自定义->编辑->添加ASP.NET账户和IUSER_计算机名 
上述问题消失,但是在执行如下代码时:
this._wordDocument.SaveAs(ref myFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
   会抛出异常:保存word文档失败消息筛选器显示应用程序正在使用中。 (异常来自 HRESULT:0x8001010A (RPC_E_SERVERCALL_RETRYLATER))"(异常②)然后我又做了配置操作二,如下:
1、  在命令行中输入:dcomcnfg,会显示出“组件服务”管理器 
2、打开“组件服务->计算机->我的电脑->DCOM 配置”,找到“Microsoft Word文档”,单击右键,选择“属性” 
3、在“属性”对话框中单击“标识”选项卡,选择“交互式用户”。
上述问题消失,但是又出现了:COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005异常情况。然后我又做了配置操作一,异常②消失;但是又出现了异常①,再做配置操作二,异常①消失,异常②又出现。就这样两个异常就循环出现下去了。
出现的问题????