首页 > 代码库 > KindEditor的内容以Word的形式导出

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 = http://www.mamicode.com/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组件导致的,具体怎么解决?可以去网上找。