C# html轉mht

項目中引入cdosys.dll組件,位置:  C:\Windows\System32\cdosys.dllhtml

CDO、ADODB 這些類都來自於cdosys.dll組件orm

/// <summary>
/// 把Html格式二進制流轉換成Mht格式的二進制流
/// </summary>
/// <param name="htmlUrl">Html格式的二進制流</param>
/// <returns>Mht格式的二進制流</returns>
public static byte[] ConvertHtmlUrlToMht(string htmlUrl)
{
string _CharSet = "gb2312";
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 static bool ConvertHtmlUrlToMht(string htmlUrl, string filePath)
{
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;
}htm

相關文章
相關標籤/搜索