【Stimulsoft Reports.WinForms教程】在運(yùn)行時(shí)使用交叉表創(chuàng)建報(bào)表
【下載Stimulsoft Reports.Ultimate最新版本】
此示例顯示如何使用代碼中的交叉表組件創(chuàng)建報(bào)表,Сross-Tab是Business Analysis的工具,這是一個(gè)特殊組件,用于處理,分組和匯總數(shù)據(jù)源中的數(shù)據(jù),結(jié)果以表格的形式呈現(xiàn),要從代碼創(chuàng)建交叉表,需要?jiǎng)?chuàng)建一個(gè)主要的交叉表組件:
#region CrossTab Stimulsoft.Report.CrossTab.StiCrossTab crossTab1 = new Stimulsoft.Report.CrossTab.StiCrossTab(); crossTab1.ClientRectangle = new Stimulsoft.Base.Drawing.RectangleD(1.8, 4.6, 14.6, 13); crossTab1.DataSourceName = "Categories"; crossTab1.Name = "CrossTab1"; ...
交叉表是一組單元格,由行和列列表組成,此外,交叉表組件具有每行和每列的總單元格和總和,現(xiàn)在將行,列和總單元格添加到創(chuàng)建的Cross-Tab組件:
... Stimulsoft.Report.CrossTab.StiCrossRowTotal crossTab1_RowTotal1 = new Stimulsoft.Report.CrossTab.StiCrossRowTotal(); crossTab1_RowTotal1.Guid = "416a93a6cbff4f24929c07006f5f4c21"; crossTab1_RowTotal1.Name = "CrossTab1_RowTotal1"; crossTab1_RowTotal1.Text.Value = "Total"; Stimulsoft.Report.CrossTab.StiCrossTitle crossTab1_Row1_Title = new Stimulsoft.Report.CrossTab.StiCrossTitle(); crossTab1_Row1_Title.Name = "CrossTab1_Row1_Title"; crossTab1_Row1_Title.TypeOfComponent = "Row:CrossTab1_Row1"; crossTab1_Row1_Title.Text.Value = "CategoryID"; Stimulsoft.Report.CrossTab.StiCrossColumnTotal crossTab1_ColTotal1 = new Stimulsoft.Report.CrossTab.StiCrossColumnTotal(); crossTab1_ColTotal1.Guid = "9e5a67edfe87448e96ebcf75e4ef19c4"; crossTab1_ColTotal1.Name = "CrossTab1_ColTotal1"; crossTab1_ColTotal1.Text.Value = "Total"; ...
交叉表中顯示的值和總計(jì)的值可能不同,可以使用DisplayValue.Value屬性設(shè)置要在單元格中顯示的值,可以使用Row1.Value.Value屬性設(shè)置要匯總的值,還需要為左右交叉表列設(shè)置標(biāo)題,定義交叉表的 Data columns數(shù)據(jù)列和標(biāo)題:
... Stimulsoft.Report.CrossTab.StiCrossTitle crossTab1_LeftTitle = new Stimulsoft.Report.CrossTab.StiCrossTitle(); crossTab1_LeftTitle.Guid = "a4a019be008042c9a4c4b604e041ceba"; crossTab1_LeftTitle.Name = "CrossTab1_LeftTitle"; crossTab1_LeftTitle.TypeOfComponent = "LeftTitle"; crossTab1_LeftTitle.Text.Value = "Categories"; Stimulsoft.Report.CrossTab.StiCrossRow crossTab1_Row1 = new Stimulsoft.Report.CrossTab.StiCrossRow(); crossTab1_Row1.Alias = "CategoryID"; crossTab1_Row1.Guid = "7f0d8b9785504d009e6afe47f70a74d3"; crossTab1_Row1.Name = "CrossTab1_Row1"; crossTab1_Row1.TotalGuid = "416a93a6cbff4f24929c07006f5f4c21"; crossTab1_Row1.DisplayValue.Value = "{Categories.CategoryID}"; crossTab1_Row1.Value.Value = "{Categories.CategoryID}"; Stimulsoft.Report.CrossTab.StiCrossColumn crossTab1_Column1 = new Stimulsoft.Report.CrossTab.StiCrossColumn(); crossTab1_Column1.Alias = "CategoryName"; crossTab1_Column1.Guid = "fc86b73eb9694091b62b55fce6041715"; crossTab1_Column1.Name = "CrossTab1_Column1"; crossTab1_Column1.TotalGuid = "9e5a67edfe87448e96ebcf75e4ef19c4"; crossTab1_Column1.DisplayValue.Value = "{Categories.CategoryName}"; crossTab1_Column1.Value.Value = "{Categories.CategoryName}"; Stimulsoft.Report.CrossTab.StiCrossSummary crossTab1_Sum1 = new Stimulsoft.Report.CrossTab.StiCrossSummary(); crossTab1_Sum1.Alias = "Description"; crossTab1_Sum1.Guid = "ec4c270655bf49a58766bf36a2b21c5c"; crossTab1_Sum1.Name = "CrossTab1_Sum1"; crossTab1_Sum1.Summary = Stimulsoft.Report.CrossTab.Core.StiSummaryType.None; crossTab1_Sum1.Value.Value = "{Categories.Description}"; Stimulsoft.Report.CrossTab.StiCrossTitle crossTab1_RightTitle = new Stimulsoft.Report.CrossTab.StiCrossTitle(); crossTab1_RightTitle.Guid = "43929f3151c248b6b4e07b0a8ea44f93"; crossTab1_RightTitle.Name = "CrossTab1_RightTitle"; crossTab1_RightTitle.TypeOfComponent = "RightTitle"; crossTab1_RightTitle.Text.Value = "CategoryName"; #endregion
該的button1_Click方法讀取XML數(shù)據(jù),登記在新的報(bào)表,并增加了交叉表prevously創(chuàng)建:
private void button1_Click(object sender, EventArgs e) { var data = new DataSet(); data.ReadXmlSchema("..\\..\\Data\\Demo.xsd"); data.ReadXml("..\\..\\Data\\Demo.xml"); var report = new StiReport(); report.RegData("Demo", data); report.Dictionary.Synchronize(); #region CrossTab ... #endregion report.Pages[0].Components.Add(crossTab1); crossTab1.Components.AddRange(new Stimulsoft.Report.Components.StiComponent[] { crossTab1_RowTotal1, crossTab1_Row1_Title, crossTab1_ColTotal1, crossTab1_LeftTitle, crossTab1_Row1, crossTab1_Column1, crossTab1_Sum1, crossTab1_RightTitle}); report.Show(); }
示例代碼的結(jié)果如下圖所示: