C # reading table information in Word

Source: Internet
Author: User

In many cases, a lot of information is stored in Word documents. We need to extract the information and use it for other purposes. The Word format is Ms confidential. I don't know if there are any Nb users who can analyze the livestream. I don't have the ability or the intention. Therefore, only components provided by Ms can be used for programming. However, Ms does not provide hosted class libraries, but provides Pia conversion for COM components. For more information, see http://blog.joycode.com/kaneboy/articles/67688.aspxin the kaneboy's blog. The expert's explanation is very clear.
What I want to do is to extract the table information in the Word document. It is difficult to find relatedCode(Open an existing document and analyze its content), but I think this kind of work is very meaningful. I wrote a small demo, as shown below:

Object ofilename = @ "C: \ Documents ents and Settings \ liush \ My Documents ents \ testdoc.doc ";
Object oreadonly = true;
Object omissing = system. reflection. Missing. value;

Word. _ application oword;
Word. _ document odoc;
Oword = new word. Application ();
Oword. Visible = true; // only for easy observation
Odoc = oword. Documents. Open (ref ofilename, ref omissing, ref oreadonly, ref omissing, ref omissing,
Ref omissing, ref omissing, ref omissing );

// MessageBox. Show (odoc. Tables. Count. tostring ());
For (INT tablepos = 1; tablepos <= odoc. Tables. Count; tablepos ++)
{
Word. Table nowtable = odoc. Tables. Item (tablepos );
String tablemessage = string. Format ("tables {0}/{1}: \ n", tablepos, odoc. Tables. Count );

For (INT rowpos = 1; rowpos <= nowtable. Rows. Count; rowpos ++)
{
For (INT columpos = 1; columpos <= nowtable. Columns. Count; columpos ++)
{
Tablemessage + = nowtable. Cell (rowpos, columpos). range. text;
Tablemessage = tablemessage. Remove (tablemessage. Length-2, 2); // remove \ r \
Tablemessage + = "\ t ";
}

Tablemessage + = "\ n ";
}

MessageBox. Show (tablemessage );
}

If you have read the kaneboyArticle(This is one of the series). It is not difficult to understand this code. Open an existing document and traverse all the tables. The information is simply displayed. In practice, you can analyze the information. After completing these steps, I finally found some official support documents:
Http://msdn2.microsoft.com/zh-CN/library/y1xatbkd.aspx
Among them, word tasks have simple code examples for various word operations, written in VB and C. After reading this, I think everyone will understand that VB's support for com is a little clearer than C. (You can see this http://blog.joycode.com/kaneboy/archive/2005/08/03/61489.aspx) the same code, with VB to open the Word document operations, the Code is as follows:

Dim filename as string = "C: \ Documents ents and Settings \ liush \ My Documents ents \ testdoc.doc"
Dim isreadonly as Boolean = true

Dim wordapplication as word. Application = new word. Application ()
Dim worddocument as word. Document
Wordapplication. Visible = true
Worddocument = wordapplication. Documents. Open (filename, isreadonly)

So the next time I want to perform the com operation, I will return to my lovely VB. However, C # problems that have been used for too long are getting more and more, and it is customary to add brackets and add points...

PS: these jobs help my mom. Their data room wants to store the data directory information into the database, and then establish a website for convenient retrieval and management. These contents are originally stored in Word documents. This was their first digital result, when they turned a lot of paper materials into Word documents. In their understanding at the time, digitalization was just to convert the original format into Word documents. Now they have lost patience with these difficult maintenance and retrieval tasks and began to consider databases and networks. Maybe digitalization is also a process of gradual transformation of thinking.

Related Article

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.