Office COM组件--写Word的小程序

来源:互联网 发布:windows me安装教程 编辑:程序博客网 时间:2024/06/06 03:50
今天本是周末,回到家,父母已经准备好吃的,很幸福,但一看Yuki还很忙,原来是在做一个很繁琐的工作,将一个1600行的Excel表信息整理为Word文档,FT,这不废了吗,每个30秒都得10多个小时,这叫什么事啊,为了减轻其工作量,我说这个事交给我吧。
 
于是按此做了一个程序
1.将Excel表导入数据库中;
2.将Excel表数据读出,每一条按照规定格式写入Word文档
 
具体为:
1.在Visual Studio 2003中新建一个C#的控制台程序
2.添加Office COM组件引用,具体包括Microsoft Word 11.0 Object Library(Interop.Word),Microsoft Office 11.0 Object Library(Microsoft Interop Core),当然前提是安装了Office2003;
3.调用Word模板,创建Word临时文档,主要使用Word.Application 和 Word.Document;
4.按照格式写入内容,主要使用Word.Paragraph和oPara1.Range.Font。
5.往Word中写东西时用到了标签的概念,即从这个地方插入文字,这个需要在dot模板建立时通过菜单项“插入--书签”来预先设置。
 
花了两个半小时,代码能跑就满足了,最终获得的Word文档长达440页,可见这个活不是人干的。我很高兴,呵呵。
 
核心代码如下
---------------------创建Word文档的方法------------------------------
/// <summary>
  /// 打开Word文档,并且返回对象wDoc,wDoc
  /// </summary>
  /// <param name="FileName">完整Word文件路径+名称</param> 
  /// <param name="wDoc">返回的Word.Document wDoc对象</param>
  /// <param name="WApp">返回的Word.Application对象</param>
  public static void CreateWordDocument(string FileName,ref Word.Document wDoc,ref Word.Application WApp)
  {
   if(FileName == "") return;
   Word.Document thisDocument = null;
   Word.FormFields   formFields = null;
   Word.Application thisApplication = new Word.ApplicationClass();
   thisApplication.Visible = true;
   thisApplication.Caption = "";
   thisApplication.Options.CheckSpellingAsYouType = false;
   thisApplication.Options.CheckGrammarAsYouType = false;
   Object filename = FileName;
   Object ConfirmConversions = false;
   Object ReadOnly = true;
   Object AddToRecentFiles = false;
   Object PasswordDocument = System.Type.Missing;
   Object PasswordTemplate = System.Type.Missing;
   Object Revert = System.Type.Missing;
   Object WritePasswordDocument = System.Type.Missing;
   Object WritePasswordTemplate = System.Type.Missing;
   Object Format = System.Type.Missing;
   Object Encoding = System.Type.Missing;
   Object Visible = System.Type.Missing;
   Object OpenAndRepair = System.Type.Missing;
   Object DocumentDirection =  System.Type.Missing;
   Object NoEncodingDialog = System.Type.Missing;
   Object XMLTransform = System.Type.Missing;
   try
   {
    Word.Document wordDoc =
     thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
     ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
     ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
     ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
     ref NoEncodingDialog, ref XMLTransform );
   
    thisDocument = wordDoc;
    wDoc = wordDoc;
    WApp = thisApplication;
    formFields = wordDoc.FormFields;
   }
   catch(Exception ex)
   {
    Console.WriteLine(ex.ToString());
    
  }
--------------------------------------------------------------------------
 
-----------------创建文档并写入的代码--------------------------------
private static void WordWriteData()
  {
    Object Nothing = System.Reflection.Missing.Value;
  
   Word.Document wDoc=null;
   Word.Application wApp=null;
   CreateWordDocument("D:/Temp/test.dot",ref wDoc,ref wApp);
      wDoc = wApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
   ArrayList lstEntity = GetStaffInfo();
   foreach (Hashtable ht in lstEntity)
   {
    object bkmG = "TWaterTable3";
    object unit;
    object count = 1; //移动数
    object extend;
    object WdLine = Word.WdUnits.wdLine;//换一行;
    object wStyle;
    object wFont;
    wFont = new FontClass();
   
   
    unit = Word.WdUnits.wdCell;
    extend = Word.WdMovementType.wdExtend;
    string sObject = (string)ht["object"];
    string sCity = (string)ht["city"];
    string sName = (string)ht["name"];
    string sFirstName = (string)ht["first"];
    string sFamilyName = (string)ht["family"];
    string sCorp = (string)ht["corp"];
    string sPosition = (string)ht["postion"];
    string sResume = (string)ht["resume"];
    string sMark =(string)ht["mark"];
    if (sName == "" && (sFirstName != "" || sFamilyName != ""))
    {
     sName = sFirstName + " " +sFamilyName;
    }
    //对标签"C"进行填充
    object bkmC="C";
    if(wApp.ActiveDocument.Bookmarks.Exists("C") == true)
    {
     wApp.ActiveDocument.Bookmarks.get_Item
      (ref bkmC).Select();
    }
 
    Word.Paragraph oPara1;
    oPara1 = wDoc.Content.Paragraphs.Add(ref Nothing);
    oPara1.Range.Text = "【" +sName +"】";
    oPara1.Range.Font.Bold = 1;
    oPara1.Range.Font.Size =12;
    oPara1.Range.Font.Name="宋体";
    oPara1.Range.Font.Color=Word.WdColor.wdColorBlack;
    oPara1.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
    oPara1.Range.InsertParagraphAfter();
//    Word.Paragraph oPara2;
//    oPara2 = wDoc.Content.Paragraphs.Add(ref Nothing);
//    oPara2.Range.Text = sName ;
//    oPara2.Range.Font.Bold = 1;
//    oPara2.Range.Font.Size =12;
//    oPara2.Range.Font.Name="宋体";
//    oPara2.Range.Font.Color=Word.WdColor.wdColorRed;
//    oPara2.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
//    oPara2.Range.InsertParagraphAfter();
//
//
//    Word.Paragraph oPara3;
//    oPara3 = wDoc.Content.Paragraphs.Add(ref Nothing);
//    oPara3.Range.Text = "】" ;
//    oPara3.Range.Font.Bold = 1;
//    oPara3.Range.Font.Size =12;
//    oPara3.Range.Font.Name="宋体";
//    oPara3.Range.Font.Color=Word.WdColor.wdColorBlack;
//    oPara3.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
//    oPara3.Range.InsertParagraphAfter();
    Word.Paragraph oPara4;
    oPara4 = wDoc.Content.Paragraphs.Add(ref Nothing);
    oPara4.Range.Text = "来源:" + sObject;
    oPara4.Range.Font.Bold = 1;
    oPara4.Range.Font.Size =12;
    oPara4.Range.Font.Name="宋体";
    oPara4.Range.Font.Color=Word.WdColor.wdColorBlack;
    oPara4.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
    oPara4.Range.InsertParagraphAfter();
    Word.Paragraph oPara5;
    oPara5 = wDoc.Content.Paragraphs.Add(ref Nothing);
    oPara5.Range.Text = sPosition ;
    oPara5.Range.Font.Bold = 1;
    oPara5.Range.Font.Size =12;
    oPara5.Range.Font.Name="宋体";
    oPara5.Range.Font.Color=Word.WdColor.wdColorBlack;
    oPara5.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
    oPara5.Range.InsertParagraphAfter();
    Word.Paragraph oPara6;
    oPara6 = wDoc.Content.Paragraphs.Add(ref Nothing);
    oPara6.Range.Text = "    "+sResume ;
    oPara6.Range.Font.Bold = 0;
    oPara6.Range.Font.Size =12;
    oPara6.Range.Font.Name="宋体";
    oPara6.Range.Font.Color=Word.WdColor.wdColorBlack;
    oPara6.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
    oPara6.Range.InsertParagraphAfter();

    Word.Paragraph oPara7;
    oPara7 = wDoc.Content.Paragraphs.Add(ref Nothing);
    oPara7.Range.Text = "    "+ sMark ;
    oPara7.Range.Font.Bold = 0;
    oPara7.Range.Font.Size =12;
    oPara7.Range.Font.Name="宋体";
    oPara7.Range.Font.Color=Word.WdColor.wdColorBlack;
    oPara7.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
    oPara7.Range.InsertParagraphAfter();
    Word.Paragraph oPara8;
    oPara8 = wDoc.Content.Paragraphs.Add(ref Nothing);
    oPara8.Range.Text = "";
    oPara8.Range.Font.Bold = 1;
    oPara8.Range.Font.Size =24;
    oPara8.Range.Font.Name="宋体";
    oPara8.Range.Font.Color=Word.WdColor.wdColorBlack;
    oPara8.Format.SpaceAfter = 1;    //24 pt spacing after paragraph.
    oPara8.Range.InsertParagraphAfter();
}
 
-------------------------------------------------------------------------
 
在EXcel数据导入数据库时,可能是由于某些字段文字太多,出现了一个问题,如下:
---------------------SQL导入错误信息-----------------------------------------------
   Error at Source for Row number 50. Errors encountered so far in this task: 1.
   Data for source column 10 ('Mark') is too large for the specified buffer size.
   Data for source column 9 ('Resume') is too large for the specified buffer size.
------------------------------------------------------------------------------------------
 
最终查询了一下资料,通过先将Excel另存为txt格式,再将txt按照TAB区分导入数据库得以解决。
原创粉丝点击