我想为多名员工生成excel报告

时间:2012-02-06 12:14:59

标签: c# asp.net dataset report

我想为月份生成员工报告。但我不能这样做。我只是试着这样做。但它只返回最后一个员工记录。

使用列表框并选择多个员工生成报告。但我不能这样做。

我的部分鳕鱼在这里..

DataSet4TableAdapters.sp_getalltimesheetTableAdapter TA1 = new DataSet4TableAdapters.sp_getalltimesheetTableAdapter();
DataSet4.sp_getalltimesheetDataTable DS1 = TA1.GetData();
foreach (DataRow dr in DS1.Rows)
{
    string name = Convert.ToString(dr["Employee_ID"]) + "-" + Convert.ToString(dr["Employee_Name"]);
    for (int i = 0; i < lbxempname.Items.Count; i++ )
    {
        if (lbxempname.Items[i].Selected)
        {
            string id = lbxempname.Items[i].Text;
            if (name == id)
            {
                string expression = "Employee_Id='" + name.Substring(0,8) + "'and punchDate >=#" + txtstartdate.Text + "# and PunchDate <=#" + txtenddate.Text + "# ";
                DataSet4TableAdapters.sp_getalltimesheetTableAdapter TA = new DataSet4TableAdapters.sp_getalltimesheetTableAdapter();
                DataSet4.sp_getalltimesheetDataTable DS = TA.GetData();
                if (DS.Rows.Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('No records found');</script>", false);
                }
                else
                {
                    DataView dataview = DS1.DefaultView;
                    dataview.RowFilter = expression;
                    Session["TaskTable"] = dataview;
                    grdvw = new GridView();
                    grdvw.DataSource = Session["TaskTable"];
                    grdvw.DataBind();
                    dt = new DataTable();
                    dt.Columns.Add("EmployeeId");
                    dt.Columns.Add("EmployeeName");
                    dt.Columns.Add("Department");
                    dt.Columns.Add("Date");
                    dt.Columns.Add("Day");
                    dt.Columns.Add("Intime");
                    dt.Columns.Add("Outtime");
                    dt.Columns.Add("Late_After");
                    dt.Columns.Add("Early_Before");
                    dt.Columns.Add("Remarks");
                    foreach (GridViewRow oItem in grdvw.Rows)
                    {
                        DataRow dr1 = dt.NewRow();
                        string empid = oItem.Cells[0].Text;
                        string empname = oItem.Cells[1].Text;
                        string dept = oItem.Cells[2].Text;
                        string PunchDate = oItem.Cells[3].Text;
                        string Day = oItem.Cells[4].Text;
                        string intime = oItem.Cells[5].Text;
                        string outtime = oItem.Cells[6].Text;

                        groupdetails(empid, empname, PunchDate, dept, intime, outtime);
                        if (intime == "" || outtime == "")
                        {
                            remark = "A";
                        }
                        else
                        {
                            remark = "P";
                        }
                        dr1["EmployeeId"] = Convert.ToString(empid);
                        dr1["EmployeeName"] = Convert.ToString(empname);
                        dr1["Department"] = Convert.ToString(dept);
                        dr1["Date"] = Convert.ToString(PunchDate);
                        dr1["Day"] = Convert.ToString(Day);
                        dr1["Intime"] = Convert.ToString(intime);
                        dr1["Outtime"] = Convert.ToString(outtime);
                        dr1["Late_After"] = Convert.ToString(Session["late"].ToString());
                        dr1["Early_Before"] = Convert.ToString(Session["early"].ToString());
                        dr1["Remarks"] = Convert.ToString(remark);
                        dt.Rows.Add(dr1);
                        ViewState["new"] = dt;

                    }
                }
            }

        }

    }
}
grdvw.DataSource = ViewState["new"];
grdvw.DataBind();
System.IO.StringWriter stringwriter = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
grdvw.RenderControl(htmlwriter);
Response.Write(stringwriter.ToString());
Response.End();

它返回上一个选定员工的输出。但我想从列表框中选择所有员工记录 请帮我... 我的输出是这样的......

   EmployeeId   EmployeeName    Department  Date            Day     Intime      Outtime     Late_After  Early_Before    Remarks
   00626243         HariharanJ  SBS IT      01-04-2012  Wednesday   06:40:00    15:00:00    00:00        00:00          P
   00626243         HariharanJ  SBS IT      01-03-2012  Tuesday     00:00:00    14:40:00    00:00        00:05          P
   00626243         HariharanJ  SBS IT      01-05-2012  Thursday    07:00:00    14:35:00    00:20        00:10          P

以上报告是列表框最后选择的员工。我想为所有员工提供这种报告,并且有一点小小的突破......

2 个答案:

答案 0 :(得分:1)

我做过同样的事情,请查看此链接 描述了整个代码和场景,我认为它会对你有所帮助

Export Gridview in Excel

答案 1 :(得分:0)

我建议看看NPOI,一个Apache POI的.NET移植;此库允许您从API生成MS Office文件,而不需要任何其他依赖项,例如前端对象,MS Office安装等。)