如何将HTML格式转成MHT格式?
usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Text;
usingSystem.IO;
namespaceHTMLConversion
...{
/**////<summary>
///HtmlConvertToMht类
///</summary>
HtmlConvertToMht类的实现#regionHtmlConvertToMht类的实现
publicclassHtmlConvertToMhtByCdo:IHtmlConvertToMht
...{
publicHtmlConvertToMhtByCdo()
...{
//
//TODO:在此处添加构造函数逻辑
//
}
变量#region变量
string_CharSet="gb2312";
#endregion
属性#region属性
publicstringCharSet
...{
get
...{
return_CharSet;
}
set
...{
_CharSet=value;
}
}
#endregion
方法#region方法
/**////<summary>
///把Html格式二进制流转换成Mht格式的二进制流
///</summary>
///<paramname="htmlUrl">Html格式的二进制流</param>
///<returns>Mht格式的二进制流</returns>
publicbyte[]ConvertHtmlUrlToMht(stringhtmlUrl)
...{
stringstrMhtContent=string.Empty;
byte[]BytMhtContent=null;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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);
returnBytMhtContent;
}
/**////<summary>
///把Html格式二进制流转换成Mht格式的二进制流,成功返回true,失败返回false
///</summary>
///<paramname="htmlUrl">Html格式的二进制流</param>
///<paramname="filePath">Mht文件的保存路径</param>
///<returns>操作结果成功:true,失败;false</returns>
publicboolConvertHtmlUrlToMht(stringhtmlUrl,stringfilePath)
...{
boolisConvertHtmlBytesToMht=false;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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;
returnisConvertHtmlBytesToMht;
}
/**////<summary>
///把Html格式二进制流转换成Mht格式的二进制流
///</summary>
///<paramname="htmlContent">Html格式的二进制流</param>
///<returns>Mht格式的二进制流</returns>
publicbyte[]ConvertHtmlBytesToMht(byte[]htmlContent)
...{
stringstrMhtContent=string.Empty;
stringstrHtmlContent=Encoding.Default.GetString(htmlContent);
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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);
returnBytMhtContent;
}
/**////<summary>
///根据输入的Html文本内容(二进制)返回Mht文本内容(二进制流),成功返回true,失败返回false
///</summary>
///<paramname="htmlContent">Html格式的二进制流</param>
///<paramname="filePath">Mht文件的保存路径</param>
///<returns>操作结果成功:true,失败;false</returns>
publicboolConvertHtmlBytesToMht(byte[]htmlContent,stringfilePath)
...{
boolisConvertHtmlBytesToMht=false;
stringstrHtmlContent=Encoding.GetEncoding(_CharSet).GetString(htmlContent);
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=null;
message.MimeFormatted=true;
message.HTMLBody=strHtmlContent;
stream=message.GetStream();
stream.Charset=_CharSet;
stringstrMhtContent=stream.ReadText(stream.Size);
byte[]BytMhtContent=Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
FileStreamfileStream=newFileStream(filePath,FileMode.Create,FileAccess.Write);
fileStream.Write(BytMhtContent,0,BytMhtContent.Length);
message=null;
stream.Close();
isConvertHtmlBytesToMht=true;
returnisConvertHtmlBytesToMht;
}
/**////<summary>
///根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流)
///</summary>
///<paramname="htmlContent">Html文本内容(字符串)</param>
///<returns>Mht格式的二进制流</returns>
publicbyte[]ConvertHtmlTextToMht(stringhtmlContent)
...{
stringstrMhtContent=String.Empty;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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);
returnbytMhtContent;
}
///<summary>
///根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流),成功返回true,失败返回false
///</summary>
///<paramname="htmlContent">Html文本内容(字符串)</param>
///<paramname="filePath">Mht文件的保存路径</param>
///<returns>操作结果:成功:true,失败:false</returns>
publicboolConvertHtmlTextToMht(stringhtmlContent,stringfilePath)
...{
boolisConvertHtmlBytesToMht=false;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=null;
message.MimeFormatted=true;
message.HTMLBody=htmlContent;
stream=message.GetStream();
stream.Charset=_CharSet;
stringstrMhtContent=stream.ReadText(stream.Size);
usingSystem.Runtime.InteropServices;
usingSystem.Text;
usingSystem.IO;
namespaceHTMLConversion
...{
/**////<summary>
///HtmlConvertToMht类
///</summary>
HtmlConvertToMht类的实现#regionHtmlConvertToMht类的实现
publicclassHtmlConvertToMhtByCdo:IHtmlConvertToMht
...{
publicHtmlConvertToMhtByCdo()
...{
//
//TODO:在此处添加构造函数逻辑
//
}
变量#region变量
string_CharSet="gb2312";
#endregion
属性#region属性
publicstringCharSet
...{
get
...{
return_CharSet;
}
set
...{
_CharSet=value;
}
}
#endregion
方法#region方法
/**////<summary>
///把Html格式二进制流转换成Mht格式的二进制流
///</summary>
///<paramname="htmlUrl">Html格式的二进制流</param>
///<returns>Mht格式的二进制流</returns>
publicbyte[]ConvertHtmlUrlToMht(stringhtmlUrl)
...{
stringstrMhtContent=string.Empty;
byte[]BytMhtContent=null;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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);
returnBytMhtContent;
}
/**////<summary>
///把Html格式二进制流转换成Mht格式的二进制流,成功返回true,失败返回false
///</summary>
///<paramname="htmlUrl">Html格式的二进制流</param>
///<paramname="filePath">Mht文件的保存路径</param>
///<returns>操作结果成功:true,失败;false</returns>
publicboolConvertHtmlUrlToMht(stringhtmlUrl,stringfilePath)
...{
boolisConvertHtmlBytesToMht=false;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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;
returnisConvertHtmlBytesToMht;
}
/**////<summary>
///把Html格式二进制流转换成Mht格式的二进制流
///</summary>
///<paramname="htmlContent">Html格式的二进制流</param>
///<returns>Mht格式的二进制流</returns>
publicbyte[]ConvertHtmlBytesToMht(byte[]htmlContent)
...{
stringstrMhtContent=string.Empty;
stringstrHtmlContent=Encoding.Default.GetString(htmlContent);
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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);
returnBytMhtContent;
}
/**////<summary>
///根据输入的Html文本内容(二进制)返回Mht文本内容(二进制流),成功返回true,失败返回false
///</summary>
///<paramname="htmlContent">Html格式的二进制流</param>
///<paramname="filePath">Mht文件的保存路径</param>
///<returns>操作结果成功:true,失败;false</returns>
publicboolConvertHtmlBytesToMht(byte[]htmlContent,stringfilePath)
...{
boolisConvertHtmlBytesToMht=false;
stringstrHtmlContent=Encoding.GetEncoding(_CharSet).GetString(htmlContent);
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=null;
message.MimeFormatted=true;
message.HTMLBody=strHtmlContent;
stream=message.GetStream();
stream.Charset=_CharSet;
stringstrMhtContent=stream.ReadText(stream.Size);
byte[]BytMhtContent=Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
FileStreamfileStream=newFileStream(filePath,FileMode.Create,FileAccess.Write);
fileStream.Write(BytMhtContent,0,BytMhtContent.Length);
message=null;
stream.Close();
isConvertHtmlBytesToMht=true;
returnisConvertHtmlBytesToMht;
}
/**////<summary>
///根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流)
///</summary>
///<paramname="htmlContent">Html文本内容(字符串)</param>
///<returns>Mht格式的二进制流</returns>
publicbyte[]ConvertHtmlTextToMht(stringhtmlContent)
...{
stringstrMhtContent=String.Empty;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=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);
returnbytMhtContent;
}
///<summary>
///根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流),成功返回true,失败返回false
///</summary>
///<paramname="htmlContent">Html文本内容(字符串)</param>
///<paramname="filePath">Mht文件的保存路径</param>
///<returns>操作结果:成功:true,失败:false</returns>
publicboolConvertHtmlTextToMht(stringhtmlContent,stringfilePath)
...{
boolisConvertHtmlBytesToMht=false;
CDO.Messagemessage=newCDO.MessageClass();
ADODB.Streamstream=null;
message.MimeFormatted=true;
message.HTMLBody=htmlContent;
stream=message.GetStream();
stream.Charset=_CharSet;
stringstrMhtContent=stream.ReadText(stream.Size);
本文地址:http://www.45fan.com/dnjc/72576.html