G
Guest
Hi
I've created a generic 'Report' class that takes a DataView from a DataGrid control and writes it out to the response as a csv file for download. Basically, when a user clicks the download button on the page, this class is instantiated and the download method called. The odd thing is you get the Save/Open dialog popup twice!! Any ideas why? The code look like
public void DownloadDataView(DataView view, Page page
string fileName = view.Table.TableName.ToString()
if (fileName == ""
fileName = "report"
page.Response.BufferOutput = true
page.Response.Clear()
page.Response.ClearContent()
page.Response.ClearHeaders()
page.Response.ContentType = "application/octet-stream"
page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".csv\"")
page.Response.Flush()
//write out column heading
int i = view.Table.Columns.Count
for(int j = 0; j<i; j++
if (j == i -1
page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString()
Replace("\"", "'") + "\"\n")
els
page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString(
.Replace("\"", "'") + "\",")
page.Response.Flush()
//write out actual dat
foreach(DataRow row in view.Table.Rows
for(int j = 0; j<i; j++
if (j == i -1
page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\"\n")
els
page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\",")
page.Response.End()
Thanks for your help
Dan
I've created a generic 'Report' class that takes a DataView from a DataGrid control and writes it out to the response as a csv file for download. Basically, when a user clicks the download button on the page, this class is instantiated and the download method called. The odd thing is you get the Save/Open dialog popup twice!! Any ideas why? The code look like
public void DownloadDataView(DataView view, Page page
string fileName = view.Table.TableName.ToString()
if (fileName == ""
fileName = "report"
page.Response.BufferOutput = true
page.Response.Clear()
page.Response.ClearContent()
page.Response.ClearHeaders()
page.Response.ContentType = "application/octet-stream"
page.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".csv\"")
page.Response.Flush()
//write out column heading
int i = view.Table.Columns.Count
for(int j = 0; j<i; j++
if (j == i -1
page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString()
Replace("\"", "'") + "\"\n")
els
page.Response.Write("\"" + view.Table.Columns[j].ColumnName.ToString(
.Replace("\"", "'") + "\",")
page.Response.Flush()
//write out actual dat
foreach(DataRow row in view.Table.Rows
for(int j = 0; j<i; j++
if (j == i -1
page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\"\n")
els
page.Response.Write("\"" + row[j].ToString().Replace("\"", "'") + "\",")
page.Response.End()
Thanks for your help
Dan