Obtain the word count in C # and output it to excel.

Source: Internet
Author: User
Reference http://www.microsoft.com/china/msdn/library/office/office/UndstaExcelObjModNETDev.mspx
I have been registering for a while and have never had time to write something. I am so sorry.
Recently, my wife needs to count the words in a large number of Word documents.
The number of words in multiple Word documents is small. Program .
The number of words (or characters) used to read a word file. You must first reference the word object and Excel object.
// Open multiple Word files and obtain the number of words
Word. applicationclass thisapplication = new word. applicationclass ();
Object missingvalue = type. missing;
Object Mytrue = true;
Int count;
Object filename;
Foreach (listviewitem item in listview1.items) // list of multiple Word documents
{
Count = 0;
Filename = item. subitems [0]. text;
Word. Document thisdocument = thisapplication. Documents. Open (ref filename, ref missingvalue,
Ref Mytrue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue, ref missingvalue, ref missingvalue,
Ref missingvalue );
Word. Range RNG = thisdocument. content;
RNG. Select ();
Count = thisdocument. characters. count;
}
Thisdocument. Close (ref missingvalue, ref missingvalue, ref missingvalue );

// Save to an Excel file
Void savetoexcel ()
{
System. Windows. Forms. savefiledialog file = new savefiledialog ();
File. defaultext = ". Doc ";
File. Filter = "excle file (*. xls) | *. xls | all files (*. *) | *.*";
File. Title = "select the location to save the file ";
If (file. showdialog () = system. Windows. Forms. dialogresult. OK)
{
Excel. applicationclass thisapplication = new excel. applicationclass ();
Excel. Workbook workbook = thisapplication. workbooks. Add (type. Missing );
Excel. worksheet newworksheet;
Newworksheet =
(Excel. worksheet) Workbook. worksheets. Add (
Type. Missing, type. Missing );
Int I = 1;
Foreach (listviewitem item in listview1.items)
{
Excel. Range = (Excel. Range) newworksheet. cells [I, 1];
Range. value2 = item. subitems [0]. text;
Range = (Excel. Range) newworksheet. cells [I, 2];
Range. value2 = item. subitems [1]. text;
I ++;
}
Foreach (Excel. Workbook book in thisapplication. workbooks)
{
Book. savecopyas (file. filename );
Book. Close (false, type. Missing, type. Missing );
}
}
}

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.