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
}
}
【代码片段】把Html格式二进制流转换成Mht格式的二进制流
最新推荐文章于 2021-08-26 15:23:10 发布