搜索 | 会员  
将Word,Excel转换成Html
来源: 博客园   作者:凌鸢  日期:2015/3/11  类别:编程语言  主题:.NET  编辑:Maxwell
首先,需要了解VBA编程,它是Office外部编程接口,我用的是Office2003所以添加引用是MicrosoftOffice11.0ObjectLibrary,否则无法操作Office的

首先,需要了解的是VBA编程,它是Office提供的外部编程接口,我采用的是Office2003所以添加的引用是Microsoft Office 11.0 Object Library 这个Com组件,否则无法操作Office的。下面开始:

Word -〉Html

Microsoft.Office.Interop.Word.ApplicationClass appclass = new Microsoft.Office.Interop.Word.ApplicationClass();//实例化一个Word
Type wordtype = appclass.GetType();
Microsoft.Office.Interop.Word.Documents docs = appclass.Documents;//获取Document
Type docstype = docs.GetType();
object filename = Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath;//n.FullPath为Word文件的路径
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docstype.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,new object[]{filename,true,true});//打开文件
Type doctype = doc.GetType();
object savefilename = Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath.Split('.').GetValue(0)+".html";
doctype.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,null,doc,new object[] {savefilename,Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML});// 另存为Html格式
wordtype.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,appclass,null);//退出
Thread.Sleep(3000);//为了使退出完全,这里阻塞3秒
StreamReader objreader = new StreamReader(savefilename.ToString(),System.Text.Encoding.GetEncoding("GB2312"));  // 以下内容是为了在Html中加入对本身Word文件的下载       
FileStream fs = new FileStream(savefilename.ToString().Split('.').GetValue(0).ToString()+"$.html",FileMode.Create);
streamHtmlHelp = new System.IO.StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
streamHtmlHelp.WriteLine("<a href=\""+n.Text+"\">源文件下载</a><br>");
do 
{
    str = objreader.ReadLine();
    streamHtmlHelp.WriteLine(str);
}
while (str !=  "</html>");
streamHtmlHelp.Close();
objreader.Close();
File.Delete(savefilename.ToString());
File.Move(savefilename.ToString().Split('.').GetValue(0).ToString()+"$.html",savefilename.ToString());

Excel -〉Html(这个与Word不同,因为Excel为很多层,又并列很多层,必须都清空才能销毁实例,但实际中我发现并不是每次都能销毁,所以网上求解多次没有结果,只能杀没进程,为了保证只杀灭最近的进程,我用时间进行判断)

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(Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath,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   =   Application.StartupPath+@"\ccyt_chm\inform\"+n.FullPath.Split('.').GetValue(0)+".html";   
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("<a href=\""+"../"+n.Text+"\">源文件下载</a><br>");
do 
{
    str = objreader.ReadLine();
    streamHtmlHelp.WriteLine(str);
}
while (str !=  "</html>");
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");


德仔网尊重行业规范,每篇文章都注明有明确的作者和来源;德仔网的原创文章,请转载时务必注明文章作者和来源:德仔网;
头条那些事
大家在关注
广告那些事
我们的推荐
也许感兴趣的
干货