office文档转为html

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using PPT=Microsoft.Office.Interop.PowerPoint;
using System.Threading;
using System.IO;

///


///DocConveter 的摘要说明
///文档类型转换
///

public class DocConveter
{
public DocConveter()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public string Doc2HTML(string sourceFile,string destFile)
{
try
{
object filePath = sourceFile;
object readOnly = true;
object addToRecentFiles = false;
object confirmConversion = false;
object revert = true;
object visible = false;

object formate = WdSaveFormat.wdFormatFilteredHTML;
object missing = System.Reflection.Missing.Value;
object savePath = destFile;
object saveChanges = false;
//word引用程序
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
//打开一个文档
Document doc = word.Documents.Open(ref filePath,
ref confirmConversion, ref readOnly, ref addToRecentFiles, ref missing, ref missing, ref revert, ref missing, ref missing, ref missing, ref missing,
ref visible, ref missing, ref missing, ref missing, ref missing);
//另存为网页格式
doc.SaveAs(ref savePath, ref formate, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
word.Quit(ref saveChanges, ref missing, ref missing);
System.Runtime.InteropServices.Marshal.ReleaseComObject(word);
word = null;
GC.Collect();
return "";
}
catch(Exception e)
{
return "文档转换发生错误了!"+e.Message;
};
}
public string Xls2HTML(string sourceFile, string destFile)
{
try
{

Microsoft.Office.Interop.Excel.Application repExcel = new Microsoft.Office.Interop.Excel.Application();//实例化Excel
Microsoft.Office.Interop.Excel.Workbook workbook = null;
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;

workbook = repExcel.Application.Workbooks.Open(sourceFile, 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);//打开文件,n.FullPath是文件路径

worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
object htmlFile = destFile;
object ofmt = Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml;
workbook.SaveAs(htmlFile, 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();
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");//依据时间杀灭进程
foreach (System.Diagnostics.Process p in process)
{
if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
{
p.Kill();
}
}
Thread.Sleep(3000);//保证完全关闭
/*
StreamReader objreader = new StreamReader(htmlFile.ToString().Split('.').GetValue(0) + ".files\\sheet001.html", System.Text.Encoding.GetEncoding("GB2312"));// 以下内容是在Html的第一个框架中添加下载原Excel的超链接
FileStream fs = new FileStream(htmlFile.ToString().Split('.').GetValue(0) + ".files\\sheet001$.html", FileMode.Create);
streamHtmlHelp = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
streamHtmlHelp.WriteLine("源文件下载
");
do
{
str = objreader.ReadLine();
streamHtmlHelp.WriteLine(str);
}
while (str != "");
streamHtmlHelp.Close();
objreader.Close();
File.Delete(htmlFile.ToString().Split('.').GetValue(0) + ".files\\sheet001.html");
File.Move(htmlFile.ToString().Split('.').GetValue(0) + ".files\\sheet001$.html", htmlFile.ToString().Split('.').GetValue(0) + ".files\\sheet001.html");
*/

return "";
}
catch (Exception e)
{
return "文档转换发生错误了!"+e.Message;
}
}

public string PPT2HTML(string sourceFile,

string destFile)
{
try
{
string path; //文件路径变量

path = sourceFile; //路径
PPT.Application pptApp = new PPT.ApplicationClass(); //初始化
PPT.Presentation pptDoc; //Excel文档变量
PPT.Presentation pptDoctmp;

string pathHtml = destFile;
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();
System.Runtime.InteropServices.Marshal.ReleaseComObject(pa);
pa = null;
GC.Collect();
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("POWERPNT");//依据时间杀灭进程
foreach (System.Diagnostics.Process p in process)
{
if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5)
{
p.Kill();
}
}
Thread.Sleep(3000);//保证完全关闭

return "";
}
catch (Exception e)
{
return "文档转换发生错误了!" + e.Message;
}
}
}

相关主题
相关文档
最新文档