在FindValue(...)方法在數(shù)據(jù)表中搜索的對應(yīng)x軸和y軸值的Z軸值。xAxisField是X軸字段的列名(例如“Product”),而xAxisValue是在該列的值。該yAxisField是的Y軸字段的列名(例如“Sales Person”),并yAxisValue是在該列的值。該zAxisField是列名,在其中Z軸值,是您正在尋找地(例如“Sale Amount”)。
private string[] FindValues(string xAxisField, string xAxisValue, string yAxisField, string yAxisValue, string[] zAxisFields) { int zAxis = zAxisFields.Length; if (zAxis < 1) zAxis++; string[] zAxisValues = new string[zAxis]; //set default values for (int i = 0; i <= zAxisValues.GetUpperBound(0); i++) { zAxisValues[i] = "0"; } try { foreach (DataRow row in _DataTable.Rows) { if (Convert.ToString(row[xAxisField]) == xAxisValue && Convert.ToString(row[yAxisField]) == yAxisValue) { for (int z = 0; z < zAxis; z++) { zAxisValues[z] = Convert.ToString(row[zAxisFields[z]]); } break; } } } catch { throw; } return zAxisValues; }
在FindValues(...)方法類似FindValue(...)方法,然而,它會返回多個z軸的值。這是用于高級的數(shù)據(jù)透視表,對應(yīng)于x軸的值,您會有多個Z軸列。
private void MainHeaderTopCellStyle(HtmlTableCell cell) { if (_CssTopHeading == "") { cell.Style.Add("font-family", "tahoma"); cell.Style.Add("font-size", "10pt"); cell.Style.Add("font-weight", "normal"); cell.Style.Add("background-color", "black"); cell.Style.Add("color", "white"); cell.Style.Add("text-align", "center"); } else cell.Attributes.Add("Class", _CssTopHeading); }
這是CSS樣式的方法之一。這在X軸上使用流行的樣式(table的頂行)。如果您沒有指定一個CSS類名給這個屬性,該方法將使用默認(rèn)的樣式。 CSS類將會被應(yīng)用到網(wǎng)頁中的HTML table。
/// <summary> /// Creates an advanced 3D Pivot table. /// </summary> /// <param name="xAxisField">The main heading at the top of the report.</param> /// <param name="yAxisField">The heading on the left of the report.</param> /// <param name="zAxisFields">The sub heading at the top of the report.</param> /// <returns>HtmlTable Control.</returns> public HtmlTable PivotTable(string xAxisField, string yAxisField, string[] zAxisFields) { HtmlTable table = new HtmlTable(); //style table TableStyle(table); /* * The x-axis is the main horizontal row. * The z-axis is the sub horizontal row. * The y-axis is the left vertical column. */ try { //get distinct xAxisFields ArrayList xAxis = new ArrayList(); foreach (DataRow row in _DataTable.Rows) { if (!xAxis.Contains(row[xAxisField])) xAxis.Add(row[xAxisField]); } //get distinct yAxisFields ArrayList yAxis = new ArrayList(); foreach (DataRow row in _DataTable.Rows) { if (!yAxis.Contains(row[yAxisField])) yAxis.Add(row[yAxisField]); } //create a 2D array for the y-axis/z-axis fields int zAxis = zAxisFields.Length; if (zAxis < 1) zAxis = 1; string[,] matrix = new string[(xAxis.Count * zAxis), yAxis.Count]; string[] zAxisValues = new string[zAxis]; for (int y = 0; y < yAxis.Count; y++) //loop thru y-axis fields { //rows for (int x = 0; x < xAxis.Count; x++) //loop thru x-axis fields { //main columns //get the z-axis values zAxisValues = FindValues(xAxisField, Convert.ToString(xAxis[x]) , yAxisField, Convert.ToString(yAxis[y]), zAxisFields); for (int z = 0; z < zAxis; z++) //loop thru z-axis fields { //sub columns matrix[(((x + 1) * zAxis - zAxis) + z), y] = zAxisValues[z]; } } } //calculate totals for the y-axis decimal[] yTotals = new decimal[(xAxis.Count * zAxis)]; for (int col = 0; col < (xAxis.Count * zAxis); col++) { yTotals[col] = 0; for (int row = 0; row < yAxis.Count; row++) { yTotals[col] += Convert.ToDecimal(matrix[col, row]); } } //calculate totals for the x-axis decimal[,] xTotals = new decimal[zAxis, (yAxis.Count + 1)]; for (int y = 0; y < yAxis.Count; y++) //loop thru the y-axis { int zCount = 0; for (int z = 0; z < (zAxis * xAxis.Count); z++) //loop thru the z-axis { xTotals[zCount, y] += Convert.ToDecimal(matrix[z, y]); if (zCount == (zAxis - 1)) zCount = 0; else zCount++; } } for (int xx = 0; xx < zAxis; xx++) //Grand Total { for (int xy = 0; xy < yAxis.Count; xy++) { xTotals[xx, yAxis.Count] += xTotals[xx, xy]; } } //Build HTML Table //Append main row (x-axis) HtmlTableRow mainRow = new HtmlTableRow(); mainRow.Cells.Add(new HtmlTableCell()); for (int x = 0; x <= xAxis.Count; x++) //loop thru x-axis + 1 { HtmlTableCell cell = new HtmlTableCell(); cell.ColSpan = zAxis; if (x < xAxis.Count) cell.InnerText = Convert.ToString(xAxis[x]); else cell.InnerText = "Grand Totals"; //style cell MainHeaderTopCellStyle(cell); mainRow.Cells.Add(cell); } table.Rows.Add(mainRow); //Append sub row (z-axis) HtmlTableRow subRow = new HtmlTableRow(); subRow.Cells.Add(new HtmlTableCell()); subRow.Cells[0].InnerText = yAxisField; //style cell SubHeaderCellStyle(subRow.Cells[0]); for (int x = 0; x <= xAxis.Count; x++) //loop thru x-axis + 1 { for (int z = 0; z < zAxis; z++) { HtmlTableCell cell = new HtmlTableCell(); cell.InnerText = zAxisFields[z]; //style cell SubHeaderCellStyle(cell); subRow.Cells.Add(cell); } } table.Rows.Add(subRow); //Append table items from matrix for (int y = 0; y < yAxis.Count; y++) //loop thru y-axis { HtmlTableRow itemRow = new HtmlTableRow(); for (int z = 0 ; z <= (zAxis * xAxis.Count); z++) //loop thru z-axis + 1 { HtmlTableCell cell = new HtmlTableCell(); if (z == 0) { cell.InnerText = Convert.ToString(yAxis[y]); //style cell MainHeaderLeftCellStyle(cell); } else { cell.InnerText = Convert.ToString(matrix[(z-1), y]); //style cell ItemCellStyle(cell); } itemRow.Cells.Add(cell); } //append x-axis grand totals for (int z = 0; z < zAxis; z++) { HtmlTableCell cell = new HtmlTableCell(); cell.InnerText = Convert.ToString(xTotals[z, y]); //style cell TotalCellStyle(cell); itemRow.Cells.Add(cell); } table.Rows.Add(itemRow); } //append y-axis totals HtmlTableRow totalRow = new HtmlTableRow(); for (int x = 0; x <= (zAxis * xAxis.Count); x++) { HtmlTableCell cell = new HtmlTableCell(); if (x == 0) cell.InnerText = "Totals"; else cell.InnerText = Convert.ToString(yTotals[x-1]); //style cell TotalCellStyle(cell); totalRow.Cells.Add(cell); } //append x-axis/y-axis totals for (int z = 0; z < zAxis; z++) { HtmlTableCell cell = new HtmlTableCell(); cell.InnerText = Convert.ToString(xTotals[z, xTotals.GetUpperBound(1)]); //style cell TotalCellStyle(cell); totalRow.Cells.Add(cell); } table.Rows.Add(totalRow); } catch { throw; } return table; }
PivotTable(…) 方法,是所有神奇發(fā)生的地方。有兩種重載方法,一個創(chuàng)建了一個簡單的數(shù)據(jù)透視表,而其他(上面的方法)創(chuàng)建一個高級的數(shù)據(jù)透視表。唯一的區(qū)別在于,一個簡單只有一個的z軸,而高級的,不止一個。
出處:朱祁林
責(zé)任編輯:bluehearts
上一頁 ASP.NET實現(xiàn)類似Excel的數(shù)據(jù)透視表 [2] 下一頁 ASP.NET實現(xiàn)類似Excel的數(shù)據(jù)透視表 [4]
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|