星空网 > 软件开发 > ASP.net

DevExpress ASPxHtmlEditor控件格式化并导出Word

在前台页面中先插入一个ASPxHtmlEditor控件,名为ASPxHtmlEditor1。 我用的Dev版本为14.1

格式化文本 

在后台插入如下代码

 1     const string css = "style='text-align:justify;"         //两端对齐
 2         + "text-justify:inter-ideograph;"
 3         + "text-indent: 2em;"                               //首行缩进2字符
 4         + "line-height:1.25;"                               //1.25倍行距
 5         + "margin-top:0;margin-bottom:0;"                   //段前段后0行
 6         + "font-size: 12pt;"                                //字体:小四
 7         + "font-family:Times New Roman,宋体;'";              //中文字体:宋体,西文字体:Times New Roman
 8 
 9 
10     ASPxHtmlEditor1.Html = "<p " + css + ">";               //段落用标签p标记
11     for (int i = 0; i < 30;i++)
12         ASPxHtmlEditor1.Html += "测试文本123abCD";           //这是内容
13     ASPxHtmlEditor1.Html += "</p>

 上述代码实现了对文字的格式化,可以基本满足生成一般报告的格式需要。

Word导出及页面设置 

 对于ASPxHtmlEditor控件,无法直接设置导出Word或其他格式文件的页面(页边距、纸张大小等),需借助RichEditDocumentServer来转存实现。

添加如下引用:

1 using DevExpress.XtraRichEdit;
2 using DevExpress.XtraRichEdit.API.Native;

插入下面两个函数

 1     private void SetPrintOptions(IRichEditDocumentServer richedit)              //设置格式

 2     {
 3         foreach (Section _section in richedit.Document.Sections)
 4         {
 5             _section.Page.PaperKind = System.Drawing.Printing.PaperKind.A4;     //A4纸
 6             _section.Page.Landscape = false;                                    //竖版
 7             _section.Margins.Left = 295f;                                       //左侧页边距 2.5cm
 8             _section.Margins.Right = 295f;
 9             _section.Margins.Top = 295f;
10             _section.Margins.Bottom = 295f;
11 
12         }
13     }
14 
15     protected void PushToBrowser(string fileName)                               //导出文件
16     {
17 
18         FileStream fs = new FileStream(fileName, FileMode.Open);
19         byte[] bytes = new byte[(int)fs.Length];
20         fs.Read(bytes, 0, bytes.Length);
21         fs.Close();
22         if (File.Exists(fileName))
23             File.Delete(fileName);
24 
25         Response.ContentType = "application/octet-stream";
26         Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
27         Response.BinaryWrite(bytes);
28         Response.Flush();
29         Response.End();
30     }

 在执行导出的事件(如Button_Click事件)中添加如下代码

 1         string outputFileName = "newtext.docx";            //导出文件的名称

 2 
 3         FileStream fs = new FileStream("test.docx", FileMode.Create);
 4         ASPxHtmlEditor1.Export(DevExpress.Web.ASPxHtmlEditor.HtmlEditorExportFormat.Docx, fs);
 5         fs.Close();
 6         fs.Close();
 7 
 8         RichEditDocumentServer srv = new RichEditDocumentServer();
 9         srv.LoadDocument("test.docx", DocumentFormat.Open10         srv.BeginUpdate();
11         SetPrintOptions(srv);
12         srv.EndUpdate();
13         srv.SaveDocument(outputFileName, DocumentFormat.Open14 
15         if (File.Exists("test.docx"))
16             File.Delete("test.docx");
17 
18         PushToBrowser(outputFileName);

 

 输出的文本如下:

DevExpress ASPxHtmlEditor控件格式化并导出Word

 

 p.s.关于页边距设置成cm的换算

DevExpress ASPxHtmlEditor控件格式化并导出Word 

 

试了几组数据,做了个3次拟合,先凑合用吧。(x是期望设置的厘米数 如2.5cm y就是程序的中用到的参数 如295 )

 




原标题:DevExpress ASPxHtmlEditor控件格式化并导出Word

关键词:ASP

ASP
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

出海掘金,FP独立站新手卖家必码这2点!:https://www.kjdsnews.com/a/1339127.html
实力认可!跨税云荣获“2022年度最佳税务服务”奖项:https://www.kjdsnews.com/a/1339128.html
紧急!4月1日起,这项新政策将在墨西哥强制实行!:https://www.kjdsnews.com/a/1339129.html
夏季来袭,哪些品类能借助TikTok营销爆单?:https://www.kjdsnews.com/a/1340117.html
跨境电商综合试验区再度扩围,覆盖我国31个省区市:https://www.kjdsnews.com/a/1340118.html
7个TikTok变现方式!不允许你不知道:https://www.kjdsnews.com/a/1340119.html
价格翻了8倍,居然还有人买,秘诀在这里。:https://www.kjdsnews.com/a/1836642.html
欧洲B2B电商市场有待挖掘!销售总额将达1.7万亿欧元:https://www.kjdsnews.com/a/1836643.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流