Click here to Skip to main content
15,943,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am exporting a gridview(dynamically created) data to excel.In my local machine it is downloaded.But if I drag the row to increase or drag the column and close the file it wants me to save as usual.I save the changes.Now, after a second a folder is being created automatically with html files and css. If I delete the folder the excel file can not be opened.It requires an html file which was in that folder.The folder is not created when I download the file.Only if I change any thing in that file the folder is created.

my code----------------------------------------------

private void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "Downloaded" + DateTime.Now.ToString("ddMMyyss") + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
dgGrid.ItemStyle.VerticalAlign = VerticalAlign.Top;
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ClearContent();
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;

string brstyle = @"<style>br { mso-data-placement:same-cell; }</style>"; //--as i have to create two lines in a cell--//
Response.Write(brstyle);
Response.Write(tw.ToString());
Response.Flush();
Response.End();
}
}
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900