国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

分享一個動態生成RDLC報表的類

2019-11-17 01:31:59
字體:
來源:轉載
供稿:網友

分享一個動態生成RDLC報表的類

在實際工作中,當需要進行大批量查詢和生成報表的時候,可以使用我寫的類。

特點:

  • 無需報表設計器、無需為報表設置數據集
  • 只需要傳入查詢結果就可以全自動生成報表,傳入的對象為Dynamic(目前支持DataTable和IEnumable<T>的傳入參數)
  • 文字、數據表可以無限添加
  • 支持圖表 (2014-5-28 v0.4 增加參數類,完成圖表顯示功能)
  • 支持數據分組(2014-5-19 v0.3 添加表格內分組)

我沒有采用使用操縱微軟報表Schema的方法,而是用了拼接字符串:(

將來想到的擴展功能有:

  • 支持子報表

更新日志:

2014-5-15 更新,將匿名類轉換成DataTable,然后去除0和0.00%等字段。

* 2014-5-19 v0.3 添加表格內分組* 2014-5-28 v0.4 增加參數類,開始做圖表顯示功能

源碼

接口:

using System.Text;using System.xml;using System.Xml.Linq;using System.Xml.Serialization;using Microsoft.Reporting.WebForms;    public interface IDynamicReport    {        void SetReport(ReportViewer reportViewer);        void AddData<T>(IEnumerable<T> data);        void AddData(DataTable dataTable);        void ShowReport();        void LoadReport(string reportPath);        void SetColoumStyle(List<ReportColoumStyle> coloumStyle);        void AddText(string title);    }

輔助類和枚舉:

    public class ReportColoumStyle    {        public string ColoumName { get; set; }        public float ColoumWidth { get; set; }        public TextAlign TextAlign { get; set; }        public ReportColoumStyle()        {            ColoumWidth = DynamicReport.ColoumWidth;        }    }    public enum TextAlign    {        Left,        Center,        Right    }    public enum ReportType    {        Tables,        Chart,        Finally    }    internal enum DataType    {        DataTable,        Enumerable    }    internal class ReportItemPattern    {        public string DataSetName { get; set; }        public string DataSetString { get; set; }        public string TablixString { get; set; }        public dynamic Data { get; set; }        public string DataSetPattern        {            get            {                return "    <DataSet Name=/"@DataSetNameData/">" +                       "       <Fields>@Fields</Fields>" +                       "       <Query>" +                       "           <DataSourceName>DummyDataSource</DataSourceName>" +                       "           <CommandText />" +                       "       </Query>" +                       "    </DataSet>";            }        }        public string TablixPattern        {            get            {                return " <Tablix Name=/"Tablix@DataSetName/">" +                       "   <TablixBody>" +                       "       <TablixColumns>@TablixColumns</TablixColumns>" +                       "       <TablixRows>" +                       "           <TablixRow>" +                       "               <Height>0.23622in</Height>" +                       "               <TablixCells>@TablixHeader</TablixCells>" +                       "           </TablixRow>" +                       "           <TablixRow>" +                       "               <Height>0.23622in</Height>" +                       "               <TablixCells>@TablixCells</TablixCells>" +                       "           </TablixRow>" +                       "       </TablixRows>" +                       "   </TablixBody>" +                       "   <TablixColumnHierarchy>" +                       "       <TablixMembers>@TablixMember</TablixMembers>" +                       "   </TablixColumnHierarchy>" +                       "   <TablixRowHierarchy>" +                       "       <TablixMembers>" +                       "           <TablixMember>" +                       "               <KeepWithGroup>After</KeepWithGroup>" +                       "           </TablixMember>" +                       "           <TablixMember>" +                       "               <Group Name=/"詳細信息@DataSetName/" />" +                       "           </TablixMember>" +                       "       </TablixMembers>" +                       "   </TablixRowHierarchy>" +                       "   <DataSetName>@DataSetNameData</DataSetName>" +                       "   <Top>@TopPositioncm</Top>" +                       "   <Left>@LeftPostioncm</Left>" +                       "   <Height>1.2cm</Height>" +                       "   <Width>14.35207cm</Width>" +                       "   <Style>" +                       "       <Border>" +                       "           <Style>None</Style>" +                       "       </Border>" +                       "   </Style>" +                       "</Tablix>";            }        }    }    internal static class DynamicReportExtension    {        public static dynamic RemoveZeroData(this object data)        {            if (data is DataTable)            {                return ((DataTable)data).ChangeEachColumnTypeToString();            }            else if (data is IEnumerable)            {                var _data = ((IEnumerable) data).Cast<object>();                return _data.CopyToDataTable().RemoveZeroData();            }            return data;        }        public static DataTable ChangeEachColumnTypeToString(this DataTable dt)        {            DataTable tempdt = new DataTable();            foreach (DataColumn dc in dt.Columns)            {                DataColumn tempdc = new DataColumn();                tempdc.ColumnName = dc.ColumnName;                tempdc.DataType = typeof (String);                tempdt.Columns.Add(tempdc);            }            int coloumCount = dt.Columns.Count;            foreach (DataRow dr in dt.Rows)            {                var newrow = tempdt.NewRow();                for (int i = 0; i < coloumCount; i ++)                {                    var value = dr[i].ToString();                    switch (value)                    {                        case "0":                        case "0.00%":                            newrow[i] = "-";                            break;                        default:                            newrow[i] = value;                            break;                    }                                    }                tempdt.Rows.Add(newrow);            }            return tempdt;        }    }    internal static class DataSetLinqOperators    {        public static DataTable CopyToDataTable<T>(this IEnumerable<T> source)        {            return new ObjectShredder<T>().Shred(source, null, null);        }        public static DataTable CopyToDataTable<T>(this IEnumerable<T> source,                                                   DataTable table, LoadOption? options)        {            return new ObjectShredder<T>().Shred(source, table, options);        }    }    internal class ObjectShredder<T>    {        PRivate FieldInfo[] _fi;        private PropertyInfo[] _pi;        private Dictionary<string, int> _ordinalMap;        private Type _type;        public ObjectShredder()        {            _type = typeof (T);            _fi = _type.GetFields();            _pi = _type.GetProperties();            _ordinalMap = new Dictionary<string, int>();        }        public DataTable Shred(IEnumerable<T> source, DataTable table, LoadOption ? options)        {            if (typeof (T).IsPrimitive)            {                return ShredPrimitive(source, table, options);            }            if (table == null)            {                table = new DataTable(typeof (T).Name);            }            // now see i
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙里县| 五大连池市| 宝坻区| 兴隆县| 孝昌县| 元阳县| 宁晋县| 金华市| 兴仁县| 诏安县| 麟游县| 西青区| 白山市| 临猗县| 太和县| 卢龙县| 蒙城县| 大余县| 龙海市| 佛冈县| 崇阳县| 易门县| 宁都县| 陇西县| 平潭县| 望城县| 呼和浩特市| 含山县| 闵行区| 金华市| 南投县| 贵阳市| 吴川市| 富川| 积石山| 资阳市| 阿拉善左旗| 西乌珠穆沁旗| 湟源县| 繁峙县| 江华|