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 );
}
}
}