用.net生成Word文件

using System;
using Office;
using System.IO;

namespace YSJ.PublicClass
{
 /// <summary>
 /// Word 的摘要说明。
 /// </summary>
 public class WordClass
 {

  Word.Application wordApp=null;
  Word.Document wordDoc=null;

  
  object Nothing=System.Reflection.Missing.Value;
  object format=Word.WdSaveFormat.wdFormatDocument;
  object objt=true;
  object unit = Word.WdUnits.wdStory ;
  object pBreak= (int)Word.WdBreakType.wdPageBreak;
  
  public WordClass()
  {
   
  }
  /// <summary>
  /// 创建Word对象
  /// </summary>
  public void CreateWordApp()
  {
   object DAltert=false;
   wordApp=new Word.ApplicationClass();
   wordApp.DisplayAlerts=Word.WdAlertLevel.wdAlertsNone;
  }

  /// <summary>
  /// 将图片添加到Word文件中
  /// </summary>
  /// <param name="strImgPath">图片路径</param>
  public void AddImgToWord(string strImgPath)
  {
   //图片
   if (System.IO.File.Exists(strImgPath))
   {
     wordApp.Selection.EndKey( ref unit, ref Nothing);
    wordApp.Selection.InlineShapes.AddPicture(strImgPath,ref Nothing,ref objt,ref Nothing);
    wordApp.Selection.Range.InsertAfter(""+'/n');
   }
  }
  /// <summary>
  /// 在当前光标位置初插入图片
  /// </summary>
  /// <param name="strImgPath">图片路径</param>
  public void AddImgToWordLocation(string strImgPath)
  {
   //图片
   if (System.IO.File.Exists(strImgPath))
   {
    wordApp.Selection.Range.InlineShapes.AddPicture(strImgPath,ref Nothing,ref objt,ref Nothing);
   }
  }
  
  /// <summary>
  /// 在特定的行中插入指定的内容 并回车
  /// </summary>
  /// <param name="Title">内容</param>
  /// <param name="strFontName">字体</param>
  /// <param name="FontSize">字体大小</param>
  public void InsertCaption(string Title,string strFontName,float FontSize,bool Center)
  {
   Word.Range Rg=wordApp.Selection.Range;

   Rg.InsertBefore('/n'+Title);
   Rg.Font.Name=strFontName;
   Rg.Font.Size=FontSize;
   if(Center)
   {
    Rg.Paragraphs.Alignment=Word.WdParagraphAlignment.wdAlignParagraphCenter;
   }
  }

  /// <summary>
  /// 插入回车
  /// </summary>
  /// <param name="How">回车个数</param>
  public void InsertEnter(int How)
  {
   for(int i=0;i<=How;i++)
   {
    wordApp.Selection.Range.InsertBefore(""+'/n');
   }
  }
  
  /// <summary>
  /// 用Word打开文件
  /// </summary>
  /// <param name="strFilePath">文件路径</param>
  public void OpenFileToWord(string strFilePath)
  {
   
   if (System.IO.File.Exists(strFilePath))
   {
    object srcFileName=@strFilePath;
    object wdDelete=Word.WdDeleteCells.wdDeleteCellsEntireColumn;
    object wdCount=1;
    wordDoc=wordApp.Documents.Open(ref srcFileName,ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    wordApp.Selection.TypeBackspace();
    wordApp.Selection.TypeBackspace();
    wordApp.Selection.Range.InsertBreak(ref pBreak );
   }
  }
  
  /// <summary>
  /// 关闭Word对象
  /// </summary>
  public void CloseWord()
  {
   wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
   wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  }

  /// <summary>
  /// 保存成Word文件
  /// </summary>
  /// <param name="strWordFilePath"></param>
  public void SaveWord(string strWordFilePath)
  {
   //将htm文件save as成doc文件
   object dstFileName=@strWordFilePath;
   wordDoc.SaveAs(ref dstFileName,ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing,  ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);   
  }

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值