Office COM component-word writing Applet

Source: Internet
Author: User
It was a weekend. When I got home, my parents were prepared and happy. But Yuki was still very busy. It turned out to be a very tedious job, organizing a 1600-row Excel table into Word documents, ft. Isn't that a waste? It takes more than 10 hours every 30 seconds. What is this? To reduce the workload, let me tell you this. So I made a program based on this. 1. Import the Excel table into the database; 2. Read the Excel table data and write each entry into the word document in the specified format. Specifically: 1. create a console program C # in Visual Studio 2003. add an office COM component reference, including Microsoft Word 11.0 Object Library (InterOP. word), Microsoft Office 11.0 Object Library (Microsoft InterOP core), of course, the premise is that the installation of office2003; 3. call the Word template to create a temporary Word document, mainly using word. application and word. document; 4. write content according to the format, mainly using word. paragraph and opara1.range. font. 5. The label concept is used when writing to the word, that is, to insert text from this place, which needs to be pre-set through the menu item "insert-bookmarks" when the dot template is created. It took two and a half hours for the code to run and the Final Word documents were as long as 440 pages. It can be seen that this job was not done by humans. I'm very happy. The core code is as follows: --------------------- how to create a Word document -------------------------- // <summary>   /// Open the Word document and return the wdoc and wdoc objects.    /// </Summary>    /// <Param name = "FILENAME"> complete Word file path + name </param>     /// <Param name = "wdoc"> the returned word. Document wdoc object </param>    /// <Param name = "wapp"> returned word. Application Object </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 ());
      }  
    }-------------------------------------------------------------------------- ----------------- Code for creating a document and writing it -------------------------------- 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 );      Arraylist lstentity = getstaffinfo ();      Foreach (hashtable Ht in lstentity)
      {
        Object bkmg = "twatertable3 ";
        Object Unit;
        Object COUNT = 1; // number of moves
        Object extend;
        Object wdline = word. wdunits. wdline; // change the line;        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;
        }
        // Fill the label "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 = "Source:" + 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 ();} ------------------------------------------------------------------------- When importing Excel data to the database, it may be because some fields have too many characters and there is a problem, as shown below -----------------------------------------------   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.Bytes ------------------------------------------------------------------------------------------ Finally, after querying the information, you can save the Excel file as a TXT file, and then import the TXT file to the database by tab.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.