发布网友 发布时间:2022-04-21 20:27
共3个回答
热心网友 时间:2022-04-21 11:57
第一步,先制作了一个很简单的html模板。将动态内容用诸如“$htmldata[1]”来代替,等取出数据后用Replace函数进行替换。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
第二步:
建立一个静态类,代码如下:
public class Function
{
static Function() { }
//根据html生成mht文件,需要引入相应的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)//参数内容都是从数据库读出来的文章信息,其中content就是ewebeditor生成的html代码
{
DateTime dt = DateTime.Parse(addTime);//将string型的日期格式转为DateTime型的因为默认的日期格式不能作为文件名,所以将日期的“:”替换为“-”
string Temp_Name = @"D:\Application\Visual Studio 2010\Projects\富文本转Word\富文本转Word\temp\Articles.html";//HTML模板的路径
string File_Name = @"D:\Application\Visual Studio 2010\Projects\富文本转Word\富文本转Word\html\【" + dt.ToShortDateString().Replace("/","-") +"】" +name + ".html";//生成html文件的路径
string File_NameM = @"D:\Application\Visual Studio 2010\Projects\富文本转Word\富文本转Word\html\【" + dt.ToShortDateString().Replace("/","-") +"】" +name + ".mht";//生成mht文件的路径
string File_Name2 = @"D:\Application\Visual Studio 2010\Projects\富文本转Word\富文本转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模板的内容
}
sr.Close();
//替换相应的内容到指定的位置
htmltext = htmltext.Replace("$htmldata[1]", name);
htmltext = htmltext.Replace("$htmldata[2]", name);
htmltext = htmltext.Replace("$htmldata[3]", ("点击数:" + hits + " 发布时间:" + addTime + " 发布者:" + 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);//因为带图片的html直接转为Word的话,图片会以引用的形式展示(也就是说不是内置到word文档里去的,一旦断网或将图片放在别的路径之后,打开word文档图片会显示不出来,所以通过折冲的办法先生成html,然后转换为mht,再转为word)
WordAction.SaveAsWord(File_NameM, File_Name2);//生成word
}
}
建立另外一个操作word 的静态类,代码如下(代码都是拷的O(∩_∩)O):
public class WordAction
{
public static void SaveAsWord(string fileName, string pFileName)//使用原生方法将mht转换为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视图修改为默认视图,不然打开word的时候会以web视图去展示,而不是默认视图。(唯独这句代码是自己加的 = =|||)
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进程
{
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.Moles != null)
if (p.Moles.Count > 0)
{
System.Diagnostics.ProcessMole pm = p.Moles[0];
myS += "\n Moles[0].FileName:" + pm.FileName;
myS += "\n Moles[0].MoleName:" + pm.MoleName;
myS += "\n Moles[0].FileVersionInfo:\n" + pm.FileVersionInfo.ToString();
if (pm.MoleName.ToLower() == "winword.exe")
p.Kill();
}
}
catch
{ }
finally
{
}
}
}
}
}
第三步:
随便建了个aspx页面,写上以下代码。
string title = "";
string postuser = "";
string content = "";
string addTime = "";
string hits = "";
DataTable dt = CatalogAccess.GetArticles();//从数据库取出自己需要的数据
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/");//替换图片文件的引用目录,这个动作是非必要的,因为我把图片都下载到本地了,所以替换一下里面引用的图片路径,只要根据模板生成的html能正常显示图片就可以了
Function.WriteHtml(title, content, addTime, hits, postuser);//生成word文档
}
}
好了,大功告成啦。当然生成的word文档跟网页存在一点差别,在接受的范围内。
热心网友 时间:2022-04-21 13:15
在Word里面打开mht文件,然后再另存为DOC或DOCX文档热心网友 时间:2022-04-21 14:50
这好办