第一步,先制作了一個(gè)很簡(jiǎn)單的html模板。將動(dòng)態(tài)內(nèi)容用諸如“$htmldata[1]”來(lái)代替,等取出數(shù)據(jù)后用Replace函數(shù)進(jìn)行替換。

創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元滑縣做網(wǎng)站,已為上家服務(wù),為滑縣各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
!DOCTYPE?html?PUBLIC?"-//W3C//DTD XHTML 1.0 Transitional//EN"?""
html?xmlns=""
head
meta?http-equiv="Content-Type"?content="text/html; charset=utf-8"?/
title$htmldata[1]/title
style?type="text/css"
*?{
margin:0px;
padding:0px;
}
.article_s{
width:980px;
height:auto;
overflow:hidden;
margin:0?auto;
}
.article_s_t_f{
width:960px;
height:auto;
line-height:?30px;
font-size:?18px;
padding-top:?10px;
text-align:?center;
font-weight:700;
margin:0?auto;
}
.article_s_t_s{
width:960px;
line-height:?30px;
text-align:?center;
font-size:?13px;
border-bottom:1px?dashed?#CCC;
margin:0?auto;
}
.article_s_l{
width:960px;
margin:0?auto;
line-height:28px;
font-size:14px;
padding:10px?0px?10px?0px;
}
.article_s_c{
width:960px;
height:23px;
text-align:center;
margin-bottom:20px;
}
/style
/head
body
div?class="article_s"
div?class="article_s_t_f"$htmldata[2]/div
div?class="article_s_t_s"$htmldata[3]/div
div?class="article_s_l"
$htmldata[4]
/div
/div
/body
/html
第二步:
建立一個(gè)靜態(tài)類,代碼如下:
public?class?Function
{
static?Function() { }
//根據(jù)html生成mht文件,需要引入相應(yīng)的dll,如圖所示
public?static?void?HtmlToMht(string?src,?string?dst)
{
CDO.Message?msg =?new?CDO.MessageClass();
CDO.Configuration?c =?new?CDO.ConfigurationClass();
msg.Configuration = c;
msg.CreateMHTMLBody(src, CDO.CdoMHTMLFlags.cdoSuppressNone,?"",?"");
ADODB.Stream?stream = msg.GetStream();
stream.SaveToFile(dst, ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
}
public?static?void?WriteHtml(string?name,string?content,string?addTime,string?hits,stringuser)//參數(shù)內(nèi)容都是從數(shù)據(jù)庫(kù)讀出來(lái)的文章信息,其中content就是ewebeditor生成的html代碼
{
DateTime?dt =?DateTime.Parse(addTime);//將string型的日期格式轉(zhuǎn)為DateTime型的因?yàn)槟J(rèn)的日期格式不能作為文件名,所以將日期的“:”替換為“-”
string?Temp_Name =?@"D:\Application\Visual Studio 2010\Projects\富文本轉(zhuǎn)Word\富文本轉(zhuǎn)Word\temp\Articles.html";//HTML模板的路徑
string?File_Name =?@"D:\Application\Visual Studio 2010\Projects\富文本轉(zhuǎn)Word\富文本轉(zhuǎn)Word\html\【"?+ dt.ToShortDateString().Replace("/","-") +"】"?+name +?".html";//生成html文件的路徑
string?File_NameM =?@"D:\Application\Visual Studio 2010\Projects\富文本轉(zhuǎn)Word\富文本轉(zhuǎn)Word\html\【"?+ dt.ToShortDateString().Replace("/","-") +"】"?+name +?".mht";//生成mht文件的路徑
string?File_Name2 =?@"D:\Application\Visual Studio 2010\Projects\富文本轉(zhuǎn)Word\富文本轉(zhuǎn)Word\html\【"?+ dt.ToShortDateString().Replace("/",?"-") +?"】"?+ name +?".doc";//生成Word文檔的路徑
StreamReader?sr =?new?StreamReader(Temp_Name);
StringBuilder?htmltext =?new?StringBuilder();
String?line;
while?((line = sr.ReadLine()) !=?null)
{
htmltext.Append(line);//讀取到html模板的內(nèi)容
}
sr.Close();
//替換相應(yīng)的內(nèi)容到指定的位置
htmltext = htmltext.Replace("$htmldata[1]", name);
htmltext = htmltext.Replace("$htmldata[2]", name);
htmltext = htmltext.Replace("$htmldata[3]", ("點(diǎn)擊數(shù):"?+ hits +?" 發(fā)布時(shí)間:"?+ addTime +?"??發(fā)布者:"?+ user));
htmltext = htmltext.Replace("$htmldata[4]", content);
using?(StreamWriter?sw =?new?StreamWriter(File_Name,?false, System.Text.Encoding.GetEncoding("UTF-8")))?//保存地址
{
//生成HTML文件
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
HtmlToMht(File_Name, File_NameM);//因?yàn)閹D片的html直接轉(zhuǎn)為Word的話,圖片會(huì)以引用的形式展示(也就是說(shuō)不是內(nèi)置到word文檔里去的,一旦斷網(wǎng)或?qū)D片放在別的路徑之后,打開word文檔圖片會(huì)顯示不出來(lái),所以通過(guò)折沖的辦法先生成html,然后轉(zhuǎn)換為mht,再轉(zhuǎn)為word)
WordAction.SaveAsWord(File_NameM, File_Name2);//生成word
}
}
建立另外一個(gè)操作word?的靜態(tài)類,代碼如下(代碼都是拷的O(∩_∩)O):
public?class?WordAction
{
public?static?void?SaveAsWord(string?fileName,?string?pFileName)//使用原生方法將mht轉(zhuǎn)換為word文檔,不是那種直接修改后綴名的方式
{
object?missing = System.Reflection.Missing.Value;
object?readOnly =?false;
object?isVisible =?true;
object?file1 = fileName;
object?html1 = pFileName;
object?format =?WdSaveFormat.wdFormatDocument;
ApplicationClass?oWordApp =?new?ApplicationClass();
oWordApp.Visible =?false;
Document?oWordDoc = oWordApp.Documents.Open(ref???file1,?ref???format,?ref??readOnly,ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref??missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref??missing,?ref?missing);
oWordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;//將web視圖修改為默認(rèn)視圖,不然打開word的時(shí)候會(huì)以web視圖去展示,而不是默認(rèn)視圖。(唯獨(dú)這句代碼是自己加的?= =|||)
oWordDoc.SaveAs(ref???html1,?ref???format,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing,ref???missing,?ref???missing,?ref???missing,?ref???missing,?ref???missing);
oWordDoc.Close(ref?????missing,?ref?????missing,?ref?????missing);
oWordDoc =?null;
oWordApp.Application.Quit(ref???missing,?ref???missing,?ref???missing);
oWordApp =?null;
killAllProcess();
}
protected?static?void?killAllProcess()?//?殺掉所有winword.exe進(jìn)程
{
System.Diagnostics.Process[] myPs;
myPs = System.Diagnostics.Process.GetProcesses();
foreach?(System.Diagnostics.Process?p?in?myPs)
{
if?(p.Id != 0)
{
string?myS =?"WINWORD.EXE"?+ p.ProcessName +?"??ID:"?+ p.Id.ToString();
try
{
if?(p.Modules !=?null)
if?(p.Modules.Count 0)
{
System.Diagnostics.ProcessModule?pm = p.Modules[0];
myS +=?"\n Modules[0].FileName:"?+ pm.FileName;
myS +=?"\n Modules[0].ModuleName:"?+ pm.ModuleName;
myS +=?"\n Modules[0].FileVersionInfo:\n"?+ pm.FileVersionInfo.ToString();
if?(pm.ModuleName.ToLower() ==?"winword.exe")
p.Kill();
}
}
catch
{ }
finally
{
}
}
}
}
}
第三步:
隨便建了個(gè)aspx頁(yè)面,寫上以下代碼。
string?title =?"";
string?postuser =?"";
string?content =?"";
string?addTime =?"";
string?hits =?"";
DataTable?dt =?CatalogAccess.GetArticles();//從數(shù)據(jù)庫(kù)取出自己需要的數(shù)據(jù)
for?(int?i = 0; i dt.Rows.Count; i++)
{
DataRow?dr = dt.Rows[i];
title = dr["Title"].ToString();
postuser = dr["PostUser"].ToString();
addTime = dr["AddTime"].ToString();
hits = dr["Hits"].ToString();
content = dr["Content"].ToString();
content = content.Replace("src=\"/new/Editor/uploadfile/",?"src=\"files/");//替換圖片文件的引用目錄,這個(gè)動(dòng)作是非必要的,因?yàn)槲野褕D片都下載到本地了,所以替換一下里面引用的圖片路徑,只要根據(jù)模板生成的html能正常顯示圖片就可以了
Function.WriteHtml(title, content, addTime, hits, postuser);//生成word文檔
}
}
好了,大功告成啦。當(dāng)然生成的word文檔跟網(wǎng)頁(yè)存在一點(diǎn)差別,在接受的范圍內(nèi)。
可以啊,MHT是MIME
HTML的縮寫,是一種用來(lái)保存HTML文件的格式,與HTML不同,它可以將HTML頁(yè)面以及頁(yè)面中連接的圖片文件保存到一個(gè)單一的文件中,非常便于使用和保存。MHT默認(rèn)使用IE瀏覽器打開,你家的電腦無(wú)法打開這個(gè)擴(kuò)展名的文件,是由于相應(yīng)的關(guān)聯(lián)出現(xiàn)了問(wèn)題。請(qǐng)?jiān)谝蜷_的文件上單擊右鍵,選擇打開方式,從中選擇Ineternet
Explorer瀏覽器;或者先打開IE瀏覽器,然后從菜單“文件”-“打開”中找到你的MHT文件,將其載入。另外,在資源管理器中點(diǎn)擊菜單“工具”-“文件夾選項(xiàng)”,于彈出窗口的“文件類型”選項(xiàng)卡下找到擴(kuò)展名為MHT的文件類型,確認(rèn)其打開方式跟它下面的MHTML相同,都是“Internet
Explorer”,這種方法可以一次性的更改MHT的關(guān)聯(lián)。
具體步驟如下:
需要準(zhǔn)備的材料分別是:電腦、
1、首先打開電腦,右鍵單擊MHTML文檔文件選擇打開“重命名”選項(xiàng)。
2、然后在彈出來(lái)的窗口中將后綴名改為“.docx”格式,回車確定。
3、然后在彈出來(lái)的窗口中點(diǎn)擊“是”選項(xiàng)。
4、然后就可以轉(zhuǎn)換成Word文檔了。
新聞標(biāo)題:go語(yǔ)言mht轉(zhuǎn)word go語(yǔ)言如何
網(wǎng)頁(yè)URL:http://chinadenli.net/article24/ddodgce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、企業(yè)建站、網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)