0

I have been working on a project and I have created a dynamic asp.net table with rows and column, now I want to export that data to excel which is very easily done if we do have Gridview but I have to do it with dynamic table. I will be really thankful as I have searched on google a lot. I have not got any answer regarding this.

Please Help

 <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);

        }


    }

I want to export this dynamic generated asp table data to excel...please help me out

1

1 Answer 1

0

Add your data to a datatable then output to excel

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

Sign up to request clarification or add additional context in comments.

1 Comment

i have added the code inn explanation above ...please help me out if possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.