KindEditor的内容以Word的形式导出
//导出按钮
protected void btn_Export_Click(object sender, EventArgs e)
{
Model.article art = new BLL.Common().GetModel(this.id);
WriteHtml(art.content);//art.content这个是显示的内容,我存在数据库中,是html 标签,从编辑器存到数据库中
}
//参数内容都是从数据库读出来的文章信息,其中content就是ewebeditor生成的html代码
public void WriteHtml(string content)
{
DateTime dt = DateTime.Now;//将string型的日期格式转为DateTime型的因为默认的日期格式不能作为文件名,所以将日期的“:”替换为“-”
string Temp_Name = @"E:\代码\起航动力\Code\DTcms.Web\FileTemplate\Temlpe\Articles.html";//HTML模板的路径
//生成html文件的路径
string File_Name = @"E:\代码\起航动力\Code\DTcms.Web\FileTemplate\html\【" + dt.ToShortDateString().Replace("/", "-") + "】" + ".html";
//生成mht文件的路径
string File_NameM = @"E:\代码\起航动力\Code\DTcms.Web\FileTemplate\html\【" + dt.ToShortDateString().Replace("/", "-") + "】" + ".mht";
//生成Word文档的路径
string File_Name2 = @"E:\代码\起航动力\Code\DTcms.Web\FileTemplate\html\【" + dt.ToShortDateString().Replace("/", "-") + "】" + ".doc";
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[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)
SaveAsWord(File_NameM, File_Name2);//生成word
}
public 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 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();
Document oWordDoc = new Document();
try
{
oWordApp.Visible = false;
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();
}
catch (System.Threading.ThreadAbortException ex)
{
object miss = System.Reflection.Missing.Value;
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
oWordDoc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
oWordApp.Application.Quit(ref miss, ref miss, ref miss);
killAllProcess();
}
//导出
string file = pFileName;
FileInfo fi = new FileInfo(file);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(file), System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file);
Response.Flush();
Response.End();
}
#region// 杀掉所有winword.exe进程
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.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
{ }
}
}
}
#endregion
如果是用vs2010编译时,可能会出现“错误19Encountered multiple versions of the assembly with GUID '0c2d4b67-e583-4be2-9fd6-653e9acef7a6'. Try pre-importi”。由于引用com组件导致的,具体怎么解决?可以去网上找。
KindEditor的内容以Word的形式导出的更多相关文章
- 批量导出access某表内容到word文档
一.需求: 需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下 二.界面,简单设计如下: 三.添加office相关引用 添加后可在解决方案资源管理器中看到: ...
- C#导出文本内容到word文档源码
将做工程过程中较好的代码片段珍藏起来,下面的代码内容是关于C#导出文本内容到word文档的代码,希望能对小伙伴们也有好处.<%@ Page Language="C#" Aut ...
- java导出excel 浏览器直接下载或者或以文件形式导出
/** * excel表格直接下载 */ public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse ht ...
- PHP中导出Excel,将数据以Excel形式导出
现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下: <?php date_default_timezone_set('PRC');//设置时区 /*设置h ...
- Freemaker基于word模板动态导出压缩文件汇总整理
Freemaker基于word模板动态导出压缩文件汇总整理 Freemaker基于word模板动态导出单个文件思路和代码详情见连接: https://www.cnblogs.com/lsy-blogs ...
- Freemaker基于word模板动态导出汇总整理
Freemaker基于word模板动态导出汇总整理 一.使用的jar包: 二.Word模板动态导出的基本思路: 1.首先通过自己在word中创建好需要导出的word文本+表格的模板,模板中需要填写内容 ...
- word excel 等导出相关操作
无插件,无com组件,利用EXCEL.WORD模板做数据导出(一) http://www.cnblogs.com/tzy080112/p/3413938.html 使用Aspose.Cells组件生成 ...
- WinForm小白的WPF初试一:从PropertyGrid控件,输出内容到Word(上)
学WinForm也就半年,然后转到WPF,还在熟悉中.最近拿到一个任务:从PropertyGrid控件,输出内容到Word.难点有: 一.PropertyGrid控件是WinForm控件,在WPF中并 ...
- SqlServer 如何以脚本形式导出数据
你是否遇到这样的情况,在公司导出一个数据库,回到家里导入自己的电脑里,然后发现数据库版本不匹配,这真是一个悲剧. 那么以下这个方法就可以避免这个悲剧,将数据以脚本的形式导出,这样灵活性更好. 1.选择 ...
随机推荐
- 2016年12月28日 星期三 --出埃及记 Exodus 21:23
2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...
- Winform MDI窗体容器,权限以及简单通讯
MDI窗体容器: 一般来说,窗体是顶级容器,不允许放在其他任何容器内,但是如果将某个窗体的IsMdiContainer属性设置为True,那此窗体就会成为窗体容器,可以在其中放入其他窗体 在内部的窗体 ...
- jquery.Huploadify 上传
html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- CSS样式表(二)
[layout] clear:该属性的值指出了不允许有浮动对象的边. 默认值:none none: 允许两边都可以有浮动对象 both: 不允许有浮动对象 left: 不允许左边有浮动对象 right ...
- 关于ios苹果系统的中的右键事件,查遍了全网都没有的小技巧。
前阵子公司要求写一套手机端,兼容各种平台和系统,当然,pc端也没有放过. 我用了bootstrap框架和jq.在安卓中的右键事件只需要取消浏览器默认事件,然后长按就可以触发pc端的右键事件,非常好,一 ...
- Boost学习笔记(六) progress_display注意事项
progress_display可以用作基本的进度显示,但它有个固有的缺陷:无法把进度显示输出与程序的输出分离. 这是因为progress_display和所有C++程序一样,都向标准输出(cout) ...
- 前端利器---Bootstrap
看着那么多大神在博客上都有自己的心得和分享,我虽然工作不久,但也想做一下自己的笔记起码对自己是一次积累和锻炼的过程.所以心血来潮今天就注册了博客. 我今天想说一下Bootstrap,学前台的大概对Bo ...
- Fluent API 配置
EF里实体关系配置的方法,有两种: Data Annotation方式配置 也可以 Fluent API 方式配置 Fluent API 配置的方法 EF里的实体关系 Fluent API 配置分为H ...
- jQuery Length属性
Length属性 属性用于返回当前jQuery对象的元素个数. 语法 jQueryObject.length 返回值 Number类型 返回该jQuery对象封装的DOM元素的个数. 实例说明 代码 ...
- 【IOS 开发】Object - C 入门 之 数据类型详解
1. 数据类型简介及输出() http://www.把下面的替换我.com/html/topnews201408/79/1279.htm csdn123