原創|使用教程|編輯:張瑩心|2021-09-27 14:57:20.327|閱讀 446 次
概述:在FastReport .NET中,可以通過一維數組在多個頁面上對類似矩陣進行排序。按照自己的意愿來操作矩陣中的數據,并在報告的不同頁面上應用相同的排序順序。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
報表生成器FastReport .NET是適用于.NET Core 3,ASP.NET,MVC和Windows窗體的全功能報告庫。使用FastReport .NET,您可以創建獨立于應用程序的.NET報告。
假設我們有一個任務:按照所需的順序對第一頁上的矩陣進行排序,記住這個順序并在其他頁面上申請類似的矩陣。
當報告中有多個頁面顯示標題相同但包含不同數據的矩陣時,可能需要這樣做。例如,第一個矩陣顯示銷售的產品數量,第二個矩陣顯示按產品分類的銷售額。我們需要按數量或金額排序,然后對第二個矩陣應用相同的順序。這種情況在分析報告中很常見。
讓我們在實踐中看到它。我們采取一個完全假設的水果銷售統計數據。但是,只有水果的種類是不夠的,還會有水果進口國的名單。售出商品數量將顯示三年。
表結構:
標準的排序機制在這里對我們沒有幫助。因此,我們將對每個國家銷售的水果數量進行排序。讓我們概述一系列步驟:
2.1. 獲取帶有水果類型的單元格的值以及每年銷售的產品數量;
2.2. 對所需年份的值進行排序;
2.3. 對于每一行,根據排序列表中行的索引填充水果的單元格和所有年份的數字。
第一列是國家,這對我們來說沒問題,這意味著我們將對其余列的單元格進行排序。我們首先需要記住它們,以便我們可以根據排序計劃將它們排列成所需的順序。我們將選擇包含特定年份數據的列之一,并按降序或升序對其進行排序。然后我們將使用生成的索引順序按列對所有單元格進行排序。
矩陣有一個用于修改已構造對象的事件 - ModifyResult。讓我們在報告腳本中為此事件創建一個處理程序。
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 } } }
現在我們復制帶有矩陣的報告頁面,但我們將輸出總和,而不是金額字段。
我們將選擇我們在矩陣事件中為ModifyResult創建的處理程序。
運行報告后,我們會看到兩頁上的水果類型的順序是一樣的。這意味著,第一頁上的排序適用于第二頁。
因此,使用報告腳本,我們可以按照自己的意愿來操作矩陣中的數據。最重要的是,在報告的不同頁面上應用相同的排序順序。
如果您有任何疑問或需求,請隨時加入FastReport技術交流群(702295239),我們很高興為您提供查詢和咨詢。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn