Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Yeah...M done with exporting grid view to excel...
Now what i am suppose to do is i need to save that excel workbook(sheet) in directory folder?
Posted
Updated 13-Sep-12 19:38pm
v2

private void ExportGridView() 
{    
System.IO.StringWriter sw = new System.IO.StringWriter();     System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);      // Render grid view control.    
 gvFiles.RenderControl(htw);
      // Write the rendered content to a file.   
  string renderedGridView = sw.ToString(); 
    File.WriteAllText(@"C:\Path\On\Server\ExportedFile.xlsx", renderedGridView); } 
 
Share this answer
 
Comments
cunny 14-Sep-12 2:03am    
Hello Richin..I have used the below code for exporting and saving the excel file to folder
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", header);
Response.Charset = "";
Response.ContentType = contentType;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

gvdownloaddocuments.AllowPaging = false;
bindata();
gvdownloaddocuments.HeaderRow.Style.Add("background-color", "#FFFFFF");
gvdownloaddocuments.HeaderRow.Cells[0].Visible = false;
for (int i = 0; i < gvdownloaddocuments.HeaderRow.Cells.Count; i++)
{
gvdownloaddocuments.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
gvdownloaddocuments.HeaderRow.Cells[i].Style.Add("color", "#FFFFFF");
}
gvdownloaddocuments.RenderControl(hw);
Response.Output.Write(sw.ToString());
string renderedGridView = sw.ToString();
File.WriteAllText(@"C:\Documents and Settings\user01\My Documents\Downloads\GridViewExport.xls", renderedGridView);

Response.End();
}
---------------
But the file is not getting saved in folder.? Where am i going wrong.?
 
Share this answer
 
Is this you want?
Workbook.SaveAs . This method you can use for saving the workbook.
Server.MapPath. This method gives you Virtual directory path.
 
Share this answer
 
v2
You tagged it as ASP.NET. Not sure on how you have exported the data as you have not shared much. Have a look here on how it is done (it saves the excel file too!):
Export DataTable to Excel in Asp.Net without using excel interop[^]
9 Solutions to Export Data to Excel for ASP.NET[^]
ASP.Net 2.0: Export GridView to Excel [^]
 
Share this answer
 

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