Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Export to a Datagrid, YET AGAIN!

Reply
Thread Tools

Export to a Datagrid, YET AGAIN!

 
 
Kevin Blakeley
Guest
Posts: n/a
 
      06-25-2003
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?


 
Reply With Quote
 
 
 
 
George Durzi
Guest
Posts: n/a
 
      06-25-2003
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


"Kevin Blakeley" <(E-Mail Removed)> wrote in message
news:OCm%(E-Mail Removed)...
> 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?
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export to PDF with google maps + msacces export Maarten Porters Ruby 1 07-28-2008 01:45 PM
Any way to import or export a wireless setting? Al Keniston Wireless Networking 4 11-09-2005 11:38 AM
How to export/import Wireless settings on a domain? Frank Larsen Wireless Networking 0 08-27-2005 11:04 AM
Yet another book recommendation, but for someone who can program and yet does not the terminology well Berehem C Programming 4 04-28-2005 05:25 PM
Invalid export DLL or export format =?Utf-8?B?RGF2aWQgVmFsbGU=?= ASP .Net 0 10-29-2003 11:46 AM



Advertisments