Export to a Datagrid, YET AGAIN!

K

Kevin Blakeley

I know this was just posted but I did not want this message to get lost in
the other thread as it's slightly different.

Yes I want to export my dataset to excel for my clients, but I don't want it
to show in the browser. Everything I have tried makes it so that IE will
load excel within the browser session and view the file in there. What I
want is for the user to be prompted with the save dialog to save the excel
file disk. How do I do this?

Real simple code:
Response.ContentType = "application/vnd.ms-excel" ;
Response.Redirect("ITErrorLog.csv",true);

Any ideas?
 
G

George Durzi

I know you asked that you didn't want the Excel to be displayed in the
browser, but you can just cut that out of this code. This code exports to an
Excel file, displays it in the browser, then deletes the Excel file.

The trick here is that you're using Xsl to transform a DataSet into HTML,
then saving it with a .Xls extension. This code obviously won't compile
since I'm referencing a bunch of stuff and values in web.config, but it
should give you the general idea

// get back the search results
DataSet dsSearch = ExecuteSearchReturnDataSet();
// write them out to disk
string exportPath = Facade.Export.ExportDataSet(
dsSearch,
Session["UserLogin"].ToString(),
ConfigurationSettings.AppSettings["ReportPath"].ToString(),
ConfigurationSettings.AppSettings["ApplicationPath"].ToString() +
@"xsl\greenbook_search.xsl",
Convert.ToInt32(cUIHelper.ExportFormat.ExportXls).ToString());

// Write the Report to the Browser
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.WriteFile(exportPath);
Response.Flush();
Response.Close();

System.IO.File.Delete(exportPath);

#region ExportDataSet UserName, FilePath, XslPath, ExportFormatChoice
public static string ExportDataSet(
DataSet SearchResults,
string UserName,
string FilePath,
string XslPath,
string ExportFormatChoice)
{
string exportPath = "";

switch ((ExportFormat)Convert.ToInt32(ExportFormatChoice))
{
case (ExportFormat.ExportXls):
exportPath = FilePath + UserName + "_SearchResults.xls";
break;
case (ExportFormat.ExportHtml):
exportPath = FilePath + UserName + "_SearchResults.html";
break;
}

// Create a FileStream to write with
System.IO.FileStream fs = new System.IO.FileStream(
exportPath, System.IO.FileMode.Create);
// Create an XmlTextWriter for the FileStream
System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(
fs, System.Text.Encoding.Unicode);
try
{
XmlDataDocument xmlDoc = new XmlDataDocument(SearchResults);
System.Xml.Xsl.XslTransform xslTran = new System.Xml.Xsl.XslTransform();
xslTran.Load(XslPath);
xslTran.Transform(xmlDoc, null, xtw);
xtw.Close();
return exportPath;
}
catch (Exception ex)
{
xtw.Close();
System.IO.File.Delete(exportPath);
ExceptionManager.Publish(ex);
throw(ex);
}
}
#endregion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top