//导出按钮

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的形式导出的更多相关文章

  1. 批量导出access某表内容到word文档

    一.需求: 需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下 二.界面,简单设计如下: 三.添加office相关引用 添加后可在解决方案资源管理器中看到: ...

  2. C#导出文本内容到word文档源码

    将做工程过程中较好的代码片段珍藏起来,下面的代码内容是关于C#导出文本内容到word文档的代码,希望能对小伙伴们也有好处.<%@ Page Language="C#" Aut ...

  3. java导出excel 浏览器直接下载或者或以文件形式导出

    /** * excel表格直接下载 */ public static void exportExcelByDownload(HSSFWorkbook wb,HttpServletResponse ht ...

  4. PHP中导出Excel,将数据以Excel形式导出

    现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下: <?php date_default_timezone_set('PRC');//设置时区 /*设置h ...

  5. Freemaker基于word模板动态导出压缩文件汇总整理

    Freemaker基于word模板动态导出压缩文件汇总整理 Freemaker基于word模板动态导出单个文件思路和代码详情见连接: https://www.cnblogs.com/lsy-blogs ...

  6. Freemaker基于word模板动态导出汇总整理

    Freemaker基于word模板动态导出汇总整理 一.使用的jar包: 二.Word模板动态导出的基本思路: 1.首先通过自己在word中创建好需要导出的word文本+表格的模板,模板中需要填写内容 ...

  7. word excel 等导出相关操作

    无插件,无com组件,利用EXCEL.WORD模板做数据导出(一) http://www.cnblogs.com/tzy080112/p/3413938.html 使用Aspose.Cells组件生成 ...

  8. WinForm小白的WPF初试一:从PropertyGrid控件,输出内容到Word(上)

    学WinForm也就半年,然后转到WPF,还在熟悉中.最近拿到一个任务:从PropertyGrid控件,输出内容到Word.难点有: 一.PropertyGrid控件是WinForm控件,在WPF中并 ...

  9. SqlServer 如何以脚本形式导出数据

    你是否遇到这样的情况,在公司导出一个数据库,回到家里导入自己的电脑里,然后发现数据库版本不匹配,这真是一个悲剧. 那么以下这个方法就可以避免这个悲剧,将数据以脚本的形式导出,这样灵活性更好. 1.选择 ...

随机推荐

  1. js 操作数组封装

    function OperateArray(array) { this.array = array; } OperateArray.prototype.hasValue = function(val) ...

  2. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

  3. 【转载】Windows 7 不同安装模式简要区别(图解)

    ---------------------------------------------------------------------------------------------------- ...

  4. background-position (转)

    http://blog.csdn.net/JeamKing/article/details/5617088   注:这是别人博客链接地址  具体效果图片可以查看此链接 语法:background-po ...

  5. s5pv210启动debian出错提示bash: cannot set terminal process group (-1): Inappropriate ioctl for device

    1.启动参数如下: bootargs=root=/dev/nfs nfsroot=192.168.1.8:/opt/wheezy_fs ip=192.168.1.9:192.168.1.8:192.1 ...

  6. Maven 配置 Selenium + testNG + reportNG 运行环境

    .markdown-preview:not([data-use-github-style]) { padding: 2em; font-size: 1.2em; color: rgb(56, 58, ...

  7. Codeforces Round #389 (Div. 2, Rated, Based on Technocup 2017 - Elimination Round 3) D. Santa Claus and a Palindrome STL

    D. Santa Claus and a Palindrome time limit per test 2 seconds memory limit per test 256 megabytes in ...

  8. About next_permutation

    哈哈没错这个又是我们C++党的语言优势之一,用这个函数可以求当前排序的下一个排序,也就是说可以方便的求全排列,用这个函数需要用到algorithm这个头文件. 与这个函数相反的是prev_permut ...

  9. sql server 查询表结构

    --查询表结构start SELECT 序号 = a.colorder,字段名称 = a.name,字段描述 = f.value, 标识 then '√' else '' end, 主键 FROM s ...

  10. C# 实现 Excel文件的数据导入

    前台 <asp:FileUpload ID="fuFile" runat="server" /> 后台 public string GetExcel ...