Pivot.zip文件中包括兩個(gè)解決方案。Pivot 是一個(gè)類(lèi)庫(kù)解決方案是。您可以編譯此解決方案和在Web應(yīng)用程序中引用Pivot.dll。另一個(gè)解決方案是PivotTest,它是是一個(gè)ASP.NET應(yīng)用程序。這說(shuō)明如何實(shí)現(xiàn)Pivot類(lèi)。
public DataTable DataTableForTesting { get { DataTable dt = new DataTable("Sales Table"); dt.Columns.Add("Sales Person"); dt.Columns.Add("Product"); dt.Columns.Add("Quantity"); dt.Columns.Add("Sale Amount"); dt.Rows.Add(new object[] { "John", "Pens", 200, 350.00 }); dt.Rows.Add(new object[] { "John", "Pencils", 400, 500.00 }); dt.Rows.Add(new object[] { "John", "Notebooks", 100, 300.00 }); dt.Rows.Add(new object[] { "John", "Rulers", 50, 100.00 }); dt.Rows.Add(new object[] { "John", "Calculators", 120, 1200.00 }); dt.Rows.Add(new object[] { "John", "Back Packs", 75, 1500.00 }); dt.Rows.Add(new object[] { "Jane", "Pens", 225, 393.75 }); dt.Rows.Add(new object[] { "Jane", "Pencils", 335, 418.75 }); dt.Rows.Add(new object[] { "Jane", "Notebooks", 200, 600.00 }); dt.Rows.Add(new object[] { "Jane", "Rulers", 75, 150.00 }); dt.Rows.Add(new object[] { "Jane", "Calculators", 80, 800.00 }); dt.Rows.Add(new object[] { "Jane", "Back Packs", 97, 1940.00 }); dt.Rows.Add(new object[] { "Sally", "Pens", 202, 353.50 }); dt.Rows.Add(new object[] { "Sally", "Pencils", 303, 378.75 }); dt.Rows.Add(new object[] { "Sally", "Notebooks", 198, 600.00 }); dt.Rows.Add(new object[] { "Sally", "Rulers", 98, 594.00 }); dt.Rows.Add(new object[] { "Sally", "Calculators", 80, 800.00 }); dt.Rows.Add(new object[] { "Sally", "Back Packs", 101, 2020.00 }); dt.Rows.Add(new object[] { "Sarah", "Pens", 112, 196.00 }); dt.Rows.Add(new object[] { "Sarah", "Pencils", 245, 306.25 }); dt.Rows.Add(new object[] { "Sarah", "Notebooks", 198, 594.00 }); dt.Rows.Add(new object[] { "Sarah", "Rulers", 50, 100.00 }); dt.Rows.Add(new object[] { "Sarah", "Calculators", 66, 660.00 }); dt.Rows.Add(new object[] { "Sarah", "Back Packs", 50, 2020.00 }); return dt; } }
我已創(chuàng)建數(shù)據(jù)表的屬性,它建立在上面的例子中的數(shù)據(jù)表。這只是用于演示目的。
protected void Page_Load(object sender, EventArgs e) { //Advanced Pivot Pivot advPivot = new Pivot(DataTableForTesting); HtmlTable advancedPivot = advPivot.PivotTable("Sales Person", "Product", new string[] { "Sale Amount", "Quantity" }); div1.Controls.Add(advancedPivot); //Simple Pivot Pivot pivot = new Pivot(DataTableForTesting); //override default style with css pivot.CssTopHeading = "Heading"; pivot.CssLeftColumn = "LeftColumn"; pivot.CssItems = "Items"; pivot.CssTotals = "Totals"; pivot.CssTable = "Table"; HtmlTable simplePivot = pivot.PivotTable("Product", "Sales Person", "Sale Amount"); div2.Controls.Add(simplePivot); }
上述代碼包括兩個(gè)實(shí)例化的pivot對(duì)象。第一個(gè)高級(jí)的pivot和第二是一個(gè)簡(jiǎn)單的pivot。你可以看到我已經(jīng)為div添加了HtmlTable控件。我創(chuàng)建具有runat="server"屬性的div,這樣我可以在后臺(tái)代碼里面訪問(wèn)它。div只是幫助HtmlTable的定位。
使用默認(rèn)樣式的高級(jí)的數(shù)據(jù)透視表:
運(yùn)行代碼框
[Ctrl+A 全部選擇 提示:你可先修改部分代碼,再按運(yùn)行]
使用自定義的CSS樣式簡(jiǎn)單的數(shù)據(jù)透視表:
Sales Person |
Pens |
Pencils |
Notebooks |
Rulers |
Calculators |
Back Packs |
Grand Totals |
John |
350 |
500 |
300 |
100 |
1200 |
1500 |
3950 |
Jane |
393.75 |
418.75 |
600 |
150 |
800 |
1940 |
4302.50 |
Sally |
353.5 |
378.75 |
600 |
594 |
800 |
2020 |
4746.25 |
Sarah |
196 |
306.25 |
594 |
100 |
660 |
2020 |
3876.25 |
Totals |
1293.25 |
1603.75 |
2094 |
944 |
3460 |
7480 |
16875.00 |
代碼:Pivot.zip
參考原文:http://www.codeproject.com/KB/aspnet/Pivot.aspx
本文鏈接:http://m.95time.cn/tech/program/2010/7319.asp
出處:朱祁林
責(zé)任編輯:bluehearts
上一頁(yè) ASP.NET實(shí)現(xiàn)類(lèi)似Excel的數(shù)據(jù)透視表 [3] 下一頁(yè)
◎進(jìn)入論壇網(wǎng)絡(luò)編程版塊參加討論
|