您的位置: 首页 > 技术文档 > 网络编程 > ASP.NET实现类似Excel的数据透视表
详解 VS 2010中WF 4.0的应用 回到列表 asp操作Excel类
 ASP.NET实现类似Excel的数据透视表

作者:朱祁林 时间: 2010-01-07 文档类型:原创 来自:朱祁林

第 1 页 ASP.NET实现类似Excel的数据透视表 [1]
第 2 页 ASP.NET实现类似Excel的数据透视表 [2]
第 3 页 ASP.NET实现类似Excel的数据透视表 [3]
第 4 页 ASP.NET实现类似Excel的数据透视表 [4]

在FindValue(...)方法在数据表中搜索的对应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轴的值。这是用于高级的数据透视表,对应于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类名给这个属性,该方法将使用默认的样式。 CSS类将会被应用到网页中的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(…) 方法,是所有神奇发生的地方。有两种重载方法,一个创建了一个简单的数据透视表,而其他(上面的方法)创建一个高级的数据透视表。唯一的区别在于,一个简单只有一个的z轴,而高级的,不止一个。

出处:朱祁林
责任编辑:bluehearts

上一页 ASP.NET实现类似Excel的数据透视表 [2] 下一页 ASP.NET实现类似Excel的数据透视表 [4]

◎进入论坛网络编程版块参加讨论

相关文章 更多相关链接
asp操作Excel类
ASP.NET 与 PHP 正面交锋
ASP.NET跨页面传值技巧总结
ASP.NET缓存
PHP与ASP.NET优劣势分析
关键字搜索 常规搜索 推荐文档
热门搜索:CSS Fireworks 设计比赛 网页制作 web标准 用户体验 UE photoshop Dreamweaver Studio8 Flash 手绘 CG
站点最新 站点最新列表
周大福“敬•自然”设计大赛开启
国际体验设计大会7月将在京举行
中国国防科技信息中心标志征集
云计算如何让安全问题可控
云计算是多数企业唯一拥抱互联网的机会
阿里行云
云手机年终巨献,送礼标配299起
阿里巴巴CTO王坚的"云和互联网观"
1499元买真八核 云OS双蛋大促
首届COCO桌面手机主题设计大赛
栏目最新 栏目最新列表
浅谈JavaScript编程语言的编码规范
如何在illustrator中绘制台历
Ps简单绘制一个可爱的铅笔图标
数据同步算法研究
用ps作简单的作品展示页面
CSS定位机制之一:普通流
25个最佳最闪亮的Eclipse开发项目
Illustrator中制作针线缝制文字效果
Photoshop制作印刷凹凸字体
VS2010中创建自定义SQL Rule
>> 分页 首页 前页 后页 尾页 页次:3/41个记录/页 转到 页 共4个记录

蓝色理想版权申明:除部分特别声明不要转载,或者授权我站独家播发的文章外,大家可以自由转载我站点的原创文章,但原作者和来自我站的链接必须保留(非我站原创的,按照原来自一节,自行链接)。文章版权归我站和作者共有。

转载要求:转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印,亦不能抹去我站点水印。

特别注意:本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有,文章若有侵犯作者版权,请与我们联系,我们将立即删除修改。

您的评论
用户名:  口令:
说明:输入正确的用户名和密码才能参与评论。如果您不是本站会员,你可以注册 为本站会员。
注意:文章中的链接、内容等需要修改的错误,请用报告错误,以利文档及时修改。
不评分 1 2 3 4 5
注意:请不要在评论中含与内容无关的广告链接,违者封ID
请您注意:
·不良评论请用报告管理员,以利管理员及时删除。
·尊重网上道德,遵守中华人民共和国的各项有关法律法规
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·本站评论管理人员有权保留或删除其管辖评论中的任意内容
·您在本站发表的作品,本站有权在网站内转载或引用
·参与本评论即表明您已经阅读并接受上述条款
推荐文档 | 打印文档 | 评论文档 | 报告错误  
专业书推荐 更多内容
网站可用性测试及优化指南
《写给大家看的色彩书1》
《跟我去香港》
众妙之门—网站UI 设计之道
《Flex 4.0 RIA开发宝典》
《赢在设计》
犀利开发—jQuery内核详解与实践
作品集 更多内容

杂⑦杂⑧ Gold NORMANA V2