數據透視表提供的數據三維視圖效果
目標是
在大多數情況下
代碼
SELECT
SalesPeople
FROM
Sales
JOIN
SalesPeople WITH (NOLOCK)
ON SalesPeople
JOIN
Products WITH (NOLOCK)
ON Products
GROUP BY
SalesPeople
該查詢會產生下面的數據表
Sales Person
Product
Quantity
Sale Amount
John
Pens
John
Pencils
John
Notebooks
John
Rulers
John
Calculators
John
Back Packs
Jane
Pens
Jane
Pencils
Jane
Notebooks
Jane
Rulers
Jane
Calculators
Jane
Back Packs
Sally
Pens
Sally
Pencils
Sally
Notebooks
Sally
Rulers
Sally
Calculators
Sally
Back Packs
Sarah
Pens
Sarah
Pencils
Sarah
Notebooks
Sarah
Rulers
Sarah
Calculators
Sarah
Back Packs
正如你所看到的
數據透視表有
X軸構成了在表格上方的大標題
一個非常重要的一點是
因此
Pivot 類將數據表轉換成html table
代碼
#region Variables
private DataTable _DataTable;
private string _CssTopHeading;
private string _CssSubHeading;
private string _CssLeftColumn;
private string _CssItems;
private string _CssTotals;
private string _CssTable;
#endregion Variables
#region Constructors
public Pivot(DataTable dataTable)
{
Init();
_DataTable = dataTable;
}
#endregion Constructors
這部分的代碼是非常自我解釋
代碼
private string FindValue(string xAxisField
{
string zAxisValue =
try
{
foreach (DataRow row in _DataTable
{
if (Convert
{
zAxisValue = Convert
break;
}
}
}
catch
{
throw;
}
return zAxisValue;
}
在FindValue(
代碼
private string[] FindValues(string xAxisField
{
int zAxis = zAxisFields
if (zAxis <
zAxis++;
string[] zAxisValues = new string[zAxis];
//set default values
for (int i =
{
zAxisValues[i] =
}
try
{
foreach (DataRow row in _DataTable
{
if (Convert
{
for (int z =
{
zAxisValues[z] = Convert
}
break;
}
}
}
catch
{
throw;
}
return zAxisValues;
}
在FindValues(
代碼
private void MainHeaderTopCellStyle(HtmlTableCell cell)
{
if (_CssTopHeading ==
{
cell
cell
cell
cell
cell
cell
}
else
cell
}
這是CSS樣式的方法之一
代碼
/// <summary>
/// Creates an advanced
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <returns>HtmlTable Control
public HtmlTable PivotTable(string xAxisField
{
HtmlTable table = new HtmlTable();
//style table
TableStyle(table);
/*
* The x
* The z
* The y
*/
try
{
//get distinct xAxisFields
ArrayList xAxis = new ArrayList();
foreach (DataRow row in _DataTable
{
if (!xAxis
xAxis
}
//get distinct yAxisFields
ArrayList yAxis = new ArrayList();
foreach (DataRow row in _DataTable
{
if (!yAxis
yAxis
}
//create a
int zAxis = zAxisFields
if (zAxis <
zAxis =
string[
string[] zAxisValues = new string[zAxis];
for (int y =
{
//rows
for (int x =
{
//main columns
//get the z
zAxisValues = FindValues(xAxisField
for (int z =
{
//sub columns
matrix[(((x +
}
}
}
//calculate totals for the y
decimal[] yTotals = new decimal[(xAxis
for (int col =
{
yTotals[col] =
for (int row =
{
yTotals[col] += Convert
}
}
//calculate totals for the x
decimal[
for (int y =
{
int zCount =
for (int z =
{
xTotals[zCount
if (zCount == (zAxis
zCount =
else
zCount++;
}
}
for (int xx =
{
for (int xy =
{
xTotals[xx
}
}
//Build HTML Table
//Append main row (x
HtmlTableRow mainRow = new HtmlTableRow();
mainRow
for (int x =
{
HtmlTableCell cell = new HtmlTableCell();
cell
if (x < xAxis
cell
else
cell
//style cell
MainHeaderTopCellStyle(cell);
mainRow
}
table
//Append sub row (z
HtmlTableRow subRow = new HtmlTableRow();
subRow
subRow
//style cell
SubHeaderCellStyle(subRow
for (int x =
{
for (int z =
{
HtmlTableCell cell = new HtmlTableCell();
cell
//style cell
SubHeaderCellStyle(cell);
subRow
}
}
table
//Append table items from matrix
for (int y =
{
HtmlTableRow itemRow = new HtmlTableRow();
for (int z =
{
HtmlTableCell cell = new HtmlTableCell();
if (z ==
{
cell
//style cell
MainHeaderLeftCellStyle(cell);
}
else
{
cell
//style cell
ItemCellStyle(cell);
}
itemRow
}
//append x
for (int z =
{
HtmlTableCell cell = new HtmlTableCell();
cell
//style cell
TotalCellStyle(cell);
itemRow
}
table
}
//append y
HtmlTableRow totalRow = new HtmlTableRow();
for (int x =
{
HtmlTableCell cell = new HtmlTableCell();
if (x ==
cell
else
cell
//style cell
TotalCellStyle(cell);
totalRow
}
//append x
for (int z =
{
HtmlTableCell cell = new HtmlTableCell();
cell
//style cell
TotalCellStyle(cell);
totalRow
}
table
}
catch
{
throw;
}
return table;
}
PivotTable(…) 方法
Pivot
代碼
public DataTable DataTableForTesting
{
get
{
DataTable dt = new DataTable(
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
dt
return dt;
}
}
我已創建數據表的屬性
代碼
protected void Page_Load(object sender
{
//Advanced Pivot
Pivot advPivot = new Pivot(DataTableForTesting);
HtmlTable advancedPivot = advPivot
div
//Simple Pivot
Pivot pivot = new Pivot(DataTableForTesting);
//override default style with css
pivot
pivot
pivot
pivot
pivot
HtmlTable simplePivot = pivot
div
}
上述代碼包括兩個實例化的pivot對象
使用默認樣式的高級的數據透視表
John
Jane
Sally
Sarah
Grand Totals
Product
Sale Amount
Quantity
Sale Amount
Quantity
Sale Amount
Quantity
Sale Amount
Quantity
Sale Amount
Quantity
Pens
使用自定義的CSS樣式簡單的數據透視表
Sales Person
Pens
Pencils
Notebooks
Rulers
Calculators
Back Packs
Grand Totals
John
Jane
Sally
Sarah
Totals
From:http://tw.wingwit.com/Article/program/net/201311/13802.html