c#操作word(二)[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

namespace SchDeposit.FuncClass
{
 /// <summary>
 /// EnDoc 的摘要说明。
 /// </summary>
 public class EnDoc
 {
  public EnDoc()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  [DllImport ("user32.dll")]  
  public static extern int MessageBox(int h,string m,string c,int type);
  Word.ApplicationClass app = new Word.ApplicationClass();
  object optional=Missing.Value;
  object visible=true;
  object saveChanges = true;
  object NOTsaveChanges = false;
  object docreadonly=true;
  object originalFormat = Missing.Value;
  object routeDocument = Missing.Value;
  _Document doc=null;
  //  FillCtl f=new FillCtl ();

  private string dotpath;
  public  string DotPath
  {
   get
   {
    return dotpath;
   }
   set
   {
    dotpath=value;
   }
  }  
  private string GetData(string str)
  {
   if (str==null)
    str="";
   else
    str=str.Trim();
   return str;
  }

  #region ******打开文件*******
  public bool OpenFile()
  {
   try
   {
    if (File.Exists(dotpath)==true)
    {
     object fileName = dotpath;
     doc = app.Documents.Open(ref fileName,ref optional,ref docreadonly,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref optional, ref visible);
     return true;
    }
    else
     MessageBox(0,"文件不存在!" ,"Error Message Box",0);
    return  false;
   }
   catch (System.Exception e)
   {
    throw new Exception (e.Message );
   }
  }
  #endregion

  #region ******写入数据*******
  public void WriteData(ref object lbl,string txt)
  {//向标签lbl中写入数据txt
   try
   {
    doc.Bookmarks.Item(ref lbl).Select();
    doc.Application.Selection.TypeText(txt.Trim());
   }
   catch (System.Exception e)
   {
    doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
    throw new Exception (e.Message );
   }
  }
  public void WriteData(DataTable dt)
  {//将DataTable的第一行数据顺序写入文档的标签
   try
   { 
    if (dt.Rows.Count >=1)
    {
     object[] lbl=GetMark ();
     if (lbl.Length ==dt.Columns.Count)
      for (int i=0;i<lbl.Length;i++ )
       WriteData (ref lbl[i],dt.Rows[0][i].ToString ()); 
     else
      MessageBox(0, "长度不符" ,"Error Message Box",0);
    }
   }
   catch (System.Exception e)
   {
    doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
    throw new Exception (e.Message );
   }
  }

  #endregion

本文关键:c#操作word(二)
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top