Hardik Patel

Hardik Patel

  • NA
  • 378
  • 469.5k

how to export data in excel 2007 in asp.net c#?

Sep 4 2013 6:46 AM
i am using below code but it's not working properly for MS Excel 2007..

DataTable dt = new DataTable();
            string str1;
            str1 = "SELECT Testt1.StudentCode, Student.StudentID,Student.Name, Testt1.FMark,'' as 'LMark','' as'Status',''as 'Promote' FROM Testt1 INNER JOIN School ON Testt1.SchoolCode = School.SchoolCode INNER JOIN Student ON Testt1.StudentCode = Student.StudentCode where Testt1.Promote ='Yes' and School.SchoolCode =" + DropDownList3.SelectedValue + "";
            dt = ObjConn.GetDataTable(str1);
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            //response.Charset = "";
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=Smarks.xls");
            //response.AddHeader("Content-Disposition", "C:\"" + fileName + "\"");
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            DataGrid dg = new DataGrid();
            dg.DataSource = dt;
            dg.DataBind();
            dg.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();

Answers (1)