zoukankan      html  css  js  c++  java
  • C# 将Word,Excel转换成Html

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

  • 相关阅读:
    thinkphp5 tp5 命名空间 报错 Namespace declaration statement has to be the very first statement in the script
    开启 php 错误 提示 php-fpm 重启 nginx 500错误 解决办法 wdlinux lnmp 一键包 php脚本无法解析执行
    js 设置 cookie 定时 弹出层 提示层 下次访问 不再显示 弹窗 getCookie setCookie setTimeout
    php 二维数组 转字符串 implode 方便 mysql in 查询
    nginx 重启 ps -ef|grep nginx kill -HUP 主进程号
    jquery bootstrap help-block input 表单 提示 帮助 信息
    jquery 倒计时 60秒 短信 验证码 js ajax 获取
    jQuery如何获取同一个类标签的所有的值 遍历
    linux下C语言文件操作相关函数
    gcc,gdb用法
  • 原文地址:https://www.cnblogs.com/lingyuan/p/2241136.html
Copyright © 2011-2022 走看看