Dev Express - Reports

  • DevExpress 与报告相关的组建
    • DevExpress.Data.v?
    • DevExpress.Docs.v?
    • DevExpress.Office.v?
    • DevExpress.RichEdit.v?
    • DevExpress.Utils.v?
    • DevExpress.Charts.v?.Core
    • DevExpress.XtraCharts.v?
    • DevExpress.XtraCharts.v?.UI

通过 Add Reference 添加以上相关组件到工程

  • 设置纸张类型和边距
public static void UpdateMargins(RichEditDocumentServer server, int marginLeft, int marginTop, int marginRight, int marginBottom)
{
    foreach (var section in server.Document.Sections) {
        section.Page.PaperKind = System.Drawing.Printing.PaperKind.A4;
        section.Margins.Left = marginLeft;
        section.Margins.Top = marginTop;
        section.Margins.Right = marginRight;
        section.Margins.Bottom = marginBottom;
    }
}
  • 插入页码

方法1:按照 PAGE/TOTAL 的方式显示

public static void UpdatePageNumbers(RichEditDocumentServer server, bool footer = true, ParagraphAlignment alignment = ParagraphAlignment.Center)
{
    var subdoc = footer ? server.Document.Sections[0].BeginUpdateFooter() : server.Document.Sections[0].BeginUpdateHeader();
    var pp = subdoc.BeginUpdateParagraphs(subdoc.Range);
    pp.Alignment = alignment;
    var field1 = subdoc.Fields.Create(subdoc.Range.End, "PAGE");
    subdoc.AppendText(" / ");
    var field2 = subdoc.Fields.Create(subdoc.Range.End, "NUMPAGES");
    subdoc.EndUpdate();
}

方法2:更通用,适用于已经布局好的HTML页面

public static void UpdatePageNumbers(RichEditDocumentServer server)
{
    var subdoc = server.Document.Sections[0].BeginUpdateFooter();
    foreach (var range in subdoc.FindAll("$(PAGE)", SearchOptions.CaseSensitive, subdoc.Range)) {
        var field = subdoc.Fields.Create(range.End, "PAGE");
    }
 
    foreach (var range in subdoc.FindAll("$(NUMPAGES)", SearchOptions.CaseSensitive, subdoc.Range)) {
        var field2 = subdoc.Fields.Create(range.End, "NUMPAGES");
    }
    subdoc.ReplaceAll("$(PAGE)", string.Empty, SearchOptions.CaseSensitive);
    subdoc.ReplaceAll("$(NUMPAGES)", string.Empty, SearchOptions.CaseSensitive);
    subdoc.EndUpdate();
}
  • 设置纸张平铺
try {
    RichEditDocumentServer server = new RichEditDocumentServer();
    server.CreateNewDocument();
    server.Document.HtmlText = htmlText;
    server.Document.Sections[0].Page.Landscape = true;
    server.SaveDocument(docFileName, DocumentFormat.Doc);
}
catch (System.Exception ex) {
    Console.WriteLine(ex.Message);
}
  • 设置页头和页尾
public static void UpdateHeader(RichEditDocumentServer server, string htmlText)
{
    if (!string.IsNullOrEmpty(htmlText)) {
        var hdoc = server.Document.Sections[0].BeginUpdateHeader();
        hdoc.AppendHtmlText(htmlText);
        hdoc.EndUpdate();
    }
}

public static void UpdateFooter(RichEditDocumentServer server, string htmlText)
{
    if (!string.IsNullOrEmpty(htmlText)) {
        var fdoc = server.Document.Sections[0].BeginUpdateFooter();
        var range = fdoc.AppendHtmlText(htmlText);
        fdoc.EndUpdate();
    }
}
  • RichEdit 的特殊字符
public class ControlChars
{
    public const string CrLf = "\r\n";
    public const string NewLine = "\r\n";
    public const char Cr = (char)13;
    public const char Lf = (char)10;
    public const char Back = (char)8;
    public const char FormFeed = (char)12;
    public const char Tab = (char)9;
    public const char VerticalTab = (char)11;
    public const char NullChar = (char)0;
    public const char Quote = (char)34;
}
  • 生成报告
public static void GenerateDoc(string htmlText, string htmlHeader, string htmlFooter, string docFileName)
{
    RichEditDocumentServer server = new RichEditDocumentServer();
    server.CreateNewDocument();
    server.Document.HtmlText = htmlText;
    server.Document.ReplaceAll("$(PAGE_BREAK)", new string(ControlChars.FormFeed, 1), SearchOptions.CaseSensitive);
    UpdateMargins(server, 100, 100, 100, 100);
    UpdateHeader(server, htmlHeader);
    UpdateFooter(server, htmlFooter);
    UpdatePageNumbers(server);
    server.SaveDocument(docFileName, DocumentFormat.Doc);
}
  • 生成二维码

添加引用:DevExpress.Docs.v?.dll 和 DevExpress.Data.v?.dll.

public static Bitmap GenerateQRCodeImage(string codeText)
{
    BarCode barCode = new BarCode();
    barCode.Symbology = Symbology.QRCode;
    barCode.CodeText = codeText;
    barCode.BackColor = Color.White;
    barCode.ForeColor = Color.Blue;
    barCode.RotationAngle = 0;
    barCode.CodeBinaryData = System.Text.Encoding.Default.GetBytes(barCode.CodeText);
    barCode.Options.QRCode.CompactionMode = QRCodeCompactionMode.Byte;
    barCode.Options.QRCode.ErrorLevel = QRCodeErrorLevel.Q;
    barCode.Options.QRCode.ShowCodeText = false;
    barCode.DpiX = 72;
    barCode.DpiY = 72;
    return barCode.BarCodeImage;
}
  • 插入图表
class Program
{
    class ExScore
    {
        public string Title { get; set; }
        public int Score { get; set; }
        public ExScore(string title, int score)
        {
            Title = title;
            Score = score;
        }
    }
 
    static void GenerateChart(string path, int width, int height, DevExpress.XtraCharts.ViewType viewType, IEnumerable<ExScore> scores)
    {
        using (var chart = new DevExpress.XtraCharts.ChartControl() { Width = width, Height = height }) {
            var series = new Series("高考成绩", viewType);
            series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;
            (series.Label as SideBySideBarSeriesLabel).Position = BarSeriesLabelPosition.Top;
            foreach (var q in scores) {
                series.Points.AddPoint(q.Title, q.Score);
            }
            chart.Series.Add(series);
            chart.ExportToImage(path, System.Drawing.Imaging.ImageFormat.Png);
            Process.Start(path);
        }
    }
 
    static void Main(string[] args)
    {
        GenerateChart(@"C:\TEMP\chart.png", 600, 400, DevExpress.XtraCharts.ViewType.Bar, new ExScore[] {
            new ExScore("语文", 91),
            new ExScore("数学", 112),
            new ExScore("英语", 117),
            new ExScore("物理", 128),
            new ExScore("化学", 113),
        });
    }
}

效果图

chart-with-dev!

Field Name      Functionality
----------      -------------
AUTHOR          Inserts the name of a person or organization who created the document.
COMMENTS        Inserts the document notes.
CREATEDATE      Inserts the current date and time. After a mail merge the field is replaced with the date and time of the mail merge operation.
DATE            Inserts the current date and time.
DOCPROPERTY     Inserts the value of the document property specified by the field parameter.
DOCVARIABLE     Enables you to programmatically insert complex content when this field is updated.
HYPERLINK       Enables you to navigate to another location or to a bookmark.
IF              Compares two values and inserts the text according to the results of the comparison.
INCLUDEPICTURE  Inserts the specified image.
KEYWORDS        Inserts the document keywords or tags.
LASTSAVEDBY     Inserts the name of the last person who modified and saved the document.
MERGEFIELD      Retrieves a value from the bound data source.
NUMPAGES        Inserts the total number of pages.
PAGE            Inserts the number of the page containing the field.
PRINTDATE       Inserts the date and time that a document was last printed.
REVNUM          Inserts the number of document revisions.
SAVEDATE        Inserts a date and time a document was last saved.
SEQ             Provides sequential numbering in the document.
SUBJECT         Inserts the document topic of content.
SYMBOL          Inserts a symbol.
TC              Defines entries for the table of contents.
TIME            Inserts the current time.
TITLE           Inserts the document title.
Tag Attribute Interpretation
A Underlined blue text.
B Bold text.
BASE href Specifies the absolute URI that serves as the base URI to resolve a relative URI.
BASEFONT size Specifies font size.
color Specifies font color.
face Specifies font name.
BIG Suggests bigger font size.
BLOCKQUOTE Indented section of text (left and right indents are about 40 pixels wide).
BR Line feed.
CENTER Center aligns the content within container.
CODE Specifies the font “Courier New”, font size 10pt.
FONT size Specifies font size.
color Specifies font color.
face Specifies font name.
H1-H6 Specifies bold text with the font size equal to that of the heading style.
align Specifies the alignment of the heading.
HEAD
HTML
I Italic text.
IMG src Location of inline image (URI).
height Image height.
width Image width.
LI Defines the list item.
type Specifies the type of the list item marker.
value Specifies the start value for list numbering. The following list element in the same list will take its default sequence number, based on this attribute.
LINK Defines the external document relationship.
href Specifies the location of the external file (URI).
type Only Text/CSS type links are currently processed
media Only the <screen> attribute value, defining media for non-paged computer screens, is currently processed.
META Only the <meta charset> attribute which sets the character encoding is currently processed.
OL Defines an ordered list.
type Specifies the numbering style.
value Specifies the starting number for the list.
P Defines a paragraph.
align Specifies the horizontal alignment for the content of the paragraph.
SCRIPT Text inside this tag is ignored.
SMALL Suggests smaller font size.
SPAN Combined with the style sheet, specifies formatting of the text within the tag.
STRIKE Strike-through text.
STRONG Bold text.
STYLE Specifies document styles.
SUB Subscript.
SUP Superscript.
TABLE Defines a table.
TABLE align Specifies table alignment.
TABLE bgcolor Specifies shading.
TABLE border Specifies border width.
TABLE bordercolor Specifies border color.
TABLE cellpadding Specifies inner cell margins.
TABLE cellspacing Specifies cell spacing.
TABLE cols Specifies column count.
TABLE width Specifies table width.
TD Defines a table cell.
TD align Specifies horizontal alignment for cell contents.
TD bgcolor Specifies shading.
TD bordercolor Specifies border color.
TD colspan Specifies the number of columns a cell should span.
TD height Specifies the height of a cell.
TD nowrap Sets the TableCellPropertiesBase.NoWrap property. The attribute is interpreted differently in HTML browser and in RichEditControl.
TD rowspan Specifies the number of rows a cell should span
TD valign Specifies vertical alignment for cell contents.
TD width Specifies the width of a cell
TH Defines a table cell with bold text and center aligned contents
TH (any allowed) The same as for the <TD> tag
TR Defines a row in a table
TR align Specifies horizontal alignment for cells in a row.
TR bgcolor Specifies shading.
TR bordercolor Specifies border color.
TR height Specifies the height of a row.
TR valign Specifies vertical alignment for cells in a row.
TITLE Text inside this tag is ignored.
U Underlined text.
UL Defines bulleted list.
type Specifies the bullet style.

不支持的 HTML 标签

Tag Attribute
HR
DIV border
DIV page-break-before, page-break-after, page-break-inside
DIV background-color
TD nowrap
!important
DIV text-align: justify
DIV style=”padding”
TABLE style=”padding”
Tags of CSS3 Shapes
LI style=”list-style-image”
LI style=”list-style-type”
TAB