/// /// 将PPT文件转换成HTML格式 ///
/// PPT文件路径 public static void PptToHtmlFile(string PptFilePath) {
Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptFile = null; try {
//获得html文件名
string htmlFileName = PptFilePath.Substring(0, PptFilePath.LastIndexOf(\".\")) + \".html\";
//打开一个ppt文件
pptFile = ppt.Presentations.Open(PptFilePath, Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoCTrue, Microsoft.Office.Core.MsoTriState.msoFalse);
//转换成html格式
pptFile.SaveAs(htmlFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML,
Microsoft.Office.Core.MsoTriState.msoCTrue); }
finally {
if (pptFile != null) {
pptFile.Close(); }
ppt.Quit(); GC.Collect(); } }
/// /// 将Excel文件转换成HTML格式 ///
/// Excel文件路径 public static void ExcelToHtmlFile(string ExcelFilePath) {
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook oBook = null; // 缺省参数
object Unknown = Type.Missing; try {
//目标html文件路径
object Target = ExcelFilePath.Substring(0, ExcelFilePath.LastIndexOf(\".\")) + \".html\";
//为了保险,只读方式打开 object readOnly = true; // 指定另存为格式(html)
object format = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
//打开Excel文件
oBook = excelApp.Workbooks.Open(ExcelFilePath, Unknown, readOnly,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown,
Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);
// 转换格式
oBook.SaveAs(Target, format, Unknown, Unknown, Unknown, Unknown,
Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
Unknown, Unknown, Unknown, Unknown, Unknown);
}
finally {
if (oBook != null) {
oBook.Close(false, Unknown, Unknown); }
excelApp.Quit(); GC.Collect(); } }
/// /// 将Word文档转换成HTML格式 ///
/// Word文档格式 public static void WordToHtmlFile(string WordFilePath) {
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = null; // 缺省参数
object Unknown = Type.Missing; try {
// 指定原文件和目标文件 object Source = WordFilePath;
object Target = WordFilePath.Substring(0, WordFilePath.LastIndexOf(\".\")) + \".html\";
//为了保险,只读方式打开 object readOnly = true; // 指定另存为格式(html)
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
// 打开doc文件
doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
// 转换格式
doc.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); }
finally {
if (doc != null) {
// 关闭文档和Word程序
doc.Close(ref Unknown, ref Unknown, ref Unknown);
}
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
GC.Collect(); } }
有了这三个方法,我们可以在上传文件时将对应的office文件转成html的文件,跟office文件放在同一个目录中,同时加一个对应的链接“HTML方式查看”即可。
如果是第二期添加这个功能,那可以在获得文件列表的时候,先取得文件的扩展名,然后再查看有没有对应的HTML文件,如果没有,则即时生成一个,很方便,不过如果量很大的话,转换的速度会比较慢,最好是写一个winform的程序遍历整个目录,先把html文件生成一遍,这样再打开就会很快了。
简单的写一个如下:
/// /// 生成文件对应的HTML版本(没有考虑HTML文件已经存在的处理) ///
/// 文件路径
///
public static bool ConvertFileToHtml(string fileFullName) {
System.IO.FileInfo file = new System.IO.FileInfo(fileFullName); if (file.Exists) {
string strExt = file.Extension.Substring(1).ToLower();
switch (strExt) {
case \"doc\": case \"docx\":
WordToHtmlFile(fileFullName); return true; case \"xls\": case \"xlsx\":
ExcelToHtmlFile(fileFullName); return true; case \"ppt\": case \"pptx\":
PptToHtmlFile(fileFullName); return true; default:
}
return false; } }
return false;
这样就OK了
因篇幅问题不能全部显示,请点此查看更多更全内容