动态生成的asp.net表格,其中包含excel中的数据

本文关键字:数据 包含 excel 表格 asp net 动态 | 更新日期: 2023-09-27 18:26:30

我一直在做一个项目,我已经创建了一个包含行和列的动态asp.net表,现在我想将数据导出到excel,如果我们有Gridview,这很容易完成,但我必须使用动态表。我会非常感谢,因为我已经在谷歌上搜索了很多。关于这件事,我没有得到任何答复。

请帮助

 <asp:Table ID="tbl" runat="server" CellPadding="0" BackColor="Wheat" GridLines="Both">
    </asp:Table>

    protected void btn_submit(object sender, EventArgs e)
{
   // int totalcount = 0;
    DateTime datefrom = DateTime.Parse(txt_selectdatefrom.Text);
    string finaldatefrom = datefrom.ToString("yyyy-MM-dd");
    DateTime dateto = DateTime.Parse(txt_selectdateto.Text);
    string finaldateto = dateto.ToString("yyyy-MM-dd");
    TimeSpan t = (DateTime.Parse(finaldateto) - DateTime.Parse(finaldatefrom));
    double noofdays = t.TotalDays;
    sql = "select studentname,studentcode from tblstudent where classcode='" + drp_class.SelectedItem.Value + "'";
    ds = obj.openDataset(sql);
    TableRow tr;
    for (int i = 0; i < ds.Tables[0].Rows.Count+1; i++)
    {
        DateTime dt = datefrom;
        tr = new TableRow();
        TableCell tc;
        TableCell tc1;
        int totalabsent = 0;
        int totalpresent = 0;
        for (int j = 0; j < noofdays+5; j++)
        {
            tc = new TableCell();
            tc.Width = 300;
            tc1 = new TableCell();
            if (j == (noofdays + 2) && i!= 0)
            {
                int totalclasses = totalabsent + totalpresent;
                tc.Text =  totalclasses.ToString();
                tc.CssClass = "studentnameattendancetextcenter";
            }
           else if (j == (noofdays + 3) && i!= 0)
            {
                tc.Text =  totalpresent.ToString();
                tc.CssClass = "studentnameattendancetextcenter";
            }
            else if (j == (noofdays + 4) && i != 0)
            {
                tc.Text =  totalabsent.ToString();
                tc.CssClass = "studentnameattendancetextcenter";
            }
            else if (i == 0 && j == (noofdays + 2))
            {
                tc.Text = "Total_Classes";
                tc.Width = 200;
                System.Drawing.Color mycolorback = System.Drawing.ColorTranslator.FromHtml("#450a2a");
                tc.BackColor = mycolorback;
                tc.CssClass = "studentnameattendancetext";
            }
            else if (i == 0 && j == (noofdays + 3))
            {
                System.Drawing.Color mycolorback = System.Drawing.ColorTranslator.FromHtml("#450a2a");
                tc.BackColor = mycolorback;
                tc.CssClass = "studentnameattendancetext";
                tc.Text = " Present";
            }
            else if (i == 0 && j == (noofdays + 4))
            {
                System.Drawing.Color mycolorback = System.Drawing.ColorTranslator.FromHtml("#450a2a");
                tc.BackColor = mycolorback;
                tc.CssClass = "studentnameattendancetext";
                tc.Text = " Absent";
            }
           if (i == 0 && j == 0)
            {
                tc.Text = "Student Name";
                tc.Width=400;
                System.Drawing.Color mycolorback = System.Drawing.ColorTranslator.FromHtml("#450a2a");
                tc.BackColor = mycolorback;
                tc.CssClass = "studentnameattendance";
            }

           else if (i == 0 && j != 0 && j != noofdays + 2 && j != noofdays + 3 && j != noofdays + 4)
           {
               if (j == 1)
               {
                   tc.Text = datefrom.ToString("dd/MM/yyyy");
                   tc.ID = j.ToString();
                   System.Drawing.Color mycolorback = System.Drawing.ColorTranslator.FromHtml("#450a2a");
                   tc.BackColor = mycolorback;
                   tc.CssClass = "studentnameattendance";
               }
               else
               {
                   dt = dt.AddDays(1);
                   tc.Text = dt.ToString("dd/MM/yyyy");
                   tc.ID = j.ToString();
                   System.Drawing.Color mycolorback = System.Drawing.ColorTranslator.FromHtml("#450a2a");
                   tc.BackColor = mycolorback;
                   tc.CssClass = "studentnameattendance";
               }

           }
           else if (j == 0 && i != 0)
           {
               tc.Text = ds.Tables[0].Rows[i - 1]["studentname"].ToString();
               tc.Width = 300;

           }
           else if (j != 0 && i != 0 && j != noofdays + 2 && j != noofdays + 3 && j != noofdays + 4)
           {
               TableCell tcell = (TableCell)tbl.FindControl(j.ToString());
               string hh = DateTime.Parse(tcell.Text).ToString("yyyy-MM-dd");
               string sqlCheck = "select status from tblattendance where studentcode='" + ds.Tables[0].Rows[i - 1]["studentcode"].ToString() + "' and Attdate='" + hh + "' ";
               DataSet dsCheck = new DataSet();
               dsCheck = obj.openDataset(sqlCheck);
               if (dsCheck.Tables[0].Rows.Count == 0)
               {
                   tc.Text = "-";
                   tc.CssClass = "textcenterattendance";
               }
               else
               {
                   if (dsCheck.Tables[0].Rows[0]["status"].Equals("/"))
                   {
                       tc.Text = "P";
                       System.Drawing.Color mycolor = System.Drawing.ColorTranslator.FromHtml("#175b02");
                       tc.ForeColor = mycolor;
                       totalpresent = totalpresent + 1;
                       tc.Height = 30;
                       tc.Width = 10;
                       tc.CssClass = "textcenterattendance";
                   }
                   else if (dsCheck.Tables[0].Rows[0]["status"].Equals("N"))
                   {
                       tc.Text = "A";
                       System.Drawing.Color mycolor = System.Drawing.ColorTranslator.FromHtml("#fa0707");
                       tc.ForeColor = mycolor;
                       totalabsent = totalabsent + 1;
                       tc.Height = 30;
                       tc.Width = 10;
                       tc.CssClass = "textcenterattendance";
                   }
                   else
                   {
                       tc.Text = dsCheck.Tables[0].Rows[0]["status"].ToString();
                       tc.CssClass = "textcenterattendance";
                   }
               }

           }
            tr.Cells.Add(tc);
            }
        tbl.Rows.Add(tr);
        }

    }

我想把这个动态生成的asp表数据导出到excel。。。请帮我解决

动态生成的asp.net表格,其中包含excel中的数据

将数据添加到数据表,然后输出到excel

http://www.codeproject.com/Tips/406704/Export-DataTable-to-Excel-with-Formatting-in-Cshar