如何创建复杂的web报表

作者:互联网   出处:控件中国网   2014-11-05 19:04:41   阅读:2

如何创建复杂的web报表
报表在我们开发过程中经常会用到,从数据库中读取数据,然后创建复杂的TABLE层现在WEB界面上,现在总结一点自己的心得,列出代码

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SpinWorkStatistics.aspx.cs"
    Inherits="Report_LabReport_SpinWorkStatistics" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>纺织工作量统计</title>
    <link href="http://www.cnblogs.com/CSS/admin.css" rel="stylesheet" type="text/css" />

    <script src="http://www.cnblogs.com/JS/datepicker.js" type="text/javascript"></script>

    <script type="text/javascript">
    function txtclear()
    {  
      document.getElementById("FromDate").value="";
      document.getElementById("ToDate").value="";
      document.getElementById("drp_Area").value="";
    }
    function checkempty()
    {
      if(document.getElementById("FromDate").value==""||document.getElementById("ToDate").value==""|| document.getElementById("drp_Area").value=="")
      {
        alert("筛选条件不能为空");      
        return false;
      }
      else
      {
        return true;
      }
   
    } 
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <input type="hidden" runat="server" id="hidUserid" />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
            <table class="formatList">
                <tr>
                    <td colspan="8" class="textColleft">
                        纺织工作量统计
                    </td>
                </tr>
                <tr>
                    <td class="labelCol">
                        <asp:Label runat="server" ID="Label39" Text="开始时间" />
                    </td>
                    <td class="textCol">
                        <asp:TextBox ID="FromDate" runat="server" CssClass="textInput" Enabled="false">
                        </asp:TextBox>
                        <a onclick="show_calendar('form1.FromDate')" href="####">
                            <img src="http://images.cnblogs.com/datepicker.gif" runat="server" id="img1" align="absMiddle"
                                border="0" /></a>
                    </td>
                    <td class="labelCol">
                        <asp:Label runat="server" ID="Label40" Text="结束时间" />
                    </td>
                    <td class="textCol">
                        <asp:TextBox ID="ToDate" runat="server" CssClass="textInput" Enabled="false">
                        </asp:TextBox>
                        <a onclick="show_calendar('form1.ToDate')" href="####">
                            <img src="http://images.cnblogs.com/datepicker.gif" runat="server" id="img2" align="absMiddle"
                                border="0" /></a>
                    </td>
                    <td class="labelCol">
                        <asp:Label runat="server" ID="Label1" Text="地区" />
                    </td>
                    <td class="textCol">
                        <asp:DropDownList ID="drp_Area" runat="server" CssClass="textInput" />
                    </td>
                    <td class="textCol" colspan="2">
                        <asp:Button ID="btnSerche" runat="server" Text="搜索" CssClass="btnCommon" OnClientClick="return checkempty()"
                            OnClick="btnSerche_Click" />
                        <input type="button" id="btnClear" value="清空" class="btnCommon" onclick="txtclear()" />
                    </td>
                </tr>
            </table>           
            <br />
            <table id="tb_htmlSpin" class="formatList" border="0" cellpadding="0" cellspacing="0">
                <asp:Literal ID="Littxt" runat="server"></asp:Literal>
            </table>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html>

C#:

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
public partial class Report_LabReport_SpinWorkStatistics : System.Web.UI.Page
{
    Edoc2v4UserLogic edoc2userlogic = new Edoc2v4UserLogic();
    ZAIQ_SampleKindLogic samplekindlogic = new ZAIQ_SampleKindLogic();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.drp_Area.DataSource = GetAreas().DefaultView;
            this.drp_Area.DataTextField = "dept_name";
            this.drp_Area.DataValueField = "dept_id";
            this.drp_Area.DataBind();

        }

    }
    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void btnSerche_Click(object sender, EventArgs e)
    {
        string datefrom = this.FromDate.Text.ToString().Trim();
        string dateend = this.ToDate.Text.ToString().Trim();
        string area = this.drp_Area.SelectedItem.Value.ToString().Trim();
        CreateTables(datefrom, dateend, area);
    }

    #region
    /// <summary>
    /// 获取区域
    /// </summary>
    /// <returns></returns>
    private DataTable GetAreas()
    {
        DataTable dt = new DataTable();
        dt = edoc2userlogic.GetAreas();
        return dt;

    }

    /// <summary>
    /// 查询项目名称
    /// </summary>
    /// <returns></returns>
    private DataTable ItemName(string fromdate, string enddate, string area)
    {
        DataTable dt = samplekindlogic.SelectItem(fromdate, enddate, area);
        return dt;
    }
    /// <summary>
    /// 查询样品类别
    /// </summary>
    /// <returns></returns>
    private DataTable SampleSort(string fromdate, string enddate, string area)
    {
        DataTable dt = samplekindlogic.SelectSampleSort(fromdate, enddate, area);
        return dt;
    }
    /// <summary>
    /// 查询纺织工作量
    /// </summary>
    /// <returns></returns>
    private DataTable SpinWorkload(string fromdate, string enddate, string area)
    {
        DataTable dt = samplekindlogic.SelectSpinWorkload(fromdate, enddate, area);
        return dt;
    }
    #endregion


    #region
    ///// <summary>
    ///// 生成Table
    ///// </summary>
    //private void CreatTable()
    //{
    //    DataTable dt_item = ItemName();
    //    DataTable dt_samkind = SampleSort();
    //    DataTable dt_SpinWork = SpinWorkload();
    //    TableRow row = new TableRow();
    //    for (int i = 0; i < dt_item.Rows.Count + 1; i++)
    //    {
    //        TableCell cell = new TableCell();
    //        if (i == 0)
    //        {
    //            cell.Text = "地区";
    //            row.Cells.Add(cell);
    //        }
    //        else
    //        {

    //            cell.Text = dt_item.Rows[i - 1][0].ToString();
    //            row.Cells.Add(cell);
    //            cell.ColumnSpan = 3;
    //        }
    //    }
    //    this.Tb_SpinBus.Rows.Add(row);
    //    for (int j = 0; j < dt_samkind.Rows.Count + 1; j++)
    //    {
    //        TableRow row1 = new TableRow();
    //        for (int k = 0; k < (dt_item.Rows.Count) * 3 + 1; k++)
    //        {
    //            TableCell cell1 = new TableCell();
    //            if (k == 0)
    //            {
    //                if (j == 0)
    //                {
    //                    cell1.Text = "";
    //                    row1.Cells.Add(cell1);

    //                }
    //                else
    //                {
    //                    cell1.Text = dt_samkind.Rows[j - 1][0].ToString();
    //                    row1.Cells.Add(cell1);
    //                }
    //            }
    //            else
    //            {
    //                if (j == 0)
    //                {
    //                    if (k % 3 == 1)
    //                    {
    //                        cell1.Text = "检测费";
    //                        row1.Cells.Add(cell1);
    //                    }
    //                    if (k % 3 == 2)
    //                    {
    //                        cell1.Text = "检测批量";
    //                        row1.Cells.Add(cell1);
    //                    }
    //                    if (k % 3 == 0)
    //                    {
    //                        cell1.Text = "检测样品量";
    //                        row1.Cells.Add(cell1);
    //                    }

    //                }
    //                else
    //                {
    //                    for (int ee = 0; ee < dt_item.Rows.Count; ee++)
    //                    {
    //                        for (int w = 0; w < dt_SpinWork.Rows.Count; w++)
    //                        {
    //                            if (dt_samkind.Rows[j - 1][0].ToString().Trim() == dt_SpinWork.Rows[w]["样品类别"].ToString().Trim() && dt_SpinWork.Rows[w]["ii_name"].ToString().Trim() == dt_item.Rows[ee][0].ToString().Trim())
    //                            {
    //                                if (k % 3 == 1)
    //                                {
    //                                    cell1.Text = dt_SpinWork.Rows[w]["检测费"].ToString();

    //                                }
    //                                if (k % 3 == 2)
    //                                {
    //                                    cell1.Text = dt_SpinWork.Rows[w]["检测批量"].ToString();

    //                                }
    //                                if (k % 3 == 0)
    //                                {
    //                                    cell1.Text = dt_SpinWork.Rows[w]["检测样品量"].ToString();
    //                                }
    //                                row1.Cells.Add(cell1);

    //                            }
    //                        }
    //                    }
    //                }
    //            }

    //        }
    //        this.Tb_SpinBus.Rows.Add(row1);

    //    }

    //}
    #endregion

    #region
    /// <summary>
    /// 静态生成table
    /// </summary>
    private void CreateTables(string fromdate, string enddate, string area)
    {
        DataTable dt_item = ItemName(fromdate, enddate, area);
        DataTable dt_samkind = SampleSort(fromdate, enddate, area);
        DataTable dt_SpinWork = SpinWorkload(fromdate, enddate, area);
        StringBuilder sb = new StringBuilder();       
        sb.Append("<tr>");
        for (int i = 0; i < dt_item.Rows.Count + 1; i++)
        {
            if (i == 0)
            {
                sb.Append("<td rowspan='2'>").Append(drp_Area.SelectedItem.Text.ToString()).Append("</td>");
            }
            else
            {
                sb.Append("<td colspan='3'>").Append(dt_item.Rows[i - 1][0].ToString()).Append("</td>");

            }
        }
        sb.Append("</tr>");
        CreateColum(ref sb, dt_samkind, dt_item, dt_SpinWork);
        CreateTotal(ref sb, dt_samkind, dt_item, dt_SpinWork);
        Littxt.Text = sb.ToString();
    }

    /// <summary>
    /// 创建小标
    /// </summary>
    /// <param name="sb"></param>
    /// <param name="samkind">样品类别</param>
    /// <param name="items">项目</param>
    /// <param name="spins">工作量</param>
    private void CreateColum(ref StringBuilder sb, DataTable samkind, DataTable items, DataTable spins)
    {
        string css = string.Empty;
        for (int j = 0; j < samkind.Rows.Count; j++)
        {
            css = (j % 2 > 0) ? "evenRow" : "oddRow";
            if (j == 0)
            {
                sb.Append("<tr>");
                for (int s = 0; s < items.Rows.Count * 3; s++)
                {
                    if (s % 3 == 1)
                    {
                        sb.Append("<td>").Append("检测费").Append("</td>");
                    }
                    if (s % 3 == 2)
                    {
                        sb.Append("<td>").Append("检测批量").Append("</td>");
                    }
                    if (s % 3 == 0)
                    {
                        sb.Append("<td>").Append("检测样品量").Append("</td>");
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("<tr").Append(" class='").Append(css).Append("'>");
            for (int k = 0; k < items.Rows.Count + 1; k++)
            {
                int rg = 0, rgs = 0;
                if (k == 0)
                {
                    sb.Append("<td>").Append(samkind.Rows[j][0].ToString()).Append("</td>");
                }
                else
                {
                    for (int re = 0; re < spins.Rows.Count; re++)
                    {

                        if (spins.Rows[re]["样品类别"].ToString() == samkind.Rows[j][0].ToString() && spins.Rows[re]["ii_name"].ToString() == items.Rows[k - 1][0].ToString())
                        {
                            sb.Append("<td>").Append(spins.Rows[re]["检测样品量"].ToString()).Append("</td>");
                            sb.Append("<td>").Append(spins.Rows[re]["检测费"].ToString()).Append("</td>");
                            sb.Append("<td>").Append(spins.Rows[re]["检测批量"].ToString()).Append("</td>");
                            rgs = 1;
                        }
                        if (spins.Rows[re]["样品类别"].ToString() == samkind.Rows[j][0].ToString() && spins.Rows[re]["ii_name"].ToString() != items.Rows[k - 1][0].ToString())
                        {
                            rg = 2;
                        }
                    }
                    if (rg == 2 && rgs != 1)
                    {
                        sb.Append("<td>").Append("/").Append("</td>");
                        sb.Append("<td>").Append("/").Append("</td>");
                        sb.Append("<td>").Append("/").Append("</td>");
                    }


                }

            }

            sb.Append("</tr>");
        }

    }
    /// <summary>
    /// 创建小计行
    /// </summary>
    /// <param name="sb"></param>
    /// <param name="samkind">样品类别</param>
    /// <param name="items">项目</param>
    /// <param name="spins">工作量</param>
    private void CreateTotal(ref StringBuilder sb, DataTable samkind, DataTable items, DataTable spins)
    {
        double Sumsample = 0.0;
        double SumCheck = 0.0;
        double SumP = 0.0;
        sb.Append("<tr>");
        for (int j = 0; j < items.Rows.Count; j++)
        {           
            if (j == 0)
            {
                sb.Append("<td>").Append("小计").Append("</td>");
            }
            for (int k = 0; k < samkind.Rows.Count; k++)
            {
                for (int re = 0; re < spins.Rows.Count; re++)
                {
                    if (spins.Rows[re]["样品类别"].ToString() == samkind.Rows[k][0].ToString() && spins.Rows[re]["ii_name"].ToString() == items.Rows[j][0].ToString())
                    {
                        Sumsample += Convert.ToDouble(spins.Rows[re]["检测样品量"]);
                        SumCheck += Convert.ToDouble(spins.Rows[re]["检测费"]);
                        SumP += Convert.ToDouble(spins.Rows[re]["检测批量"]);
                    }
                }
                if (k == samkind.Rows.Count - 1)
                {
                    sb.Append("<td>").Append(Sumsample).Append("</td>");
                    sb.Append("<td>").Append(SumCheck).Append("</td>");
                    sb.Append("<td>").Append(SumP).Append("</td>");
                    Sumsample = 0.0;
                    SumCheck = 0.0;
                    SumP = 0.0;
                }
            }
        }

        sb.Append("</tr>");
    }
    #endregion


}

思想就是用StringBuilder创建行列,赋给Literal 控件

控件中国网提供FastReport.Net 基本报表控件的下载及购买。

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat