110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using SXT.Common.Win;
//引用office Com组件
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;
namespace ExperienceCenter
{
/// <summary>
/// Office文档转换类,通过文件上传方式得到上传的文档后,调用此方法生成Html静态Office文件
/// </summary>
public class OfficeHtmlConvert
{
/// <summary>
/// 将PPT文件转换成HTML格式
/// </summary>
/// <param name="PptFilePath">PPT文件路径</param>
public static void PptToHtmlFile(string PptFilePath, string targetPath)
{
Microsoft.Office.Interop.PowerPoint.Application ppt = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation pptFile = null;
try
{
//获得html文件名
string htmlFileName = targetPath;
//打开一个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();
}
}
/// <summary>
/// 将Excel文件转换成HTML格式
/// </summary>
/// <param name="ExcelFilePath">Excel文件路径</param>
public static void ExcelToHtmlFile(string ExcelFilePath, string targetPath)
{
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 = targetPath;
//为了保险,只读方式打开
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();
}
}
/// <summary>
/// 将Word文档转换成HTML格式
/// </summary>
/// <param name="WordFilePath">Word文档格式</param>
public static void WordToHtmlFile(string WordFilePath, string targetPath)
{
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 = targetPath;
//为了保险,只读方式打开
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();
}
}
/// <summary>
/// 生成文件对应的HTML版本(没有考虑HTML文件已经存在的处理)
/// </summary>
/// <param name="fileFullName">文件路径</param>
/// <returns>如果生成了对应的HTML文件,返回true,如果不需要生成HTML文件,返回flase</returns>
public static bool ConvertFileToHtml(string fileFullName, string targetPath)
{
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, targetPath);
return true;
case "xls":
case "xlsx":
ExcelToHtmlFile(fileFullName, targetPath);
return true;
case "ppt":
case "pptx":
PptToHtmlFile(fileFullName, targetPath);
return true;
default:
return false;
}
}
return false;
}
}
}