【代码片段】把Html格式二进制流转换成Mht格式的二进制流

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace QuickEAS.CoreLib
{
    public class LibHtmlConvertToMhtByCDO
    {
        #region 变量

        string _CharSet = "utf-8";

        #endregion

        #region 属性
        public string CharSet
        {
            get
            {
                return _CharSet;
            }
            set
            {
                _CharSet = value;
            }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 把Html格式二进制流转换成Mht格式的二进制流
        /// </summary>
        /// <param name="htmlUrl">Html格式的二进制流 </param>
        /// <returns>Mht格式的二进制流 </returns>
        public byte[] ConvertHtmlUrlToMht(string htmlUrl)
        {
            string strMhtContent = string.Empty;
            byte[] BytMhtContent = null;
            CDO.Message message = new CDO.MessageClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.CreateMHTMLBody(htmlUrl, CDO.CdoMHTMLFlags.cdoSuppressNone, "", "");
            stream = message.GetStream();
            stream.Charset = _CharSet;
            strMhtContent = stream.ReadText(stream.Size);
            BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
            return BytMhtContent;
        }
        /// <summary>
        /// 把Html格式二进制流转换成Mht格式的二进制流,成功返回true,失败返回false
        /// </summary>
        /// <param name="htmlUrl">Html格式的二进制流 </param>
        /// <param name="filePath">Mht文件的保存路径 </param>
        /// <returns>操作结果 成功:true,失败;false </returns>
        public bool ConvertHtmlUrlToMht(string htmlUrl, string filePath)
        {
            bool result = false;
            try
            {
                CDO.Message msg = new CDO.MessageClass();
                msg.MimeFormatted = true;
                CDO.Configuration cfg = new CDO.ConfigurationClass();
                msg.Configuration = cfg;
                msg.CreateMHTMLBody(htmlUrl, CDO.CdoMHTMLFlags.cdoSuppressNone, "", "");
                msg.GetStream().SaveToFile(filePath, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
                result = true;
            }
            catch (System.Exception ex)
            {
            }
            return result;
            //bool isConvertHtmlBytesToMht = false;
            //CDO.Message message = new CDO.MessageClass();
            //ADODB.Stream stream = null;
            //message.MimeFormatted = true;
            //message.CreateMHTMLBody(htmlUrl, CDO.CdoMHTMLFlags.cdoSuppressNone, "", "");
            //stream = message.GetStream();
            //stream.SaveToFile(filePath, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
            //message = null;
            //stream.Close();
            //isConvertHtmlBytesToMht = true;
            //return isConvertHtmlBytesToMht;
        }
        /// <summary>
        /// 把Html格式二进制流转换成Mht格式的二进制流
        /// </summary>
        /// <param name="htmlContent">Html格式的二进制流 </param>
        /// <returns>Mht格式的二进制流 </returns>
        public byte[] ConvertHtmlBytesToMht(byte[] htmlContent)
        {
            string strMhtContent = string.Empty;
            string strHtmlContent = Encoding.Default.GetString(htmlContent);
            CDO.Message message = new CDO.MessageClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.HTMLBody = strHtmlContent;
            stream = message.GetStream();
            stream.Charset = _CharSet;
            strMhtContent = stream.ReadText(stream.Size);
            byte[] BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
            return BytMhtContent;
        }
        /// <summary>
        /// 根据输入的Html文本内容(二进制)返回Mht文本内容(二进制流),成功返回true,失败返回false
        /// </summary>
        /// <param name="htmlContent">Html格式的二进制流 </param>
        /// <param name="filePath">Mht文件的保存路径 </param>
        /// <returns>操作结果 成功:true,失败;false </returns>
        public bool ConvertHtmlBytesToMht(byte[] htmlContent, string filePath)
        {
            bool isConvertHtmlBytesToMht = false;
            string strHtmlContent = Encoding.GetEncoding(_CharSet).GetString(htmlContent);
            CDO.Message message = new CDO.MessageClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.HTMLBody = strHtmlContent;
            stream = message.GetStream();
            stream.Charset = _CharSet;
            string strMhtContent = stream.ReadText(stream.Size);
            byte[] BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
            FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            fileStream.Write(BytMhtContent, 0, BytMhtContent.Length);
            message = null;
            stream.Close();
            isConvertHtmlBytesToMht = true;
            return isConvertHtmlBytesToMht;
        }
        /// <summary>
        /// 根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流)
        /// </summary>
        /// <param name="htmlContent">Html文本内容(字符串) </param>
        /// <returns>Mht格式的二进制流 </returns>
        public string ConvertHtmlTextToMhtString(string htmlContent)
        {
            string strMhtContent = String.Empty;
            CDO.Message message = new CDO.MessageClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.HTMLBody = htmlContent;
            stream = message.GetStream();
            stream.Charset = _CharSet;
            strMhtContent = stream.ReadText(stream.Size);
            return strMhtContent;
        }
        /// <summary>
        /// 根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流)
        /// </summary>
        /// <param name="htmlContent">Html文本内容(字符串) </param>
        /// <returns>Mht格式的二进制流 </returns>
        public byte[] ConvertHtmlTextToMht(string htmlContent)
        {
            string strMhtContent = String.Empty;
            CDO.Message message = new CDO.MessageClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.HTMLBody = htmlContent;
            stream = message.GetStream();
            stream.Charset = _CharSet;
            strMhtContent = stream.ReadText(stream.Size);
            byte[] bytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
            return bytMhtContent;
        }
        /// <summary>
        /// 根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流),成功返回true,失败返回false
        /// </summary>
        /// <param name="htmlContent">Html文本内容(字符串) </param>
        /// <param name="filePath">Mht文件的保存路径 </param>
        /// <returns>操作结果:成功:true,失败:false </returns>
        public bool ConvertHtmlTextToMht(string htmlContent, string filePath)
        {
            bool isConvertHtmlBytesToMht = false;
            CDO.Message message = new CDO.MessageClass();
            ADODB.Stream stream = null;
            message.MimeFormatted = true;
            message.HTMLBody = htmlContent;
            stream = message.GetStream();
            stream.Charset = _CharSet;
            string strMhtContent = stream.ReadText(stream.Size);
            byte[] BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
            FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            fileStream.Write(BytMhtContent, 0, BytMhtContent.Length);
            message = null;
            stream.Close();
            isConvertHtmlBytesToMht = true;
            return isConvertHtmlBytesToMht;
        }
        #endregion 
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值