I wrote a small program today to convert ppt files into html files. I am afraid I will forget it later. I will record it here and share it with you.
The source code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
Using PPT = Microsoft. Office. Interop. PowerPoint;
Using System. Reflection;
Namespace WritePptDemo
{
Class Program
{
Static void Main (string [] args)
{
String path; // file path variable
PPT. Application pptApp; // Excel Application Variables
PPT. Presentation pptDoc; // Excel document variable
PPT. Presentation pptDoctmp;
Path = @ "C: \ MyPPT.ppt"; // path
PptApp = new PPT. ApplicationClass (); // Initialization
// Delete an existing one
If (File. Exists (string) path ))
{
File. Delete (string) path );
}
// Because the COM library is used, many variables need to be replaced by Nothing.
Object Nothing = Missing. Value;
PptDoc = pptApp. Presentations. Add (Microsoft. Office. Core. MsoTriState. msoFalse );
PptDoc. Slides. Add (1, Microsoft. Office. Interop. PowerPoint. PpSlideLayout. ppLayoutText );
String text = "sample text ";
Foreach (PPT. Slide slide in pptDoc. Slides)
{
Foreach (PPT. Shape shape in slide. Shapes)
{
Shape. TextFrame. TextRange. InsertAfter (text );
}
}
// WdSaveFormat is the Save format of the Excel document
PPT. PpSaveAsFileType format = PPT. PpSaveAsFileType. ppSaveAsDefault;
// Save the content of the excelDoc object as an XLSX document
PptDoc. SaveAs (path, format, Microsoft. Office. Core. MsoTriState. msoFalse );
// Close the excelDoc object
PptDoc. Close ();
// Close the excelApp Component Object
PptApp. Quit ();
Console. WriteLine (path + "created! ");
Console. ReadLine ();
String pathHtml = @ "c: \ MyPPT.html ";
PPT. Application pa = new PPT. ApplicationClass ();
PptDoctmp = pa. presentations. open (path, Microsoft. office. core. msoTriState. msoTrue, Microsoft. office. core. msoTriState. msoFalse, Microsoft. office. core. msoTriState. msoFalse );
PPT. PpSaveAsFileType formatTmp = PPT. PpSaveAsFileType. ppSaveAsHTML;
PptDoctmp. SaveAs (pathHtml, formatTmp, Microsoft. Office. Core. MsoTriState. msoFalse );
PptDoctmp. Close ();
Pa. Quit ();
Console. WriteLine (pathHtml + "created! ");
}
}
}
The above program uses C # To first create a ppt file and write text to it, and then convert the ppt to html. What needs to be explained for the above program is
To reference dll, add references to the project and select microsoft powerpoint 11.0 object library from the com component. If your computer does not have office 2003 installed, if office 2007 is installed, microsoft powerpoint 12.0 object library is installed. And even if you reference it successfully, it will still fail to compile because office PIA is rarely installed. During office installation, if you choose a typical installation, this will not be installed, this is only applicable to developers. You can download an office PIA installation on the Internet.