如何将offcie文档(.doc、.xls、.ppt)转换成mht文档(代码篇)

这段代码展示了如何使用C#通过`Microsoft.HtmlTrans`库将Office文档(.doc、.xls、.ppt)转换成MHT格式。程序首先通过远程对象获取IHtmlTrLoadBalancer,然后利用IHtmlTrLauncher执行转换任务,读取输入文件内容,通过CHICreateHtml方法创建HTML,最后将HTML和附件合并为MHTML。
摘要由CSDN通过智能技术生成
using System;
using Microsoft.HtmlTrans;
using System.Text;
using System.IO;
using System.Collections;

namespace OfficeDocConvertMHTML
{
   public class Conversion
   {
     // 字符串的编码
     protected static Encoding encoding = Encoding.Default;
     // 用于创建IHtmlTrLoadBalancerremoting对象的url
     protected static string strServiceUrl = "http://localhost:8093/HtmlTrLoadBalancer";
     public static void ConvertMHT(string inputfile, string outputfile)
     {
       // 通过url(strServiceUrl)获取一个IHtmlTrLoadBalancerremoting对象
       IHtmlTrLoadBalancer htmlTrLoadBalancer =
          (IHtmlTrLoadBalancer)System.Activator.GetObject(
          typeof(IHtmlTrLoadBalancer),strServiceUrl);
       // 用输入文件名(inputfile)作为一个任务的任务标示(strTask)
       string strTask = inputfile;

       // 根据任务标示(strTask)新建一个任务并获取任务的url(strLauncherUri)
       string strLauncherUri = htmlTrLoadBalancer.StrGetLauncher(strTask);

       // 通过任务的url(strLauncherUri)获取一个IHtmlTrLauncherremoting对象(htmlTrLauncher),
       // 并用这个对象来执行该任务
       IHtmlTrLauncher htmlTrLauncher =
          (IHtmlTrLauncher)System.Activator.GetObject(typeof(IHtmlTrLauncher),strLauncherUri);

       // 接下来是把输入文件(inputfile)的内容读入一个byte数组(bFile)
       byte[] bFile = null;
       FileStream fsInputMht = null;
       BinaryReader bwInputMht = null;
       try
       {
          fsInputMht = new FileStream(inputfile, FileMode.Open);
          bwInputMht = new BinaryReader(fsInputMht, encoding);
          bFile = new byte[fsInputMht.Length];
          for(long i = 0; i < bFile.LongLength; i++)
            bFile[i] = bwInputMht.ReadByte();
          bwInputMht.Close();
          fsInputMht.Close();

       }
       catch(Exception ex)
       {
          bwInputMht.Close();
          fsInputMht.Close();
          throw ex;
       }

       //CHICreateHtml 通过office文档创建HTML文件及其附件
       //CHICreateHtml(
       //string strLauncherUri,         任务的url
        //byte[] rgbFile,             office 文档的二进制内容
       //Microsoft.HtmlTrans.BrowserType bt, 使用浏览类型,该参数是一个枚举类型
       //string strReqFile,           office 文档的路径/url
       //string strTaskName,           任务标示名,HTML转换服务器根据其跟踪该请求
       //int timeout,                 转换超时时间,如果网络状况较差,建议值设大点
       //bool fReturnFileBits          是否返回二进制内容,分别保存在CreateHtmlInforgbMainFile属性和rgrgbThicketFiles属性中
       //);
       CreateHtmlInfo chi = htmlTrLauncher.CHICreateHtml(strLauncherUri, bFile,
          BrowserType.BT_IE4, inputfile, strTask, 120, true);

       // 结束转换任务
       htmlTrLoadBalancer.LauncherTaskCompleted(strLaun
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MHT 文件换为 Excel 可以采用以下步骤: 1. 使用 win32com 库将 MHT 文件打开为 InternetExplorer.Application 对象。 2. 使用 Beautiful Soup 库解析 MHT 文件中的 HTML。 3. 使用 pandas 库将数据导出为 Excel 文件。 以下是一个简单的示例代码,将本地 MHT 文件中的表格数据提取出来,并将其导出为 Excel 文件: ```python import win32com.client as win32 from bs4 import BeautifulSoup import pandas as pd # 创建 COM 对象 ie = win32.Dispatch("InternetExplorer.Application") # 打开 MHT 文件 ie.Navigate(r"C:\path\to\file.mht") # 等待网页加载完成 while ie.ReadyState != 4: pass # 解析 HTML soup = BeautifulSoup(ie.Document.body.innerHTML, 'html.parser') table = soup.find('table') # 提取表格数据 data = [] for row in table.find_all('tr'): cols = row.find_all('td') cols = [col.text.strip() for col in cols] data.append(cols) # 导出为 Excel 文件 df = pd.DataFrame(data) df.to_excel('output.xlsx', index=False, header=False) # 关闭 IE 对象 ie.Quit() ``` 在这个示例中,我们首先使用 win32com 库将 MHT 文件打开为 InternetExplorer.Application 对象。等待网页加载完成后,我们使用 Beautiful Soup 库解析 MHT 文件中的 HTML,并从中提取表格数据。最后,我们使用 pandas 库将数据导出为 Excel 文件。 请注意,使用该方法将 MHT 文件换为 Excel 时,可能会丢失某些网页元素,例如 JavaScript 和 CSS 文件等。同时,在使用本地 MHT 文件时也需要遵守相关规定,避免触犯法律法规。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值