• C# 将Word,Excel转换成Html


    转:http://skillerguo.blog.163.com/blog/static/1709513920071853219948/

    首先,需要了解的是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");

  • 相关阅读:
    JavaScript初学者应注意的七个细节
    KindEditor 编辑器使用方法
    有关 JavaScript 的 10 件让人费解的事情
    能说明你的Javascript技术很烂的五个原因
    分享10个便利的HTML5/CSS3框架
    现在就使用HTML5的十大原因
    你应该知道的Node.js扩展模块——Hashish
    C++ Tip: How To Get Array Length | Dev102.com
    MPI for Python — MPI for Python v1.3 documentation
    http://construct.readthedocs.org/en/latest/basics.html
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1808325.html
Copyright © 2020-2023  润新知