转: http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html
1、新建Word文档
 
    
       #region
        
        
           新建Word文档
      
        
          #region
        
        
           新建Word文档
        
        
           ///
          
          
             
          
          
            <summary>
        
        
          
            ///
          
          
             
          
          
            <summary>
          
          
             ///
          
          
             动态生成Word文档并填充内容
          
          
            ///
          
          
             动态生成Word文档并填充内容 
             ///
          
          
             
          
          
            </summary>
          
          
            ///
          
          
             
          
          
            </summary>
          
          
             ///
          
          
             
          
          
            <param name="dir">
          
          
            文档目录
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="dir">
          
          
            文档目录
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <param name="fileName">
          
          
            文档名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="fileName">
          
          
            文档名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <returns>
          
          
            返回自定义信息
          
          
            </returns>
          
          
            ///
          
          
             
          
          
            <returns>
          
          
            返回自定义信息
          
          
            </returns>
          
        
         public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           CreateWordFile(
        
        
          string
        
        
           dir, 
        
        
          string
        
        
           fileName)
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           CreateWordFile(
        
        
          string
        
        
           dir, 
        
        
          string
        
        
           fileName)
           {
        
        
          
            {
             try
                
          
          
            try
          
          
             {
                
          
          
            
              {
               Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
                      Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
               
               if
            
            
               (
            
            
              !
            
            
              Directory.Exists(dir))
                      
            
            
              if
            
            
               (
            
            
              !
            
            
              Directory.Exists(dir))
               {
                      
            
            
              
                {
                 //
              
              
                创建文件所在目录
                            
              
              
                //
              
              
                创建文件所在目录
              
              
                 Directory.CreateDirectory(dir);
              
              
                            Directory.CreateDirectory(dir);
                 }
                        }
              
            
            
               //
            
            
              创建Word文档(Microsoft.Office.Interop.Word)
                      
            
            
              //
            
            
              创建Word文档(Microsoft.Office.Interop.Word)
            
            
               Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
            
            
                      Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
               WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Add(
                      Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Add(
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               
               //
            
            
              保存
                      
            
            
              //
            
            
              保存
            
            
               object
            
            
               filename 
            
            
              =
            
            
               dir 
            
            
              +
            
            
               fileName;
            
            
                      
            
            
              object
            
            
               filename 
            
            
              =
            
            
               dir 
            
            
              +
            
            
               fileName;
               WordDoc.SaveAs(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                      WordDoc.SaveAs(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               return
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              true
            
            
              ;
               }
                  }
            
          
          
             catch
          
          
             (Exception e)
                
          
          
            catch
          
          
             (Exception e)
             {
                
          
          
            
              {
               Console.WriteLine(e.Message);
                      Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);
                      Console.WriteLine(e.StackTrace);
               return
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              false
            
            
              ;
               }
                  }
            
          
          
             }
            }
          
        
        
           #endregion
        
      
      
         新建Word文档
        
        
          #endregion
        
      
      
         新建Word文档
      
    
  2、给word文档添加页眉页脚
 
    
       #region
        
        
           给word文档添加页眉页脚
      
        
          #region
        
        
           给word文档添加页眉页脚
        
        
           ///
          
          
             
          
          
            <summary>
        
        
          
            ///
          
          
             
          
          
            <summary>
          
          
             ///
          
          
             给word文档添加页眉
          
          
            ///
          
          
             给word文档添加页眉
             ///
          
          
             
          
          
            </summary>
          
          
            ///
          
          
             
          
          
            </summary>
          
          
             ///
          
          
             
          
          
            <param name="filePath">
          
          
            文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="filePath">
          
          
            文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <returns></returns>
          
          
            ///
          
          
             
          
          
            <returns></returns>
          
        
         public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddPageHeaderFooter(
        
        
          string
        
        
           filePath)
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddPageHeaderFooter(
        
        
          string
        
        
           filePath)
           {
        
        
          
            {
             try
                
          
          
            try
          
          
             {
                
          
          
            
              {
               Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
                      Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
               Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
                      Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
               WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               filename 
            
            
              =
            
            
               filePath;
                      
            
            
              object
            
            
               filename 
            
            
              =
            
            
               filePath;
               Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
                      Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               
               ///
              
              
                /添加页眉方法一:
                      
            
            
              
                ///
              
              
                /添加页眉方法一:
              
            
             //
            
            
              WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            
                      
            
            
              //
            
            
              WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
               //
            
            
              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                      
            
            
              //
            
            
              WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
               //
            
            
              WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );
            
            
              //
            
            
              页眉内容
                      
            
            
              //
            
            
              WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );
            
            
              //
            
            
              页眉内容
            
            
               
            
            
               ///
              
              
                /添加页眉方法二:
                      
            
            
              
                ///
              
              
                /添加页眉方法二:
              
            
             if
            
            
               (WordApp.ActiveWindow.ActivePane.View.Type 
            
            
              ==
            
            
               WdViewType.wdNormalView 
            
            
              ||
            
                      
            
            
              if
            
            
               (WordApp.ActiveWindow.ActivePane.View.Type 
            
            
              ==
            
            
               WdViewType.wdNormalView 
            
            
              ||
            
            
               WordApp.ActiveWindow.ActivePane.View.Type 
            
            
              ==
            
            
               WdViewType.wdOutlineView)
                          WordApp.ActiveWindow.ActivePane.View.Type 
            
            
              ==
            
            
               WdViewType.wdOutlineView)
               {
                      
            
            
              
                {
                 WordApp.ActiveWindow.ActivePane.View.Type 
              
              
                =
              
              
                 WdViewType.wdPrintView;
                            WordApp.ActiveWindow.ActivePane.View.Type 
              
              
                =
              
              
                 WdViewType.wdPrintView;
                 }
                        }
              
            
            
               WordApp.ActiveWindow.View.SeekView 
            
            
              =
            
            
               WdSeekView.wdSeekCurrentPageHeader;
                      WordApp.ActiveWindow.View.SeekView 
            
            
              =
            
            
               WdSeekView.wdSeekCurrentPageHeader;
               WordApp.Selection.HeaderFooter.LinkToPrevious 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
                      WordApp.Selection.HeaderFooter.LinkToPrevious 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
               WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment 
            
            
              =
            
            
               WdParagraphAlignment.wdAlignParagraphCenter;
                      WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment 
            
            
              =
            
            
               WdParagraphAlignment.wdAlignParagraphCenter;
               WordApp.Selection.HeaderFooter.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              页眉内容
            
            
              "
            
            
              ;
                      WordApp.Selection.HeaderFooter.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              页眉内容
            
            
              "
            
            
              ;
               
               WordApp.ActiveWindow.View.SeekView 
            
            
              =
            
            
               WdSeekView.wdSeekCurrentPageFooter;
                      WordApp.ActiveWindow.View.SeekView 
            
            
              =
            
            
               WdSeekView.wdSeekCurrentPageFooter;
               WordApp.Selection.HeaderFooter.LinkToPrevious 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
                      WordApp.Selection.HeaderFooter.LinkToPrevious 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
               WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment 
            
            
              =
            
            
               WdParagraphAlignment.wdAlignParagraphCenter;
                      WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment 
            
            
              =
            
            
               WdParagraphAlignment.wdAlignParagraphCenter;
               WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(
            
            
              "
            
            
              页脚内容
            
            
              "
            
            
              );
                      WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(
            
            
              "
            
            
              页脚内容
            
            
              "
            
            
              );
               
               //
            
            
              跳出页眉页脚设置
                      
            
            
              //
            
            
              跳出页眉页脚设置
            
            
               WordApp.ActiveWindow.View.SeekView 
            
            
              =
            
            
               WdSeekView.wdSeekMainDocument;
            
            
                      WordApp.ActiveWindow.View.SeekView 
            
            
              =
            
            
               WdSeekView.wdSeekMainDocument;
               
               //
            
            
              保存
                      
            
            
              //
            
            
              保存
            
            
               WordDoc.Save();
            
            
                      WordDoc.Save();
               WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               return
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              true
            
            
              ;
               }
                  }
            
          
          
             catch
          
          
             (Exception e)
                
          
          
            catch
          
          
             (Exception e)
             {
                
          
          
            
              {
               Console.WriteLine(e.Message);
                      Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);
                      Console.WriteLine(e.StackTrace);
               return
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              false
            
            
              ;
               }
                  }
            
          
          
             }
            }
          
        
        
           #endregion
        
      
      
         给word文档添加页眉页脚
        
        
          #endregion
        
      
      
         给word文档添加页眉页脚
      
    
  3、设置文档格式并添加文本内容、超链接
 
    
       #region
        
        
           设置文档格式并添加文本内容、超链接
      
        
          #region
        
        
           设置文档格式并添加文本内容、超链接
        
        
           ///
          
          
             
          
          
            <summary>
        
        
          
            ///
          
          
             
          
          
            <summary>
          
          
             ///
          
          
             设置文档格式并添加内容
          
          
            ///
          
          
             设置文档格式并添加内容
             ///
          
          
             
          
          
            </summary>
          
          
            ///
          
          
             
          
          
            </summary>
          
          
             ///
          
          
             
          
          
            <param name="filePath">
          
          
            文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="filePath">
          
          
            文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <returns></returns>
          
          
            ///
          
          
             
          
          
            <returns></returns>
          
        
         public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddContent(
        
        
          string
        
        
           filePath)
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddContent(
        
        
          string
        
        
           filePath)
           {
        
        
          
            {
             try
                
          
          
            try
          
          
             {
                
          
          
            
              {
               Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
                      Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
               Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
                      Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
               WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               filename 
            
            
              =
            
            
               filePath;
                      
            
            
              object
            
            
               filename 
            
            
              =
            
            
               filePath;
               Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
                      Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               
               //
            
            
              设置居左
                      
            
            
              //
            
            
              设置居左
            
            
               WordApp.Selection.ParagraphFormat.Alignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
            
            
                      WordApp.Selection.ParagraphFormat.Alignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
               
               //
            
            
              设置文档的行间距
                      
            
            
              //
            
            
              设置文档的行间距
            
            
               WordApp.Selection.ParagraphFormat.LineSpacing 
            
            
              =
            
            
               15f;
            
            
                      WordApp.Selection.ParagraphFormat.LineSpacing 
            
            
              =
            
            
               15f;
               //
            
            
              插入段落
                      
            
            
              //
            
            
              插入段落
               //
            
            
              WordApp.Selection.TypeParagraph();
                      
            
            
              //
            
            
              WordApp.Selection.TypeParagraph();
            
            
               Microsoft.Office.Interop.Word.Paragraph para;
            
            
                      Microsoft.Office.Interop.Word.Paragraph para;
               para 
            
            
              =
            
            
               WordDoc.Content.Paragraphs.Add(
            
            
              ref
            
            
               oMissing);
                      para 
            
            
              =
            
            
               WordDoc.Content.Paragraphs.Add(
            
            
              ref
            
            
               oMissing);
               //
            
            
              正常格式
                      
            
            
              //
            
            
              正常格式
            
            
               para.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              This is paragraph 1
            
            
              "
            
            
              ;
            
            
                      para.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              This is paragraph 1
            
            
              "
            
            
              ;
               //
            
            
              para.Range.Font.Bold = 2;
                      
            
            
              //
            
            
              para.Range.Font.Bold = 2;
               //
            
            
              para.Range.Font.Color = WdColor.wdColorRed;
                      
            
            
              //
            
            
              para.Range.Font.Color = WdColor.wdColorRed;
               //
            
            
              para.Range.Font.Italic = 2;
                      
            
            
              //
            
            
              para.Range.Font.Italic = 2;
            
            
               para.Range.InsertParagraphAfter();
            
            
                      para.Range.InsertParagraphAfter();
               
               para.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              This is paragraph 2
            
            
              "
            
            
              ;
                      para.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              This is paragraph 2
            
            
              "
            
            
              ;
               para.Range.InsertParagraphAfter();
                      para.Range.InsertParagraphAfter();
               
               //
            
            
              插入Hyperlink
                      
            
            
              //
            
            
              插入Hyperlink
            
            
               Microsoft.Office.Interop.Word.Selection mySelection 
            
            
              =
            
            
               WordApp.ActiveWindow.Selection;
            
            
                      Microsoft.Office.Interop.Word.Selection mySelection 
            
            
              =
            
            
               WordApp.ActiveWindow.Selection;
               mySelection.Start 
            
            
              =
            
            
               
            
            
              9999
            
            
              ;
                      mySelection.Start 
            
            
              =
            
            
               
            
            
              9999
            
            
              ;
               mySelection.End 
            
            
              =
            
            
               
            
            
              9999
            
            
              ;
                      mySelection.End 
            
            
              =
            
            
               
            
            
              9999
            
            
              ;
               Microsoft.Office.Interop.Word.Range myRange 
            
            
              =
            
            
               mySelection.Range;
                      Microsoft.Office.Interop.Word.Range myRange 
            
            
              =
            
            
               mySelection.Range;
               
               Microsoft.Office.Interop.Word.Hyperlinks myLinks 
            
            
              =
            
            
               WordDoc.Hyperlinks;
                      Microsoft.Office.Interop.Word.Hyperlinks myLinks 
            
            
              =
            
            
               WordDoc.Hyperlinks;
               object
            
            
               linkAddr 
            
            
              =
            
            
               
            
            
              @"
            
            
              http://www.cnblogs.com/lantionzy
            
            
              "
            
            
              ;
                      
            
            
              object
            
            
               linkAddr 
            
            
              =
            
            
               
            
            
              @"
            
            
              http://www.cnblogs.com/lantionzy
            
            
              "
            
            
              ;
               Microsoft.Office.Interop.Word.Hyperlink myLink 
            
            
              =
            
            
               myLinks.Add(myRange, 
            
            
              ref
            
            
               linkAddr,
                      Microsoft.Office.Interop.Word.Hyperlink myLink 
            
            
              =
            
            
               myLinks.Add(myRange, 
            
            
              ref
            
            
               linkAddr,
               ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing);
               WordApp.ActiveWindow.Selection.InsertAfter(
            
            
              "
            
            
              \n
            
            
              "
            
            
              );
                      WordApp.ActiveWindow.Selection.InsertAfter(
            
            
              "
            
            
              \n
            
            
              "
            
            
              );
               
               //
            
            
              落款
                      
            
            
              //
            
            
              落款
            
            
               WordDoc.Paragraphs.Last.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              文档创建时间:
            
            
              "
            
            
               
            
            
              +
            
            
               DateTime.Now.ToString();
            
            
                      WordDoc.Paragraphs.Last.Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              文档创建时间:
            
            
              "
            
            
               
            
            
              +
            
            
               DateTime.Now.ToString();
               WordDoc.Paragraphs.Last.Alignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
                      WordDoc.Paragraphs.Last.Alignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
               
               //
            
            
              保存
                      
            
            
              //
            
            
              保存
            
            
               WordDoc.Save();
            
            
                      WordDoc.Save();
               WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               return
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              true
            
            
              ;
               }
                  }
            
          
          
             catch
          
          
             (Exception e)
                
          
          
            catch
          
          
             (Exception e)
             {
                
          
          
            
              {
               Console.WriteLine(e.Message);
                      Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);
                      Console.WriteLine(e.StackTrace);
               return
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              false
            
            
              ;
               }
                  }
            
          
          
             }
            }
          
        
        
           #endregion
        
      
      
         设置文档格式并添加文本内容、超链接
        
        
          #endregion
        
      
      
         设置文档格式并添加文本内容、超链接
      
    
  4、添加图片
 
    
       #region
        
        
           文档中添加图片
      
        
          #region
        
        
           文档中添加图片
        
        
           ///
          
          
             
          
          
            <summary>
        
        
          
            ///
          
          
             
          
          
            <summary>
          
          
             ///
          
          
             文档中添加图片
          
          
            ///
          
          
             文档中添加图片
             ///
          
          
             
          
          
            </summary>
          
          
            ///
          
          
             
          
          
            </summary>
          
          
             ///
          
          
             
          
          
            <param name="filePath">
          
          
            word文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="filePath">
          
          
            word文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <param name="picPath">
          
          
            picture文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="picPath">
          
          
            picture文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <returns></returns>
          
          
            ///
          
          
             
          
          
            <returns></returns>
          
        
         public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddPicture(
        
        
          string
        
        
           filePath, 
        
        
          string
        
        
           picPath)
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddPicture(
        
        
          string
        
        
           filePath, 
        
        
          string
        
        
           picPath)
           {
        
        
          
            {
             try
                
          
          
            try
          
          
             {
                
          
          
            
              {
               Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
                      Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
               Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
                      Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
               WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               filename 
            
            
              =
            
            
               filePath;
                      
            
            
              object
            
            
               filename 
            
            
              =
            
            
               filePath;
               Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
                      Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               
               //
            
            
              移动光标文档末尾
                      
            
            
              //
            
            
              移动光标文档末尾
            
            
               object
            
            
               count 
            
            
              =
            
            
               WordDoc.Paragraphs.Count;
            
            
                      
            
            
              object
            
            
               count 
            
            
              =
            
            
               WordDoc.Paragraphs.Count;
               object
            
            
               WdLine 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
                      
            
            
              object
            
            
               WdLine 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
               WordApp.Selection.MoveDown(
            
            
              ref
            
            
               WdLine, 
            
            
              ref
            
            
               count, 
            
            
              ref
            
            
               oMissing);
            
            
              //
            
            
              移动焦点
                      WordApp.Selection.MoveDown(
            
            
              ref
            
            
               WdLine, 
            
            
              ref
            
            
               count, 
            
            
              ref
            
            
               oMissing);
            
            
              //
            
            
              移动焦点
            
            
               WordApp.Selection.TypeParagraph();
            
            
              //
            
            
              插入段落
            
            
                      WordApp.Selection.TypeParagraph();
            
            
              //
            
            
              插入段落
            
            
               
            
            
               object
            
            
               LinkToFile 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              object
            
            
               LinkToFile 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
               object
            
            
               SaveWithDocument 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              object
            
            
               SaveWithDocument 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               Anchor 
            
            
              =
            
            
               WordDoc.Application.Selection.Range;
                      
            
            
              object
            
            
               Anchor 
            
            
              =
            
            
               WordDoc.Application.Selection.Range;
               WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, 
            
            
              ref
            
            
               LinkToFile, 
            
            
              ref
            
            
               SaveWithDocument, 
            
            
              ref
            
            
               Anchor);
                      WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, 
            
            
              ref
            
            
               LinkToFile, 
            
            
              ref
            
            
               SaveWithDocument, 
            
            
              ref
            
            
               Anchor);
               
               //
            
            
              保存
                      
            
            
              //
            
            
              保存
            
            
               WordDoc.Save();
            
            
                      WordDoc.Save();
               WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               return
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              true
            
            
              ;
               }
                  }
            
          
          
             catch
          
          
             (Exception e)
                
          
          
            catch
          
          
             (Exception e)
             {
                
          
          
            
              {
               Console.WriteLine(e.Message);
                      Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);
                      Console.WriteLine(e.StackTrace);
               return
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              false
            
            
              ;
               }
                  }
            
          
          
             }
            }
          
        
        
           #endregion
        
      
      
         文档中添加图片
        
        
          #endregion
        
      
      
         文档中添加图片
      
    
  5、表格处理(插入表格、设置格式、填充内容、表格中加图片)
 
    
       #region
        
        
           表格处理(插入表格、设置格式、填充内容)
      
        
          #region
        
        
           表格处理(插入表格、设置格式、填充内容)
        
        
           ///
          
          
             
          
          
            <summary>
        
        
          
            ///
          
          
             
          
          
            <summary>
          
          
             ///
          
          
             表格处理
          
          
            ///
          
          
             表格处理
             ///
          
          
             
          
          
            </summary>
          
          
            ///
          
          
             
          
          
            </summary>
          
          
             ///
          
          
             
          
          
            <param name="filePath">
          
          
            word文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="filePath">
          
          
            word文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <returns></returns>
          
          
            ///
          
          
             
          
          
            <returns></returns>
          
        
         public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddTable(
        
        
          string
        
        
           filePath)
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           AddTable(
        
        
          string
        
        
           filePath)
           {
        
        
          
            {
             try
                
          
          
            try
          
          
             {
                
          
          
            
              {
               Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
                      Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
               Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
                      Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
               WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               filename 
            
            
              =
            
            
               filePath;
                      
            
            
              object
            
            
               filename 
            
            
              =
            
            
               filePath;
               Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                      Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               
               //
            
            
              插入表格
                      
            
            
              //
            
            
              插入表格
            
            
               Microsoft.Office.Interop.Word.Table newTable 
            
            
              =
            
            
               WordDoc.Tables.Add(WordApp.Selection.Range, 
            
            
              12
            
            
              , 
            
            
              3
            
            
              , 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
            
            
                      Microsoft.Office.Interop.Word.Table newTable 
            
            
              =
            
            
               WordDoc.Tables.Add(WordApp.Selection.Range, 
            
            
              12
            
            
              , 
            
            
              3
            
            
              , 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               //
            
            
              设置表格
                      
            
            
              //
            
            
              设置表格
            
            
               newTable.Borders.OutsideLineStyle 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
            
            
                      newTable.Borders.OutsideLineStyle 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
               newTable.Borders.InsideLineStyle 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                      newTable.Borders.InsideLineStyle 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
               newTable.Columns[
            
            
              1
            
            
              ].Width 
            
            
              =
            
            
               100f;
                      newTable.Columns[
            
            
              1
            
            
              ].Width 
            
            
              =
            
            
               100f;
               newTable.Columns[
            
            
              2
            
            
              ].Width 
            
            
              =
            
            
               220f;
                      newTable.Columns[
            
            
              2
            
            
              ].Width 
            
            
              =
            
            
               220f;
               newTable.Columns[
            
            
              3
            
            
              ].Width 
            
            
              =
            
            
               105f;
                      newTable.Columns[
            
            
              3
            
            
              ].Width 
            
            
              =
            
            
               105f;
               
               //
            
            
              填充表格内容
                      
            
            
              //
            
            
              填充表格内容
            
            
               newTable.Cell(
            
            
              1
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              我的简历
            
            
              "
            
            
              ;
            
            
                      newTable.Cell(
            
            
              1
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              我的简历
            
            
              "
            
            
              ;
               //
            
            
              设置单元格中字体为粗体
                      
            
            
              //
            
            
              设置单元格中字体为粗体
            
            
               newTable.Cell(
            
            
              1
            
            
              , 
            
            
              1
            
            
              ).Range.Bold 
            
            
              =
            
            
               
            
            
              2
            
            
              ;
            
            
                      newTable.Cell(
            
            
              1
            
            
              , 
            
            
              1
            
            
              ).Range.Bold 
            
            
              =
            
            
               
            
            
              2
            
            
              ;
               
               //
            
            
              合并单元格
                      
            
            
              //
            
            
              合并单元格
            
            
               newTable.Cell(
            
            
              1
            
            
              , 
            
            
              1
            
            
              ).Merge(newTable.Cell(
            
            
              1
            
            
              , 
            
            
              3
            
            
              ));
            
            
                      newTable.Cell(
            
            
              1
            
            
              , 
            
            
              1
            
            
              ).Merge(newTable.Cell(
            
            
              1
            
            
              , 
            
            
              3
            
            
              ));
               
               //
            
            
              垂直居中
                      
            
            
              //
            
            
              垂直居中
            
            
               WordApp.Selection.Cells.VerticalAlignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
            
            
                      WordApp.Selection.Cells.VerticalAlignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
               //
            
            
              水平居中
                      
            
            
              //
            
            
              水平居中
            
            
               WordApp.Selection.ParagraphFormat.Alignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
            
            
                      WordApp.Selection.ParagraphFormat.Alignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
               
               //
            
            
              填充表格内容
                      
            
            
              //
            
            
              填充表格内容
            
            
               newTable.Cell(
            
            
              2
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              座右铭:
            
            
                      newTable.Cell(
            
            
              2
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              座右铭:
               "
            
            
              ;
            
            
              "
            
            
              ;
               //
            
            
              设置单元格内字体颜色
                      
            
            
              //
            
            
              设置单元格内字体颜色
            
            
               newTable.Cell(
            
            
              2
            
            
              , 
            
            
              1
            
            
              ).Range.Font.Color 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
            
            
                      newTable.Cell(
            
            
              2
            
            
              , 
            
            
              1
            
            
              ).Range.Font.Color 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;
               //
            
            
              合并单元格
                      
            
            
              //
            
            
              合并单元格
            
            
               newTable.Cell(
            
            
              2
            
            
              , 
            
            
              1
            
            
              ).Merge(newTable.Cell(
            
            
              2
            
            
              , 
            
            
              3
            
            
              ));
            
            
                      newTable.Cell(
            
            
              2
            
            
              , 
            
            
              1
            
            
              ).Merge(newTable.Cell(
            
            
              2
            
            
              , 
            
            
              3
            
            
              ));
               WordApp.Selection.Cells.VerticalAlignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
                      WordApp.Selection.Cells.VerticalAlignment 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
               
               //
            
            
              填充表格内容
                      
            
            
              //
            
            
              填充表格内容
            
            
               newTable.Cell(
            
            
              3
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              姓名:
            
            
              "
            
            
              ;
            
            
                      newTable.Cell(
            
            
              3
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              姓名:
            
            
              "
            
            
              ;
               newTable.Cell(
            
            
              3
            
            
              , 
            
            
              2
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              雷鑫
            
            
              "
            
            
              ;
                      newTable.Cell(
            
            
              3
            
            
              , 
            
            
              2
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              雷鑫
            
            
              "
            
            
              ;
               //
            
            
              纵向合并单元格
                      
            
            
              //
            
            
              纵向合并单元格
            
            
               newTable.Cell(
            
            
              3
            
            
              , 
            
            
              3
            
            
              ).Select();
            
            
                      newTable.Cell(
            
            
              3
            
            
              , 
            
            
              3
            
            
              ).Select();
               //
            
            
              选中一行
                      
            
            
              //
            
            
              选中一行
            
            
               object
            
            
               moveUnit 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdUnits.wdLine;
            
            
                      
            
            
              object
            
            
               moveUnit 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdUnits.wdLine;
               object
            
            
               moveCount 
            
            
              =
            
            
               
            
            
              3
            
            
              ;
                      
            
            
              object
            
            
               moveCount 
            
            
              =
            
            
               
            
            
              3
            
            
              ;
               object
            
            
               moveExtend 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                      
            
            
              object
            
            
               moveExtend 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
               WordApp.Selection.MoveDown(
            
            
              ref
            
            
               moveUnit, 
            
            
              ref
            
            
               moveCount, 
            
            
              ref
            
            
               moveExtend);
                      WordApp.Selection.MoveDown(
            
            
              ref
            
            
               moveUnit, 
            
            
              ref
            
            
               moveCount, 
            
            
              ref
            
            
               moveExtend);
               WordApp.Selection.Cells.Merge();
                      WordApp.Selection.Cells.Merge();
               
               //
            
            
              表格中插入图片
                      
            
            
              //
            
            
              表格中插入图片
            
            
               string
            
            
               pictureFileName 
            
            
              =
            
            
               System.IO.Directory.GetCurrentDirectory() 
            
            
              +
            
            
               
            
            
              @"
            
            
              \picture.jpg
            
            
              "
            
            
              ;
            
            
                      
            
            
              string
            
            
               pictureFileName 
            
            
              =
            
            
               System.IO.Directory.GetCurrentDirectory() 
            
            
              +
            
            
               
            
            
              @"
            
            
              \picture.jpg
            
            
              "
            
            
              ;
               object
            
            
               LinkToFile 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              object
            
            
               LinkToFile 
            
            
              =
            
            
               
            
            
              false
            
            
              ;
               object
            
            
               SaveWithDocument 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              object
            
            
               SaveWithDocument 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               Anchor 
            
            
              =
            
            
               WordDoc.Application.Selection.Range;
                      
            
            
              object
            
            
               Anchor 
            
            
              =
            
            
               WordDoc.Application.Selection.Range;
               WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, 
            
            
              ref
            
            
               LinkToFile, 
            
            
              ref
            
            
               SaveWithDocument, 
            
            
              ref
            
            
               Anchor);
                      WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, 
            
            
              ref
            
            
               LinkToFile, 
            
            
              ref
            
            
               SaveWithDocument, 
            
            
              ref
            
            
               Anchor);
               //
            
            
              图片宽度
                      
            
            
              //
            
            
              图片宽度
            
            
               WordDoc.Application.ActiveDocument.InlineShapes[
            
            
              1
            
            
              ].Width 
            
            
              =
            
            
               100f;
            
            
                      WordDoc.Application.ActiveDocument.InlineShapes[
            
            
              1
            
            
              ].Width 
            
            
              =
            
            
               100f;
               //
            
            
              图片高度
                      
            
            
              //
            
            
              图片高度
            
            
               WordDoc.Application.ActiveDocument.InlineShapes[
            
            
              1
            
            
              ].Height 
            
            
              =
            
            
               100f;
            
            
                      WordDoc.Application.ActiveDocument.InlineShapes[
            
            
              1
            
            
              ].Height 
            
            
              =
            
            
               100f;
               //
            
            
              将图片设置为四周环绕型
                      
            
            
              //
            
            
              将图片设置为四周环绕型
            
            
               Microsoft.Office.Interop.Word.Shape s 
            
            
              =
            
            
               WordDoc.Application.ActiveDocument.InlineShapes[
            
            
              1
            
            
              ].ConvertToShape();
            
            
                      Microsoft.Office.Interop.Word.Shape s 
            
            
              =
            
            
               WordDoc.Application.ActiveDocument.InlineShapes[
            
            
              1
            
            
              ].ConvertToShape();
               s.WrapFormat.Type 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
                      s.WrapFormat.Type 
            
            
              =
            
            
               Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
               
               newTable.Cell(
            
            
              12
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              备注:
            
            
              "
            
            
              ;
                      newTable.Cell(
            
            
              12
            
            
              , 
            
            
              1
            
            
              ).Range.Text 
            
            
              =
            
            
               
            
            
              "
            
            
              备注:
            
            
              "
            
            
              ;
               newTable.Cell(
            
            
              12
            
            
              , 
            
            
              1
            
            
              ).Merge(newTable.Cell(
            
            
              12
            
            
              , 
            
            
              3
            
            
              ));
                      newTable.Cell(
            
            
              12
            
            
              , 
            
            
              1
            
            
              ).Merge(newTable.Cell(
            
            
              12
            
            
              , 
            
            
              3
            
            
              ));
               //
            
            
              在表格中增加行
                      
            
            
              //
            
            
              在表格中增加行
            
            
               WordDoc.Content.Tables[
            
            
              1
            
            
              ].Rows.Add(
            
            
              ref
            
            
               oMissing);
            
            
                      WordDoc.Content.Tables[
            
            
              1
            
            
              ].Rows.Add(
            
            
              ref
            
            
               oMissing);
               
               //
            
            
              保存
                      
            
            
              //
            
            
              保存
            
            
               WordDoc.Save();
            
            
                      WordDoc.Save();
               WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               return
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              true
            
            
              ;
               }
                  }
            
          
          
             catch
          
          
             (Exception e)
                
          
          
            catch
          
          
             (Exception e)
             {
                
          
          
            
              {
               Console.WriteLine(e.Message);
                      Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);
                      Console.WriteLine(e.StackTrace);
               return
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              false
            
            
              ;
               }
                  }
            
          
          
             }
            }
          
        
        
           #endregion
        
      
      
         #region 表格处理
        
        
          #endregion
        
      
      
         #region 表格处理
      
    
  6、把Word文档转化为Html文件
 
    
       #region
        
        
           把Word文档转化为Html文件
      
        
          #region
        
        
           把Word文档转化为Html文件
        
        
           ///
          
          
             
          
          
            <summary>
        
        
          
            ///
          
          
             
          
          
            <summary>
          
          
             ///
          
          
             把Word文档转化为Html文件
          
          
            ///
          
          
             把Word文档转化为Html文件
             ///
          
          
             
          
          
            </summary>
          
          
            ///
          
          
             
          
          
            </summary>
          
          
             ///
          
          
             
          
          
            <param name="wordFileName">
          
          
            word文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="wordFileName">
          
          
            word文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <param name="htmlFileName">
          
          
            要保存的html文件名
          
          
            </param>
          
          
            ///
          
          
             
          
          
            <param name="htmlFileName">
          
          
            要保存的html文件名
          
          
            </param>
          
          
             ///
          
          
             
          
          
            <returns></returns>
          
          
            ///
          
          
             
          
          
            <returns></returns>
          
        
         public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           WordToHtml(
        
        
          string
        
        
           wordFileName, 
        
        
          string
        
        
           htmlFileName)
        
          public
        
        
           
        
        
          static
        
        
           
        
        
          bool
        
        
           WordToHtml(
        
        
          string
        
        
           wordFileName, 
        
        
          string
        
        
           htmlFileName)
           {
        
        
          
            {
             try
                
          
          
            try
          
          
             {
                
          
          
            
              {
               Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
                      Object oMissing 
            
            
              =
            
            
               System.Reflection.Missing.Value;
               Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
                      Microsoft.Office.Interop.Word._Application WordApp 
            
            
              =
            
            
               
            
            
              new
            
            
               Application();
               WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
                      WordApp.Visible 
            
            
              =
            
            
               
            
            
              true
            
            
              ;
               object
            
            
               filename 
            
            
              =
            
            
               wordFileName;
                      
            
            
              object
            
            
               filename 
            
            
              =
            
            
               wordFileName;
               Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
                      Microsoft.Office.Interop.Word._Document WordDoc 
            
            
              =
            
            
               WordApp.Documents.Open(
            
            
              ref
            
            
               filename, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing,
               ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                          
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               
               //
            
            
               Type wordType = WordApp.GetType();
                      
            
            
              //
            
            
               Type wordType = WordApp.GetType();
               //
            
            
               打开文件
                      
            
            
              //
            
            
               打开文件
            
            
               Type docsType 
            
            
              =
            
            
               WordApp.Documents.GetType();
            
            
                      Type docsType 
            
            
              =
            
            
               WordApp.Documents.GetType();
               //
            
            
               转换格式,另存为
                      
            
            
              //
            
            
               转换格式,另存为
            
            
               Type docType 
            
            
              =
            
            
               WordDoc.GetType();
            
            
                      Type docType 
            
            
              =
            
            
               WordDoc.GetType();
               object
            
            
               saveFileName 
            
            
              =
            
            
               htmlFileName;
                      
            
            
              object
            
            
               saveFileName 
            
            
              =
            
            
               htmlFileName;
               docType.InvokeMember(
            
            
              "
            
            
              SaveAs
            
            
              "
            
            
              , System.Reflection.BindingFlags.InvokeMethod, 
            
            
              null
            
            
              , WordDoc,
                      docType.InvokeMember(
            
            
              "
            
            
              SaveAs
            
            
              "
            
            
              , System.Reflection.BindingFlags.InvokeMethod, 
            
            
              null
            
            
              , WordDoc,
               new
            
            
               
            
            
              object
            
            
              [] 
            
            
              
                { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML }
              
            
            
              );
                          
            
            
              new
            
            
               
            
            
              object
            
            
              [] 
            
            
              
                { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML }
              
            
            
              );
            
          
        
      
    
    
      
        
          
            
               #region
                
                
                   其它格式:
                        
              
              
                
                  #region
                
                
                   其它格式:
                
              
               ///
                  
                  
                    wdFormatHTML
                          
                
                
                  
                    ///
                  
                  
                    wdFormatHTML
                     ///
                  
                  
                    wdFormatDocument
                            
                  
                  
                    ///
                  
                  
                    wdFormatDocument
                     ///
                  
                  
                    wdFormatDOSText
                            
                  
                  
                    ///
                  
                  
                    wdFormatDOSText
                     ///
                  
                  
                    wdFormatDOSTextLineBreaks
                            
                  
                  
                    ///
                  
                  
                    wdFormatDOSTextLineBreaks
                     ///
                  
                  
                    wdFormatEncodedText
                            
                  
                  
                    ///
                  
                  
                    wdFormatEncodedText
                     ///
                  
                  
                    wdFormatRTF
                            
                  
                  
                    ///
                  
                  
                    wdFormatRTF
                     ///
                  
                  
                    wdFormatTemplate
                            
                  
                  
                    ///
                  
                  
                    wdFormatTemplate
                     ///
                  
                  
                    wdFormatText
                            
                  
                  
                    ///
                  
                  
                    wdFormatText
                     ///
                  
                  
                    wdFormatTextLineBreaks
                            
                  
                  
                    ///
                  
                  
                    wdFormatTextLineBreaks
                     ///
                  
                  
                    wdFormatUnicodeText
                            
                  
                  
                    ///
                  
                  
                    wdFormatUnicodeText
                  
                
                 //
                
                
                   退出 Word
                
                          
                
                
                  //
                
                
                   退出 Word
                   //
                
                
                  wordType.InvokeMember( "Quit", System.Reflection.BindingFlags.InvokeMethod,
                          
                
                
                  //
                
                
                  wordType.InvokeMember( "Quit", System.Reflection.BindingFlags.InvokeMethod,
                   //
                
                
                      null, WordApp, null );
                          
                
                
                  //
                
                
                      null, WordApp, null );
                
                
                   #endregion
                
                
                          
                
                
                  #endregion
                
              
            
            
               
               //
            
            
              保存
                      
            
            
              //
            
            
              保存
            
            
               WordDoc.Save();
            
            
                      WordDoc.Save();
               WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordDoc.Close(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
                      WordApp.Quit(
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing, 
            
            
              ref
            
            
               oMissing);
               return
            
            
               
            
            
              true
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              true
            
            
              ;
               }
                  }
            
          
          
             catch
          
          
             (Exception e)
                
          
          
            catch
          
          
             (Exception e)
             {
                
          
          
            
              {
               Console.WriteLine(e.Message);
                      Console.WriteLine(e.Message);
               Console.WriteLine(e.StackTrace);
                      Console.WriteLine(e.StackTrace);
               return
            
            
               
            
            
              false
            
            
              ;
                      
            
            
              return
            
            
               
            
            
              false
            
            
              ;
               }
                  }
            
          
          
             }
            }
          
        
        
           #endregion
        
      
      
         把Word文档转化为Html文件
        
        
          #endregion
        
      
      
         把Word文档转化为Html文件
      
    
  
可以从这里下载整个类(包含更多内容)


 
					 
					