首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

c#生成的word文档

生成word文档    

 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" />字体打小   
       /// <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文档失败!");   
           }   
       }   
   } 

【责编:michael】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航