zoukankan      html  css  js  c++  java
  • c#生成word文档

         参考:http://blog.163.com/zhouchunping_99/blog/static/7837998820085114394716/

    1. 生成word文档  

    生成word文档

    view plaincopy to clipboardprint?

    1. public class BiultReportForm   
    2.    {   
    3.        /// <SUMMARY></SUMMARY>   
    4.        /// word 应用对象   
    5.        ///    
    6.        private Microsoft.Office.Interop.Word.Application _wordApplication;   
    7.   
    8.        /// <SUMMARY></SUMMARY>   
    9.        /// word 文件对象   
    10.        ///    
    11.        private Microsoft.Office.Interop.Word.Document _wordDocument;   
    12.   
    13.   
    14.   
    15.        /// <SUMMARY></SUMMARY>   
    16.        /// 创建文档   
    17.        ///    
    18.        public void CreateAWord()   
    19.        {   
    20.            //实例化word应用对象   
    21.            this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();   
    22.            Object myNothing = System.Reflection.Missing.Value;   
    23.   
    24.            this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);   
    25.        }   
    26.   
    27.   
    28.        /// <SUMMARY></SUMMARY>   
    29.        /// 添加页眉   
    30.        ///    
    31.        /// <PARAM name="pPageHeader" />   
    32.        public void SetPageHeader(string pPageHeader)   
    33.        {   
    34.            //添加页眉   
    35.            this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;   
    36.            this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;   
    37.            this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);   
    38.            //设置中间对齐   
    39.            this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;   
    40.            //跳出页眉设置   
    41.            this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;   
    42.        }   
    43.   
    44.   
    45.   
    46.        /// <SUMMARY></SUMMARY>   
    47.        /// 插入文字   
    48.        ///    
    49.        /// <PARAM name="pText" />文本信息   
    50.        /// <PARAM name="pFontSize" />字体打小   
    51.        /// <PARAM name="pFontColor" />字体颜色   
    52.        /// <PARAM name="pFontBold" />字体粗体   
    53.        /// <PARAM name="ptextAlignment" />方向   
    54.        public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment)   
    55.        {   
    56.            //设置字体样式以及方向   
    57.            this._wordApplication.Application.Selection.Font.Size = pFontSize;   
    58.            this._wordApplication.Application.Selection.Font.Bold = pFontBold;   
    59.            this._wordApplication.Application.Selection.Font.Color= pFontColor;   
    60.            this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;   
    61.            this._wordApplication.Application.Selection.TypeText(pText);   
    62.        }   
    63.   
    64.   
    65.        /// <SUMMARY></SUMMARY>   
    66.        /// 换行   
    67.        ///    
    68.        public void NewLine()   
    69.        {   
    70.            //换行   
    71.            this._wordApplication.Application.Selection.TypeParagraph();   
    72.        }   
    73.   
    74.   
    75.   
    76.        /// <SUMMARY></SUMMARY>   
    77.        /// 插入一个图片   
    78.        ///    
    79.        /// <PARAM name="pPictureFileName" />   
    80.        public void InsertPicture(string pPictureFileName)   
    81.        {   
    82.            object myNothing = System.Reflection.Missing.Value;   
    83.            //图片居中显示   
    84.            this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;   
    85.            this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);   
    86.        }   
    87.   
    88.   
    89.   
    90.        /// <SUMMARY></SUMMARY>   
    91.        /// 保存文件    
    92.        ///    
    93.        /// <PARAM name="pFileName" />保存的文件名   
    94.        public void SaveWord(string pFileName)   
    95.        {   
    96.            object myNothing = System.Reflection.Missing.Value;   
    97.            object myFileName = pFileName;   
    98.            object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;   
    99.            object myLockd = false;   
    100.            object myPassword = "";   
    101.            object myAddto = true;   
    102.   
    103.            try  
    104.            {   
    105.                this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,   
    106.                    ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,    
    107.                    ref myNothing, ref myNothing, ref myNothing);   
    108.            }   
    109.            catch  
    110.            {   
    111.                throw new Exception("导出word文档失败!");   
    112.            }   
    113.        }   
    114.    }  
  • 相关阅读:
    IDEA Rider Express expected
    .netCore与Framework相同位数下生成的hashcode是不一样的
    IDEA Rider代码错误提示关闭
    VS C# 项目一个解决方案包含多个git库项目问题
    Git Updates were rejected because the tip of your current branch is behind 一例解决方案
    IISExpress 管道模式、集成模式切换
    vs 下TGIT插件
    git常用命令
    自建git项目管理库及ssh方式使用
    Ext.net store数据加载事件
  • 原文地址:https://www.cnblogs.com/xy99/p/3977741.html
Copyright © 2011-2022 走看看