arkiboys:Hi,
In asp.net 3.5, how is it possible to
1)Export the data of a datatable into .csv file
2)Save the spreadsheet on the network as c:\temp\
Thanks
Hope this helps
here dtSLA is the datatable and populategrid is the mothod to populate the datatable and sbErr is a string builder
protected void ibExport_Click(object sender, ImageClickEventArgs e)
{
dtSLA = PopulateGrid();
DataView dv = dtSLA.DefaultView;
dv.Sort = "Inserted Date DESC";
iCnt = dv.Count;
Response.AddHeader("content-disposition", "attachment;filename=rSLAReport.xls;");
Response.ContentType = "application/x-msexcel";
if (ddlExport.SelectedValue == "Excel")
Response.Write(Export(dv, 0));
else
Response.Write(Export(dv, 1));
ddlExport.SelectedIndex = 0;
}
private string Export(DataView dv, int x)
{
StringBuilder sbErr = new StringBuilder("<HTML><BODY>");
sbErr.Append("<table border = 1>");
sbErr.Append("<tr><td colspan=5><b>SiteID = </b>"); sbErr.Append(txtSiteID.Text); sbErr.Append("</td>");
sbErr.Append("<tr><td colspan=5><b>Start Date = </b>"); sbErr.Append(txtSDate.Text);
sbErr.Append("<tr><td colspan=5><b>End Date = </b>"); sbErr.Append(txtEDate.Text); sbErr.Append("</td></tr>");
sbErr.Append("<tr><td colspan=5><b>Expressed in DAYS</b></td></tr>");
sbErr.Append("<tr><td colspan=5>Website Provisioning Time: It is the difference between 'In-Build Date' and 'Implemented Date'</td></tr>");
sbErr.Append("<tr><td colspan=5>Scan Provisioning Time: It is the difference between 'Scan Request Date' and 'Scan Start Date'</td></tr>");
sbErr.Append("<tr><td colspan=5>Scan Report Time: It is the difference between 'Scan Completion Date' and 'Scan Report Date'</td></tr>");
sbErr.Append("<tr><td colspan=5 rowspan=1> </td></tr>");
sbErr.Append("</table>");
sbErr.Append("<table border = 1><tr>");
sbErr.Append("<td><B>URL</B></td>");
sbErr.Append("<td> <B>Web Provisioning Time</B></td>");
sbErr.Append("<td> <B>Scan Provisioning Time</B></td>");
sbErr.Append("<td> <B>Scan Report Time</B></td>");
sbErr.Append("<td><B>Site Go Live Date</B></td>");
sbErr.Append("</tr>");
if (iCnt > 0)
{
for (int i = 0; i < iCnt; i++)
{
if (x == 0)
{
sbErr.Append("<tr>");
sbErr.Append("<td nowrap> ");
sbErr.Append(dv[i].Row["URL"].ToString() + "</td>");
sbErr.Append("<td>" + dv[i].Row["WbProvTime"].ToString() + "</td>");
sbErr.Append("<td>" + dv[i].Row["ScanProvTime"].ToString() + "</td>");
sbErr.Append("<td>" + dv[i].Row["ScanReportTime"].ToString() + "</td>");
sbErr.Append("<td>" + dv[i].Row["Inserted Date"].ToString() + "</td>");
sbErr.Append("</tr>");
}
else
{
sbErr.Append("<tr>");
sbErr.Append("<td colspan=4 nowrap> ");
sbErr.Append(dv[i].Row["URL"].ToString());
sbErr.Append("; " + dv[i].Row["WbProvTime"].ToString());
sbErr.Append("; " + dv[i].Row["ScanProvTime"].ToString());
sbErr.Append("; " + dv[i].Row["ScanReportTime"].ToString());
sbErr.Append("; " + dv[i].Row["Inserted Date"].ToString());
sbErr.Append("</tr>");
}
}
}
sbErr.Append("</table></body></html>");
return sbErr.ToString();
} My example works for both excel and csv.
Saving a file depends on the browser settings than dotnet code
Once you click export, you can either open or save, depends on the browser settings it saves (default temp folder)