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);
}
}
}