Convert office documents to TXT documents

Source: Internet
Author: User

After the code network is obtained, make a slight modification and use the DLL file obtained from vs2008. office2007 also supports another PDF file. The following code uses this logic:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using Microsoft. Office. core;
Using Microsoft. Office. InterOP. word;
Using Microsoft. Office. InterOP. Excel;
Using Microsoft. Office. InterOP. PowerPoint;
Using system. IO;

Namespace consoleapplication1
{
Public interface fileconvert
{
Void convert (string inputfilepath, string outputfilepath );
}
Public class fileconvertfactory
{
Private fileconvertfactory (){}
Public static fileconvert createfileconvert (string inputfilepath)
{
Fileconvert;
String extension = path. getextension (inputfilepath );
Switch (extension. tolower ())
{
Case (". xls "):
Case (". XLSX "):
Fileconvert = new excelfileconvert ();
Break;
Case (". ppt "):
Case (". pptx "):
Fileconvert = new pptfileconvert ();
Break;
Case (". Doc "):
Case (". docx "):
Default:
Fileconvert = new docfileconvert ();
Break;
}
Return fileconvert;
}
}
Public class docfileconvert: fileconvert
{
Public void convert (string inputfilepath, string outputfilepath)
{
Microsoft. Office. InterOP. Word. applicationclass wordapp = new Microsoft. Office. InterOP. Word. applicationclass ();
Object filename = inputfilepath;
Object missing = type. missing;
// Open the document
_ Document DOC = wordapp. Documents. Open (
Ref filename, ref missing,
Ref missing, ref missing,
Ref missing, ref missing,
Ref missing, ref missing );
Object saveformat = Microsoft. Office. InterOP. Word. wdsaveformat. wdformattextlinebreaks;
// Object saveencoding = msoencoding. msoencodingutf8;
Filename = outputfilepath;
// Save as a TXT document
Doc. saveas (
Ref filename, ref saveformat, ref missing, ref missing,
Ref missing, ref missing,
Ref missing, ref missing,
Ref missing, ref missing );
Doc. Close (ref missing, ref missing, ref missing );
Wordapp. Quit (ref missing, ref missing, ref missing );
}
}

Public class excelfileconvert: fileconvert
{
Public void convert (string inputfilepath, string outputfilepath)
{
Microsoft. Office. InterOP. Excel. applicationclass wordapp = new Microsoft. Office. InterOP. Excel. applicationclass ();
Object filename = inputfilepath;
Object missing = type. missing;
Workbook Doc = wordapp. workbooks. Open (
Inputfilepath, missing,
Missing, missing,
Missing, missing,
Missing, missing, missing );
Object saveformat = Microsoft. Office. InterOP. Excel. xlfileformat. xlunicodetext;
Object saveresolution = xlsaveconflictresolution. xllocalsessionchanges;
Doc. saveas (
Outputfilepath, saveformat, missing, missing,
Missing, missing, xlsaveasaccessmode. xlnochange, saveresolution,
Missing, missing );
Doc. Close (missing, missing, missing );
Wordapp. Quit ();
}
}
Public class pptfileconvert: fileconvert
{
Public void convert (string inputfilepath, string outputfilepath)
{
Microsoft. Office. InterOP. PowerPoint. applicationclass wordapp = new Microsoft. Office. InterOP. PowerPoint. applicationclass ();
Object filename = inputfilepath;
Object missing = type. missing;
Presentation Doc = wordapp. Presentations. Open (
Inputfilepath, msotristate. msoctrue, msotristate. msoctrue, msotristate. msofalse );
Object saveformat = ppsaveasfiletype. ppsaveasrtf;
Object saveresolution = xlsaveconflictresolution. xllocalsessionchanges;
String tempfile = path. getdirectoryname (outputfilepath) + datetime. Now. tofiletime () + "tmp. rtf ";
// Save as an RTF File
Doc. saveas (tempfile, ppsaveasfiletype. ppsaveasrtf, msotristate. msofalse );
Wordapp. Quit ();
// Convert it to a TXT file
Docfileconvert newconvert = new docfileconvert ();
Newconvert. Convert (tempfile, outputfilepath );
File. Delete (tempfile );
}
}
}

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.