• 动态RDLC报表(三)


    动态RDLC报表类DynamicReport:基础设置

           了解了RDLC报表的结构后,我们就来把传递进来的数据转变成符合RDLC报表结构的xml格式文档,用一个类的形式来做,方便以后调用。
           这个报表是按我项目的需要来做的,基本要求:
          1、可以设置页面大小、边距、字体、数据表样式;
          2、可以做多维表头和统计;
          3、可以添加多个数据表和标题;
          4、可以自动签名、盖章。

       基础的代码如下:
     

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Web;
    5. using System.Xml;
    6. using System.Data;
    7. using Microsoft.Reporting.WinForms;
    8. using System.Text;
    9. using System.Collections;
    10. using System.IO;
    11. using System.Reflection;
    12. using System.Xml.Serialization;
    13. using System.Drawing;
    14. using System.Drawing.Drawing2D;
    15. using System.Drawing.Printing;
    16. using System.Drawing.Imaging;
    17. namespace RDLC
    18. {
    19. public class DynamicReport : IDynamicReport
    20. {
    21. //需要一个ReportViewer来接收送递进来的ReportViewer加载RDLC
    22. private ReportViewer _report;
    23. private string languageCode = "zh_CHS";
    24. private string totalString = "合计:";
    25. private string notesString = "备注:";
    26. //设置字体和大小
    27. private string fontString = "微软雅黑";
    28. private int fontSize = 9;
    29. //设置页面大小
    30. private float pageWidth = 21.0F;
    31. private float pageHeight = 29.7F;
    32. //设置上边距和左边距,右边和下边就不用设置了,一般是对称的
    33. private float topMargin = 0.8F;
    34. private float leftMargin = 1.5F;
    35. #region 参数设置
    36. public string LanguageCode
    37. {
    38. get { return languageCode; }
    39. set
    40. {
    41. languageCode = value;
    42. if (languageCode == "zh_CHT")
    43. {
    44. totalString = "合計:";
    45. notesString = "備註:";
    46. }
    47. else if (languageCode == "en")
    48. {
    49. totalString = "Total:";
    50. notesString = "Remark:";
    51. }
    52. else
    53. {
    54. totalString = "合计:";
    55. notesString = "备注:";
    56. }
    57. }
    58. }
    59. public string FontString
    60. {
    61. get { return fontString; }
    62. set { fontString = value; }
    63. }
    64. public int FontSize
    65. {
    66. get { return fontSize; }
    67. set { fontSize = value; }
    68. }
    69. public float PageHeight
    70. {
    71. get { return pageHeight; }
    72. set { pageHeight = value; }
    73. }
    74. public float PageWidth
    75. {
    76. get { return pageWidth; }
    77. set { pageWidth = value; }
    78. }
    79. public float LeftMargin
    80. {
    81. get { return leftMargin; }
    82. set { leftMargin = value; }
    83. }
    84. public float TopMargin
    85. {
    86. get { return topMargin; }
    87. set { topMargin = value; headerHeight = value; }
    88. }
    89. #endregion
    90. public DynamicReport()
    91. {
    92. }
    93. }
    94. }

    这里继承了一个IDynamicReport接口

    1. public interface IDynamicReport
    2. {
    3. void SetReport(ReportViewer reportViewer, DisplayMode displayMode = DisplayMode.PrintLayout, ZoomMode zoomMode = ZoomMode.PageWidth);
    4. void AddData(DataTable dataTable, string totalColumn);
    5. void ShowReport();
    6. void LoadReport(string reportPath);
    7. void SetColoumStyle(List coloumStyle);
    8. }

    需要为表格的样式增加一个辅助类和枚举,这样主涌方便地设置表格的样式。

    1. public class ReportColoumStyle
    2. {
    3. public string ColoumName { get; set; }
    4. public float ColoumWidth { get; set; }
    5. public TextAlign TextAlign { get; set; }
    6. public ConsoleColor ConsoleColor { get; set; }
    7. public Boolean IsShortDate { get; set; }
    8. public ReportColoumStyle()
    9. {
    10. ColoumWidth = DynamicReport.ColoumWidth;
    11. }
    12. }
    13. public enum TextAlign
    14. {
    15. Left,
    16. Center,
    17. Right
    18. }

          接下来就是主体部分了,先来定义一个空白的包含RDLC所需节点的xml文档来做模板,模板中用@XXX来表示相应的节点内容,比如@DataSets、@Tablix分别代表要设置的数据源中的数据表和中要显示的数据表格,后面会用传递进来的数据替换。
     

    1. protected string _docTemplate =
    2. "" +
    3. "<Report xmlns:rd=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\" xmlns=\"http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition\">" +
    4. "<DataSources>" +
    5. " <DataSource Name=\"DummyDataSource\">" +
    6. " <ConnectionProperties>" +
    7. " <DataProvider>SQLDataProvider>" +
    8. " <ConnectString />" +
    9. " ConnectionProperties>" +
    10. " <rd:DataSourceID>3eecdab9-6b4b-4836-ad62-95e4aee65ea8rd:DataSourceID>" +
    11. " DataSource>" +
    12. "DataSources>" +
    13. "<DataSets>@DataSetsDataSets>" +
    14. "<Body>" +
    15. "<ReportItems>@Tablix" +
    16. "ReportItems>" +
    17. "<Style />" +
    18. "<Height>1cmHeight>" +
    19. "Body>" +
    20. "<Width>3cmWidth>" +
    21. "<Page>" +
    22. " <PageHeader>" +
    23. " <Height>@HeaderHeightcmHeight>" +
    24. " <PrintOnFirstPage>truePrintOnFirstPage>" +
    25. " <PrintOnLastPage>truePrintOnLastPage>" +
    26. " <ReportItems>@PageHeaderReportItems>" +
    27. " <Style>" +
    28. " <Border>" +
    29. " <Style>NoneStyle>" +
    30. " Border>" +
    31. " Style>" +
    32. " PageHeader>" +
    33. " <PageFooter>" +
    34. " <Height>@FooterHeightcmHeight>" +
    35. " <PrintOnFirstPage>truePrintOnFirstPage>" +
    36. " <PrintOnLastPage>truePrintOnLastPage>" +
    37. " <ReportItems>@PageFooterReportItems>" +
    38. " <Style>" +
    39. " <Border>" +
    40. " <Style>NoneStyle>" +
    41. " Border>" +
    42. " Style>" +
    43. " PageFooter>" +
    44. " <PageWidth>21.0cmPageWidth>" +
    45. " <PageHeight>29.7cmPageHeight>" +
    46. " <LeftMargin>0.0cmLeftMargin>" +
    47. " <RightMargin>0.0cmRightMargin>" +
    48. " <TopMargin>0.0cmTopMargin>" +
    49. " <BottomMargin>0.0cmBottomMargin>" +
    50. " <ColumnSpacing>0.13cmColumnSpacing>" +
    51. " <Style />" +
    52. "Page>" +
    53. "<EmbeddedImages>@LogoImageDataEmbeddedImages>" +
    54. "<rd:ReportID>809f16cf-ea78-4469-bf43-965c4afe69d0rd:ReportID>" +
    55. "<rd:ReportUnitType>Cmrd:ReportUnitType>" +
    56. "Report>";

    同样地分别给页眉里标签中用到的Textbox、标题中用到的Textbox和签名中的图片定义一个格式
     

    1. //标签中用到的Textbox
    2. protected string LabelPattern =
    3. " <Textbox Name=\"Name@TextboxName\"> "
    4. + @"<CanGrow>trueCanGrow>
    5. <KeepTogether>trueKeepTogether>
    6. <Paragraphs>
    7. <Paragraph>
    8. <TextRuns>
    9. <TextRun>
    10. <Value>@NameTextValue>
    11. <Style><FontFamily>@FontStringFontFamily><FontSize>9ptFontSize>Style>
    12. TextRun>
    13. TextRuns>
    14. <Style><TextAlign>RightTextAlign>Style>
    15. Paragraph>
    16. Paragraphs>
    17. <rd:DefaultName>Name@TextboxNamerd:DefaultName>
    18. <Top>@TopPositioncmTop>
    19. <Left>@NamePositioncmLeft>
    20. <Height>0.5cmHeight>
    21. <Width>@NameWidthcmWidth>
    22. <ZIndex>1ZIndex>
    23. <Style>
    24. <Border>
    25. <Style>NoneStyle>
    26. Border>
    27. <VerticalAlign>MiddleVerticalAlign>
    28. <PaddingLeft>0ptPaddingLeft>
    29. <PaddingRight>2ptPaddingRight>
    30. <PaddingTop>0ptPaddingTop>
    31. <PaddingBottom>0ptPaddingBottom>
    32. Style>
    33. Textbox>" +
    34. " <Textbox Name=\"Split@TextboxName\"> "
    35. + @"<CanGrow>trueCanGrow>
    36. <KeepTogether>trueKeepTogether>
    37. <Paragraphs>
    38. <Paragraph>
    39. <TextRuns>
    40. <TextRun>
    41. <Value>Value>
    42. <Style><FontFamily>@FontStringFontFamily><FontSize>9ptFontSize>Style>
    43. TextRun>
    44. TextRuns>
    45. <Style><TextAlign>LeftTextAlign>Style>
    46. Paragraph>
    47. Paragraphs>
    48. <rd:DefaultName>Split@TextboxNamerd:DefaultName>
    49. <Top>@TopPositioncmTop>
    50. <Left>@SplitPositioncmLeft>
    51. <Height>0.5cmHeight>
    52. <Width>0.3cmWidth>
    53. <ZIndex>1ZIndex>
    54. <Style>
    55. <Border>
    56. <Style>NoneStyle>
    57. Border>
    58. <VerticalAlign>MiddleVerticalAlign>
    59. <PaddingLeft>0ptPaddingLeft>
    60. <PaddingRight>0ptPaddingRight>
    61. <PaddingTop>0ptPaddingTop>
    62. <PaddingBottom>0ptPaddingBottom>
    63. Style>
    64. Textbox>" +
    65. " <Textbox Name=\"Value@TextboxName\"> "
    66. + @"<CanGrow>trueCanGrow>
    67. <KeepTogether>trueKeepTogether>
    68. <Paragraphs>
    69. <Paragraph>
    70. <TextRuns>
    71. <TextRun>
    72. <Value>@ValueTextValue>
    73. <Style><FontFamily>@FontStringFontFamily><FontSize>9ptFontSize><Color>@ColorColor>Style>
    74. TextRun>
    75. TextRuns>
    76. <Style><TextAlign>LeftTextAlign>Style>
    77. Paragraph>
    78. Paragraphs>
    79. <rd:DefaultName>Value@TextboxNamerd:DefaultName>
    80. <Top>@TopPositioncmTop>
    81. <Left>@ValuePositioncmLeft>
    82. <Height>0.5cmHeight>
    83. <Width>@ValueWidthcmWidth>
    84. <ZIndex>1ZIndex>
    85. <Style>
    86. <Border>
    87. <Style>NoneStyle>
    88. Border>
    89. <VerticalAlign>MiddleVerticalAlign>
    90. <PaddingLeft>0ptPaddingLeft>
    91. <PaddingRight>0ptPaddingRight>
    92. <PaddingTop>0ptPaddingTop>
    93. <PaddingBottom>0ptPaddingBottom>
    94. Style>
    95. Textbox>";
    96. //标题中用到的Textbox
    97. protected string TitlePattern =
    98. " <Textbox Name=\"Title@TextboxName\"> "
    99. + @"<CanGrow>trueCanGrow>
    100. <KeepTogether>trueKeepTogether>
    101. <Paragraphs>
    102. <Paragraph>
    103. <TextRuns>
    104. <TextRun>
    105. <Value>@TitleValue>
    106. <Style><FontFamily>@FontStringFontFamily><FontSize>@FontSizeptFontSize><FontWeight>@FontWeightFontWeight><Color>@ColorColor>Style>
    107. TextRun>
    108. TextRuns>
    109. <Style><TextAlign>@TextAlignTextAlign>Style>
    110. Paragraph>
    111. Paragraphs>
    112. <rd:DefaultName>Title@TextboxNamerd:DefaultName>
    113. <Top>@TopPositioncmTop>
    114. <Left>@LeftPositioncmLeft>
    115. <Height>@HeightcmHeight>
    116. <Width>@WidthcmWidth>
    117. <ZIndex>1ZIndex>
    118. <Style>
    119. <Border>
    120. <Style>NoneStyle>
    121. Border>
    122. <VerticalAlign>BottomVerticalAlign>
    123. <PaddingLeft>0ptPaddingLeft>
    124. <PaddingRight>0ptPaddingRight>
    125. <PaddingTop>0ptPaddingTop>
    126. <PaddingBottom>0ptPaddingBottom>
    127. Style>
    128. Textbox>";
    129. //签名中的图片
    130. string signaturePattern = " <Image Name=\"EmployeeSignature\">" +
    131. " <Source>EmbeddedSource>" +
    132. " <Value>SignatureImgValue>" +
    133. " <MIMEType>image/pngMIMEType>" +
    134. " <Sizing>AutoSizeSizing>" +
    135. " <Top>@TopPositioncmTop>" +
    136. " <Left>@LeftPositioncmLeft>" +
    137. " <Height>3.9cmHeight>" +
    138. " <Width>3.9cmWidth>" +
    139. " <ZIndex>1ZIndex>" +
    140. " <Visibility>" +
    141. " <Hidden>=IIF(First(Fields!Reviewed.Value, \"DataLogoData\")=\"√\", False, True)Hidden>" +
    142. " Visibility>" +
    143. " <Style>" +
    144. " <Border><Style>NoneStyle>Border>" +
    145. " Style>" +
    146. " Image>";

    当然也对DataSets和Tablix定义一个格式
     

    1. internal class ReportItemPattern
    2. {
    3. public string DataSetName { get; set; }
    4. public string DataSetString { get; set; }
    5. public string TablixString { get; set; }
    6. public dynamic Data { get; set; }
    7. public string DataSetPattern
    8. {
    9. get
    10. {
    11. return " \"@DataSetNameData\">" +
    12. " @Fields" +
    13. " " +
    14. " DummyDataSource" +
    15. " " +
    16. " " +
    17. " ";
    18. }
    19. }
    20. public string TablixPattern
    21. {
    22. get
    23. {
    24. return " \"Tablix@DataSetName\">" +
    25. " " +
    26. " @TablixColumns" +
    27. " " +
    28. " " +
    29. " 0.5cm" +
    30. " @TablixHeader" +
    31. " " +
    32. " " +
    33. " 0.5cm" +
    34. " @TablixCells" +
    35. " " +
    36. "@TotalRow" +
    37. " " +
    38. " " +
    39. " " +
    40. " @TablixMember" +
    41. " " +
    42. " " +
    43. " " +
    44. "@MergeMember" +
    45. " " +
    46. " After" +
    47. " true" +
    48. " " +
    49. " " +
    50. " \"详细信息@DataSetName\" />" +
    51. " " +
    52. "@TotalMember" +
    53. " " +
    54. " " +
    55. " @DataSetNameData" +
    56. " @TopPositioncm" +
    57. " @LeftPostioncm" +
    58. " 0.5cm" +
    59. " 1.0cm" +
    60. " " +
    61. " " +
    62. " " +
    63. "";
    64. }
    65. }
    66. }

    之前已经说了这里的数据列的数据类型全部用字符System.String,可数据库里可不止有字符类别啊,那就用下面的方法来转换,部分值也相应转换一下,比如Bool字段的”True“转换成”√“,数字空值转换成0等等。
     

    1. internal static class DynamicReportExtension
    2. {
    3. public static dynamic RemoveZeroData(this object data)
    4. {
    5. if (data is DataTable) return ((DataTable)data).ChangeEachColumnTypeToString();
    6. return data;
    7. }
    8. public static DataTable ChangeEachColumnTypeToString(this DataTable dt)
    9. {
    10. DataTable tempdt = new DataTable();
    11. foreach (DataColumn dc in dt.Columns)
    12. {
    13. DataColumn tempdc = new DataColumn();
    14. tempdc.ColumnName = dc.ColumnName;
    15. tempdc.DataType = typeof(String);
    16. tempdt.Columns.Add(tempdc);
    17. //System.Windows.Forms.MessageBox.Show(dc.DataType.ToString());
    18. }
    19. int coloumCount = dt.Columns.Count;
    20. foreach (DataRow dr in dt.Rows)
    21. {
    22. var newrow = tempdt.NewRow();
    23. for (int i = 0; i < coloumCount; i++)
    24. {
    25. var value = dr[i].ToString();
    26. switch (value)
    27. {
    28. case "":
    29. if (dt.Columns[i].DataType.ToString().IndexOf("System.Int") > -1) newrow[i] = "0";
    30. if (dt.Columns[i].DataType.ToString().IndexOf("System.Decimal") > -1) newrow[i] = "0.00";
    31. break;
    32. case "True":
    33. newrow[i] = "√";
    34. break;
    35. case "False":
    36. newrow[i] = "-";
    37. break;
    38. case "0.00%":
    39. newrow[i] = "-";
    40. break;
    41. default:
    42. newrow[i] = value;
    43. break;
    44. }
    45. }
    46. tempdt.Rows.Add(newrow);
    47. }
    48. return tempdt;
    49. }
    50. }

           到此,主体的基础部分已经完成,下面就是操作相应的节点处理了。

    动态RDLC报表(一)

    动态RDLC报表(二)

    动态RDLC报表(三)

    动态RDLC报表(四)

    动态RDLC报表(五)

    动态RDLC报表(六)

    动态RDLC报表(七)

    动态RDLC报表完整实例下载

  • 相关阅读:
    (附源码)springboot音乐播放小程序 毕业设计 031306
    Linux开发:进程间通过Unix Domain Socket传递文件描述符
    打破千篇一律,DIY属于自己独一无二的商城
    【SpringCloud】认识微服务
    Sourcemap安全问题
    《优化接口设计的思路》系列:第二篇—接口用户上下文的设计与实现
    uniapp h5 MD5加密
    65、内网安全-域环境&工作组&局域网探针方案
    python库sqlalchemy使用教程
    react+node.js+mysql 前后端分离项目 宝塔面板 部署到腾讯云服务器
  • 原文地址:https://blog.csdn.net/xgh815/article/details/126244394