Exporting a dashboard gridview to an excel spreadsheet
Hey guys,
I had to build a control for my client's back-end that displayed a list of registrations that were stored in a database. This all worked fine and they wanted to be able to export the data to an excel spreadsheet.
I managed to do all of this by calling myGridview.RenderControl(); to grab all the data from the gridview and shove it in a spreadsheet. However at first attempt it seemed that the .RenderControl() function took ALL the controls within the usercontrol (including the export button/headings etc), despite the fact I was only calling the function on the gridview.
Ultimately I managed to work around it by clearing all of the controls in the parent form and then just rendering the gridview data.
Anyway my question is basically why did I have to do it this way? Why does calling .RenderControl() on a single control seem to render all the controls in the parent form? Anyone happen to have an idea?
When you streamed down the spreadsheet to the user, did you call Response.End() afterwards? If you didn't, all of the page will get appended to the output stream and get stuck on the end of the document.
I'd not normally use they RenderControl() method to get the data, as it would give you the entire web control, and presumably you'd need to parse/transform the resulting HTML into CSV format? Normally I'd use the underlying data source, and have a function that turns that into the data. Here's a basic example function that turns a recordset into a CSV file and streams it to the browser:
protected void btnDownload_Click(object sender, EventArgs e)
{
//get the list of data from data access class
SqlDataReader dr = TestData.GetExportData();
//loop through and format
StringBuilder sb = new StringBuilder();
int maxFields = dr.FieldCount;
//build the csv string
//add headers
for (int x = 0; x < maxFields; x++)
{
sb.AppendFormat("\"{0}\"", dr.GetName(x));
if (x < maxFields - 1)
{
sb.Append(",");
}
else
{
sb.Append("\r\n");
}
}
System.Globalization.CultureInfo cultureInfo = System.Globalization.CultureInfo.InvariantCulture;
//add data
while (dr.Read())
{
for (int x = 0; x < maxFields; x++)
{
sb.AppendFormat("\"{0}\"", dr[x].ToString());
if (x < maxFields - 1)
{
sb.Append(",");
}
else
{
sb.Append("\r\n");
}
}
}
string exportData = sb.ToString();
dr.Close();
//send the csv to the user
string fileName = "attachment; filename=my_csv_file.csv";
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", fileName);
Response.Write(exportData);
Response.End();
}
Thanks for your response. I did eventually get it to render the gridview properly but I wasn't quite sure why my solution worked. Basically I had to include:
Tabpage tp = (TabPage)this.Parent; foreach (Control c in tp.Controls) { c.Controls.Clear(); }
This seemed to clear out the excess controls before I called GridView.RenderControl();
But in answer to your question, yes I did call Response.End(); which is why I'm confused. Also it wasn't my intention to export the data as a csv file. The client insisted on having the spreadsheet render the table so I had to do things a little differently.
Exporting a dashboard gridview to an excel spreadsheet
Hey guys,
I had to build a control for my client's back-end that displayed a list of registrations that were stored in a database. This all worked fine and they wanted to be able to export the data to an excel spreadsheet.
I managed to do all of this by calling myGridview.RenderControl(); to grab all the data from the gridview and shove it in a spreadsheet. However at first attempt it seemed that the .RenderControl() function took ALL the controls within the usercontrol (including the export button/headings etc), despite the fact I was only calling the function on the gridview.
Ultimately I managed to work around it by clearing all of the controls in the parent form and then just rendering the gridview data.
Anyway my question is basically why did I have to do it this way? Why does calling .RenderControl() on a single control seem to render all the controls in the parent form? Anyone happen to have an idea?
Thanks!
When you streamed down the spreadsheet to the user, did you call Response.End() afterwards? If you didn't, all of the page will get appended to the output stream and get stuck on the end of the document.
I'd not normally use they RenderControl() method to get the data, as it would give you the entire web control, and presumably you'd need to parse/transform the resulting HTML into CSV format? Normally I'd use the underlying data source, and have a function that turns that into the data. Here's a basic example function that turns a recordset into a CSV file and streams it to the browser:
Hi Tim,
Thanks for your response. I did eventually get it to render the gridview properly but I wasn't quite sure why my solution worked. Basically I had to include:
This seemed to clear out the excess controls before I called GridView.RenderControl();
But in answer to your question, yes I did call Response.End(); which is why I'm confused. Also it wasn't my intention to export the data as a csv file. The client insisted on having the spreadsheet render the table so I had to do things a little differently.
Anyway thanks again for the help :-)
is working on a reply...