CKEditor输出成Word文件(包含图片上传)

转载自http://www.it165.net/pro/html/201306/6055.html

先下载CKEditor

我们修改 /Scripts/ckeditor/config.js

让图片上传能够正常..

 

view source print ?
01. /*
02. Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
03. For licensing, see LICENSE.html or http://ckeditor.com/license
04. */
05.  
06. CKEDITOR.editorConfig = function( config )
07. {
08. config.filebrowserImageUploadUrl = "/fileupload.aspx";
09. };

其中我们让档案上传的处理路径指到跟执行目录下 fileupload.aspx

之后我们来撰写关于 fileupload.aspx 的部分..

fileupload.apsx 关于 .aspx 得部分只需要留下第一行其它都要删除

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="fileupload.aspx.cs" Inherits="SimpleConvetCKEditorHTMLToOpenXML.fileupload" %>
fileuploas.aspx.cs 的部分:

 

view source print ?
01. using System;
02. using System.IO;
03. using System.Web;
04.  
05. namespace SimpleConvetCKEditorHTMLToOpenXML
06. {
07. public partial class fileupload : System.Web.UI.Page
08. {
09. protected void Page_Load(object sender, EventArgs e)
10. {
11.  
12. HttpPostedFile uploads = Context.Request.Files["upload"];
13. string ckediotfuncnum = Context.Request["CKEditorFuncNum"];
14. var t = Guid.NewGuid().ToString("N");
15.  
16. //如果暂存数据夹不在就建立
17. if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "uploaded\\"))
18. {
19. Directory.GetCreationTime(AppDomain.CurrentDomain.BaseDirectory + "uploaded\\");
20. }
21.  
22. //判断图片是否为.png or .jpg
23. //当然这方法并非很好
24. //建议作法依然要打打开图片做检查会比较安全
25. if (Path.GetExtension(uploads.FileName).ToLower() == ".jpg" || Path.GetExtension(uploads.FileName).ToLower() == ".png")
26. {
27. string url = "/uploaded/" + t + Path.GetExtension(uploads.FileName);
28. File.WriteAllBytes(AppDomain.CurrentDomain.BaseDirectory + "uploaded\\" + t + Path.GetExtension(uploads.FileName), StreamToBytes(uploads.InputStream));
29. Response.Write("");
30.  
31. }
32. else
33. {
34. Response.Write("");
35. Response.Write("");
36. }
37. }
38.  
39. ///
40. /// 将Stream 转成  Byte[]
41. ///
42. ///
43. ///
44. private byte[] StreamToBytes(Stream stream)
45. {
46. stream.Position = 0;
47. var buffer = new byte[stream.Length];
48. for (int totalBytesCopied = 0; totalBytesCopied < stream.Length; )
49. totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
50. return buffer;
51. }
52.  
53.  
54. }
55. }

再来我们看一下关于 转成Word 檔的部分..

首先,你得先去下载必须的library .

Open XML SDK 2.0 for Microsoft Office

 

CKEditor输出成Word文件(包含图片上传)_第1张图片
 

安装后我计算机是Windows 8 位置会大概在..

 

CKEditor输出成Word文件(包含图片上传)_第2张图片
 

再来去下载一个方便的工具HtmlToOpenXML

 

CKEditor输出成Word文件(包含图片上传)_第3张图片
 

接下来,就可以开始写程序…

 

view source print ?
01. using System;
02. using System.IO;
03. using DocumentFormat.OpenXml;
04. using DocumentFormat.OpenXml.Packaging;
05. using DocumentFormat.OpenXml.Wordprocessing;
06. using NotesFor.HtmlToOpenXml;
07.  
08. namespace SimpleConvetCKEditorHTMLToOpenXML
09. {
10. public partial class demo : System.Web.UI.Page
11. {
12. protected void Page_Load(object sender, EventArgs e)
13. {
14.  
15. }
16.  
17. protected void btnConvertToWord_Click(object sender, EventArgs e)
18. {
19. var fileName = Guid.NewGuid().ToString("N") + ".docx";
20. var filePathName = AppDomain.CurrentDomain.BaseDirectory + "words\\" +fileName;
21. var html = CKEditorControl1.Text;
22.  
23. using (MemoryStream generatedDocument = new MemoryStream())
24. {
25. using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
26. {
27. MainDocumentPart mainPart = package.MainDocumentPart;
28. if (mainPart == null)
29. {
30. mainPart = package.AddMainDocumentPart();
31. new Document(new Body()).Save(mainPart);
32. }
33.  
34. HtmlConverter converter = new HtmlConverter(mainPart);
35. Body body = mainPart.Document.Body;
36. //如果有插入图片,这一行很重要
37. converter.BaseImageUrl = new Uri("http://localhost:16777");
38. var paragraphs = converter.Parse(html);
39. for (int i = 0; i < paragraphs.Count; i++)
40. {
41. body.Append(paragraphs[i]);
42. }
43.  
44. mainPart.Document.Save();
45. }
46.  
47. File.WriteAllBytes(filePathName, generatedDocument.ToArray());
48. ltlMessage.Text = "已输出至" "words\\" + fileName;
49. }
50.  
51. }
52. }
53. }

如果,有遇到

"The type 'System.IO.Packaging.Package' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 


 

需要引入一个library . WindowsBase

 

CKEditor输出成Word文件(包含图片上传)_第4张图片
 

结果:

 

CKEditor输出成Word文件(包含图片上传)_第5张图片
 
CKEditor输出成Word文件(包含图片上传)_第6张图片

你可能感兴趣的:(juery插件)