在前台页面中先插入一个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 

     ASPxHtmlEditor1.Html = "<p " + css + ">";               //段落用标签p标记

     for (int i = ; i < ;i++)

         ASPxHtmlEditor1.Html += "测试文本123abCD";           //这是内容

     ASPxHtmlEditor1.Html += "</p>

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

Word导出及页面设置

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

添加如下引用:

 using DevExpress.XtraRichEdit;

 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;              _section.Margins.Bottom = 295f;            }      }        protected void PushToBrowser(string fileName)                               //导出文件      {            FileStream fs = new FileStream(fileName, FileMode.Open);          byte[] bytes = new byte[(int)fs.Length];          fs.Read(bytes, , bytes.Length);          fs.Close();          if (File.Exists(fileName))              File.Delete(fileName);            Response.ContentType = "application/octet-stream";          Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));          Response.BinaryWrite(bytes);          Response.Flush();          Response.End();      }

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

 1         string outputFileName = "newtext.doc";            //导出文件的名称
 2   3         FileStream fs = new FileStream("test.rtf", FileMode.Create);  4         ASPxHtmlEditor1.Export(DevExpress.Web.ASPxHtmlEditor.HtmlEditorExportFormat.Rtf, fs);  //注意此处  先导出Rtf格式  5         fs.Close();  6         fs.Close();  7   8         RichEditDocumentServer srv = new RichEditDocumentServer();  9         srv.LoadDocument("test.rtf", DocumentFormat.Rtf);          srv.BeginUpdate();          SetPrintOptions(srv);          srv.EndUpdate();          srv.SaveDocument(outputFileName, DocumentFormat.Doc);   //再导出Doc格式  如果导出OpenXml(.Docx)格式,则中文字体丢失
           if (File.Exists("test.rtf"))              File.Delete("test.rtf");            PushToBrowser(outputFileName);

输出的文本如下:

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

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

DevExpress ASPxHtmlEditor控件格式化并导出Word (修复中文字体导出丢失)的更多相关文章

  1. Devexpress TreeList控件绑定显示父子节点对像

    今天一位同事咨询Devexpress TreeList控件绑定自动显示父子节点对像,但结果是不会显示带父子节点关系,而是将所有的节点作为父节点显示出来了,对像类的代码如下 public class I ...

  2. DevExpress 用户控件 分页(上)

    说明:使用用户控件分页,完成后,使用时非常简单,数据绑定,调用自己写的一个事件就OK了 前期准备工作: (1)添加一个用户控件 命名PageCtrl (2)打开代码:   [csharp] view ...

  3. DevExpress 用户控件 分页(中)

    说明: 1)具体调用请关注 看DevExpress 用户控件 分页(下) datanavi_ButtonClick 是DataNavigator的ButtonClikc事件 视图设计器: 分页用户控件 ...

  4. DevExpress.LookUpEdit控件实现自动搜索定位功能 兼使用方法(looUpEdit可编辑)

    DevExpress.LookUpEdit 使用方法 设置可手动输入 this.LookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditor ...

  5. devexpress表格控件gridcontrol图片列,按钮列,时间列等特殊列的实现

    1.项目中经常会在表格中插入按钮列,图片列,表格列一些非文本的特殊列.如何在devexpress表格控件gridcontrol中实现呢?以下列举一个实现添加图片列,按钮列,时间列,按钮列,开关列的示例 ...

  6. 详解DevExpress.LookUpEdit控件实现自动搜索定位功能(转)

    转载自csdn博客 爱拼才会赢 的博客 地址是详解DevExpress.LookUpEdit控件实现自动搜索定位功能(转)

  7. DevExpress WPF控件记录

    以下是博主用到DevExpress WPF控件时的一些记录笔记: 1.Canvas控件:Canvas控件的背景色一定要设置(background="Transparent"),不然 ...

  8. Qt编写控件属性设计器5-属性中文

    一.前言 在上一篇文章中就提到过,使用qtpropertybrowser来加载属性,对应加载到的属性是英文的,也就是控件类中Q_PROPERTY描述的变量名称,如何变成中文或者其他语言显示呢?这个就需 ...

  9. DevExpress某些控件继承后的可编辑性

    今天在使用DevExpress的BarManager菜单控件时,发现在进行继承时无法在继承的子类窗体中对其进行编辑与修改,另外像GridView也有类似的情形,后来查阅资料后,现在可通过DevExpr ...

随机推荐

  1. 《第一行代码》学习笔记39-服务Service(6)

    1.Android中定时任务一般有两种实现方式:(1)使用Java API里提供的Timer类:(2)使用Android的Alarm机制. 但是,Timer类有个明显的短板,不太适用于那些需要长期在后 ...

  2. iOS 数据持久化

    一.plist文件存储 获得文件 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDom ...

  3. java下io文件切割合并功能

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  4. angularjs使用ng-messages-include实例

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  5. memcache运维整理

    memcache运维总结 第一部分:memcache安装 1.安装libevent 2.安装memcache 3.安装php的memcache扩展 4.测试 第二部分:memcache客户端操作 1. ...

  6. Python入门-----介绍

    摘要:Python语言的特点 ----->优雅.明确.简单 一.Python适合的领域 web网站和各种网络服务 系统工具和脚本 作为“胶水”语言,把其他语言开发的模块包装起来方便使用 二.Py ...

  7. HTML&CSS基础学习笔记1.9-添加图片

    <img>标签是用来添加图片的~ <img>标签的使用方法:<img src="图片的地址"> 先来看段实例代码: <!DOCTYPE h ...

  8. SuperMap

    SuperMap iClient for JavaScript 新手入门 地理信息系统(英语:Geographic Information System,缩写:GIS)是一门综合性学科,结合地理学与地 ...

  9. build.prop修改详细说明

    用RE进入/system/挂载读写,找到build.prop复制到/sdcarrd进行修改比较保险.也可以挂载读写后,直接选择用文本编辑器打开,进行编辑.乱改有风险,修改需谨慎.1.# begin b ...

  10. Android从相册读取图片

    Uri originalUri = data.getData();        //获得图片的uri  bm = MediaStore.Images.Media.getBitmap(resolver ...