ASP.NET - Trying to Export DataTable to Excel in C#, Framework 3.5, ASP.NET

Asked By SUNNY N on 09-Apr-11 07:41 AM
Trying to Export DataTable to Excel in C#, Framework 3.5, ASP  ..... things working fine on local but not on live ..... nothing happen on live when clicking on Button......while give the output on local...... please help.....


 protected void btnExportDataTableToExcel_Click(object sender, EventArgs e)
      { 
       /* Filling DataTable from Session as saved it beforehand... getting the required data here in " dt " */     
        DataTable dt = (DataTable)Session["TimeLogReportForexport"];

        if (dt.Rows.Count > 0)
          {
       
          DataTable table = new DataTable();
          table = dt;

          DataColumn columnForDate = new DataColumn();
          DataRow row;
         
          table.Columns.Remove("TimelogId");          
          table.Columns.Remove("TaskId");          
          table.Columns.Remove("TimeSpentMins");
          table.Columns.Remove("EmpId");
          table.Columns.Remove("Description");

          columnForDate.AutoIncrement = true;
          columnForDate = new DataColumn("Dated", typeof(System.String));
          table.Columns.Add(columnForDate);
          columnForDate.SetOrdinal(0);
          
          for (int i = 0; i < table.Rows.Count; i++)
          {
            DateTime da = Convert.ToDateTime(table.Rows[i]["Date"]);
            da = DateTimeUtils.UTCtoIndian(da);

            table.Rows[i]["Date"] = da.ToShortDateString();

            table.Rows[i]["Dated"] = da.ToString("dd/MM/yyyy");

          }

          table.AcceptChanges();
          table.Columns.Remove("Date");         
          table.TableName = "TimeLogReport";
         
          table.WriteXml(Server.MapPath("~") + @"\TimeLogReport.xls", XmlWriteMode.IgnoreSchema);

          Response.Redirect("TimeLogReport.xls");

     }
    }



Help Please !!
Thanks...
SUNNY
Nikhil Mahajan replied to SUNNY N on 09-Apr-11 08:43 AM
hey sunny ,
use below code to export datatable to Excel/CSV

http://www.codeproject.com/KB/office/exportxlscsv.aspx
Jitendra Faye replied to SUNNY N on 11-Apr-11 12:44 AM
For coverting DataTable data to excel it is must that first you have to add dll for ms execl in your application.
Follow this links-

http://zootfroot.blogspot.com/2010/03/convert-datatable-to-excel-xml-format.html
and
http://www.go4expert.com/forums/showthread.php?t=18260
and
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_26801543.html


I hope this will help you. Here you will get sample code also.

SUNNY N replied to Nikhil Mahajan on 30-Apr-11 09:59 PM
Thanks Nikhil Sir........................
these links is really awesome.....
and have also got one more way to handle this .... want too share this too....

the code I wrote started working on server too...... when I just redirected the follow to another page on Export_Click() event.... :-)

Thanks Again :)
SUNNY N replied to Jitendra Faye on 30-Apr-11 10:05 PM
Thanks Jitendra..
your links provided me the way I want to format the .xls file (cells)....