原創(chuàng)|使用教程|編輯:張瑩心|2021-09-27 14:57:20.327|閱讀 446 次
概述:在FastReport .NET中,可以通過一維數(shù)組在多個(gè)頁面上對(duì)類似矩陣進(jìn)行排序。按照自己的意愿來操作矩陣中的數(shù)據(jù),并在報(bào)告的不同頁面上應(yīng)用相同的排序順序。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
報(bào)表生成器FastReport .NET是適用于.NET Core 3,ASP.NET,MVC和Windows窗體的全功能報(bào)告庫。使用FastReport .NET,您可以創(chuàng)建獨(dú)立于應(yīng)用程序的.NET報(bào)告。
假設(shè)我們有一個(gè)任務(wù):按照所需的順序?qū)Φ谝豁撋系木仃囘M(jìn)行排序,記住這個(gè)順序并在其他頁面上申請(qǐng)類似的矩陣。
當(dāng)報(bào)告中有多個(gè)頁面顯示標(biāo)題相同但包含不同數(shù)據(jù)的矩陣時(shí),可能需要這樣做。例如,第一個(gè)矩陣顯示銷售的產(chǎn)品數(shù)量,第二個(gè)矩陣顯示按產(chǎn)品分類的銷售額。我們需要按數(shù)量或金額排序,然后對(duì)第二個(gè)矩陣應(yīng)用相同的順序。這種情況在分析報(bào)告中很常見。
讓我們?cè)趯?shí)踐中看到它。我們采取一個(gè)完全假設(shè)的水果銷售統(tǒng)計(jì)數(shù)據(jù)。但是,只有水果的種類是不夠的,還會(huì)有水果進(jìn)口國的名單。售出商品數(shù)量將顯示三年。
表結(jié)構(gòu):
標(biāo)準(zhǔn)的排序機(jī)制在這里對(duì)我們沒有幫助。因此,我們將對(duì)每個(gè)國家銷售的水果數(shù)量進(jìn)行排序。讓我們概述一系列步驟:
2.1. 獲取帶有水果類型的單元格的值以及每年銷售的產(chǎn)品數(shù)量;
2.2. 對(duì)所需年份的值進(jìn)行排序;
2.3. 對(duì)于每一行,根據(jù)排序列表中行的索引填充水果的單元格和所有年份的數(shù)字。
第一列是國家,這對(duì)我們來說沒問題,這意味著我們將對(duì)其余列的單元格進(jìn)行排序。我們首先需要記住它們,以便我們可以根據(jù)排序計(jì)劃將它們排列成所需的順序。我們將選擇包含特定年份數(shù)據(jù)的列之一,并按降序或升序?qū)ζ溥M(jìn)行排序。然后我們將使用生成的索引順序按列對(duì)所有單元格進(jìn)行排序。
矩陣有一個(gè)用于修改已構(gòu)造對(duì)象的事件 - ModifyResult。讓我們?cè)趫?bào)告腳本中為此事件創(chuàng)建一個(gè)處理程序。
private List<List<int>> sortOrders = new List<List<int>>(); //List of sorting orders for each collection of fruit species by country private void Matrix1_ModifyResult(object sender, EventArgs e) { //Dictionaries in which we will store the row index and cell value Dictionary<int, double> firstYearCells = new Dictionary<int, double>(); Dictionary<int, double> secondYearCells = new Dictionary<int, double>(); Dictionary<int, double> thirdYearCells = new Dictionary<int, double>(); Dictionary<int, string> typeCells = new Dictionary<int, string>(); Dictionary<int, double> sortCells = new Dictionary<int, double>(); //bool prevYearSortNeeded = false; var total = false; var z = 1; var val2 = 0.0; var val3 = 0.0; List<string> countries = new List<string>(); //We will store the list of countries in this list //We get all countries from the first column for (int j=2; j<(sender as TableBase).ResultTable.RowCount-1; j++) { try { var val = (sender as TableBase).ResultTable.GetCellData(0,j).Value.ToString(); if (val.Length > 0) countries.Add(val); } catch (Exception) {} } int columnFirstYearIndex=0; int columnSecondYearIndex=0; int columnThirdYearIndex=0; int columnTypeIndex=0; //We go through all the columns of the matrix to save the cells in dictionaries for (int t=0; t < (sender as TableBase).ResultTable.ColumnCount; t++) { if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("2017")) { columnFirstYearIndex=t; } if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("2018")) { columnSecondYearIndex=t; } if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("2019")) { columnThirdYearIndex=t; } if ((sender as TableBase).ResultTable.GetCellData(t,0).Text.Contains("Fruit")) { columnTypeIndex=t; } } int countryOrder =0; //We run a loop to identify the fruit groups and sort them for each country foreach (var country in countries) { total = false; sortCells.Clear(); //We clear the list for sorting //We select cells from rows until we see Total, since Total should not be sorted while (!total) { if ((string)(sender as TableBase).ResultTable.GetCellData(columnTypeIndex,z).Text!="Total") { //We select cells for the first year var value = (sender as TableBase).ResultTable.GetCellData(columnFirstYearIndex,z).Value; if (value!=null) { Double.TryParse(value.ToString(),out val3); firstYearCells.Add(z,val3); } else firstYearCells.Add(z, 0.0); //We select cells for the second year value = (sender as TableBase).ResultTable.GetCellData(columnSecondYearIndex,z).Value; if (value!=null) { Double.TryParse(value.ToString(),out val3); secondYearCells.Add(z,val3); } else secondYearCells.Add(z, 0.0); //We select cells for the third year value = (sender as TableBase).ResultTable.GetCellData(columnThirdYearIndex,z).Value; if (value!=null) { Double.TryParse(value.ToString(),out val3); thirdYearCells.Add(z,val3); } else thirdYearCells.Add(z, 0.0); //We select cells for fruit types value = (sender as TableBase).ResultTable.GetCellData(columnTypeIndex,z).Text; typeCells.Add(z,value.ToString()); } else { //Exit condition of the loop total = true; } z++; } sortCells = firstYearCells; //We set the column for sorting - in this case by the first year List<int> keys = new List<int>(); //If we have a filled list of sorts for all countries, then the first page of the report has been built and you can use this list on the second page. This is where sorting through one-dimensional array is ensured. if ( sortOrders.Count == countries.Count ) { keys = sortOrders.ElementAt(countryOrder); } else keys = sortCells.OrderByDescending(i=>i.Value).Select(key => key.Key).ToList(); //Sort the array in descending order using the Linq library int k = 0; //Loop through all the elements of the sorted list foreach(var key in keys) { //Build cell values for all columns in sort order (sender as TableBase).ResultTable.GetCellData(columnFirstYearIndex, firstYearCells.Keys.ElementAt(k)).Text = firstYearCells[key].ToString(); (sender as TableBase).ResultTable.GetCellData(columnSecondYearIndex, secondYearCells.Keys.ElementAt(k)).Text = secondYearCells[key].ToString(); (sender as TableBase).ResultTable.GetCellData(columnThirdYearIndex, thirdYearCells.Keys.ElementAt(k)).Text = thirdYearCells[key].ToString(); (sender as TableBase).ResultTable.GetCellData(columnTypeIndex, typeCells.Keys.ElementAt(k)).Text = typeCells[key].ToString(); k++; } if (keys.Count>0) sortOrders.Add(new List<int>(keys)); //Save the sort order for the current country //It's important to clear firstYearCells.Clear(); secondYearCells.Clear(); thirdYearCells.Clear(); typeCells.Clear(); countryOrder++; //Go to the next country } } }
現(xiàn)在我們復(fù)制帶有矩陣的報(bào)告頁面,但我們將輸出總和,而不是金額字段。
我們將選擇我們?cè)诰仃囀录袨镸odifyResult創(chuàng)建的處理程序。
運(yùn)行報(bào)告后,我們會(huì)看到兩頁上的水果類型的順序是一樣的。這意味著,第一頁上的排序適用于第二頁。
因此,使用報(bào)告腳本,我們可以按照自己的意愿來操作矩陣中的數(shù)據(jù)。最重要的是,在報(bào)告的不同頁面上應(yīng)用相同的排序順序。
如果您有任何疑問或需求,請(qǐng)隨時(shí)加入FastReport技術(shù)交流群(702295239),我們很高興為您提供查詢和咨詢。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn