using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace Web1.Common
{
/// <summary>
/// 文件转换
/// </summary>
public class OfficeDocument
{
/// <summary>
/// Word生成HTML文件
/// </summary>
/// <param name="sourcefilePath">源文档路径</param>
/// <param name="targetfilePath">目标文档路径</param>
/// <returns>返回:true - 成功;false - 失败;</returns>
public static bool WordToHtml(string sourcefilePath, string targetfilePath)
{
bool operationResult = false;
try
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Type wordtype = word.GetType();
Microsoft.Office.Interop.Word.Documents docs = word.Documents;
Type docstype = docs.GetType();
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docstype.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new object[]
{
sourcefilePath, true, true
});
Type doctype = doc.GetType();
doctype.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[]
{
targetfilePath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML
});
doctype.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
wordtype.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
operationResult = true;
}
catch (Exception ex)
{
operationResult = false;
throw new Exception(ex.Message);
}
return operationResult;
}
/// <summary>
/// PPT生成HTML文件
/// </summary>
/// <param name="sourcefilePath">源文档路径</param>
/// <param name="targetfilePath">目标文档路径</param>
/// <returns>返回:true - 成功;false - 失败;</returns>
public static bool PPTtoHtml(string sourcefilePath, string targetfilePath)
{
bool operationResult = false;
try
{
Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation prsPres = ppApp.Presentations.Open(sourcefilePath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
prsPres.SaveAs(targetfilePath, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsHTML, Microsoft.Office.Core.MsoTriState.msoTrue);
prsPres.Close();
ppApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(prsPres);
prsPres = null;
//垃圾回收
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ppApp);
ppApp = null;
GC.Collect();
operationResult = true;
}
catch (Exception ex)
{
operationResult = false;
throw new Exception(ex.Message);
}
return operationResult;
}
/// <summary>
/// Excel生成HTML文件
/// </summary>
/// <param name="sourcefilePath">源文档路径</param>
/// <param name="targetfilePath">目标文档路径</param>
/// <returns>返回:true - 成功;false - 失败;</returns>
public static bool ExcelToHtml(string sourcefilePath, string targetfilePath)
{
bool operationResult = false;
try
{
Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
//打开文件,sourcefilePath是文件路径
workbook = repExcel.Application.Workbooks.Open(sourcefilePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
//进行另存为操作
workbook.SaveAs(targetfilePath, ofmt, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
object osave = false;
//逐步关闭所有使用的对象
workbook.Close(osave, Type.Missing, Type.Missing);
repExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
//垃圾回收
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);
GC.Collect();
System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);
repExcel = null;
GC.Collect();
operationResult = true;
}
catch (Exception ex)
{
operationResult = false;
throw new Exception(ex.Message);
}
return operationResult;
}
}
}