Export dataset to Excel
-
Hi, When I tried to implement the code from this forum to display data in the excel from a webpage, I am getting weird results. I am wondering if you could direct me to correct the problem? I thank you for your help. The code is : HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Charset = ""; //MAKE THE CONTENT TYPE TO EXCEL FORMAT. response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + ".xls\""); // CREATE A STRING WRITER. using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // INSTANTIATE A DATAGRID. GridView dg = new GridView(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); //STYLE TO FORMAT NUMBERS TO STRING. string style1 = @" .textmode { mso-number-format:\@; } "; response.Write(sw.ToString()); response.Write(style1); //response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } } } catch (Exception ex) { throw ex; } } ds:is the dataset and filename is the name of the filename. However, the file contains data as below: " " MonthYearUSERIDApplicationNumOfReturns" " 08/2011userid12010" " 08/2011userid22010" " 08/2011userid32009" " 08/2011userid42010" " " .textmode { mso-number-format:\@; } " Untitled Page"
I am wondering what wrong with my code. Any help is greatly appreciated. Thank you.
-
Hi, When I tried to implement the code from this forum to display data in the excel from a webpage, I am getting weird results. I am wondering if you could direct me to correct the problem? I thank you for your help. The code is : HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Charset = ""; //MAKE THE CONTENT TYPE TO EXCEL FORMAT. response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + ".xls\""); // CREATE A STRING WRITER. using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // INSTANTIATE A DATAGRID. GridView dg = new GridView(); dg.DataSource = ds.Tables[0]; dg.DataBind(); dg.RenderControl(htw); //STYLE TO FORMAT NUMBERS TO STRING. string style1 = @" .textmode { mso-number-format:\@; } "; response.Write(sw.ToString()); response.Write(style1); //response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } } } catch (Exception ex) { throw ex; } } ds:is the dataset and filename is the name of the filename. However, the file contains data as below: " " MonthYearUSERIDApplicationNumOfReturns" " 08/2011userid12010" " 08/2011userid22010" " 08/2011userid32009" " 08/2011userid42010" " " .textmode { mso-number-format:\@; } " Untitled Page"
I am wondering what wrong with my code. Any help is greatly appreciated. Thank you.
This dll can help you with good designing Export to Excel
-
This dll can help you with good designing Export to Excel