Export to a Datagrid, YET AGAIN!

Discussion in 'ASP .Net' started by Kevin Blakeley, Jun 25, 2003.

  1. 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?
     
    Kevin Blakeley, Jun 25, 2003
    #1
    1. Advertising

  2. Kevin Blakeley

    George Durzi Guest

    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" <> wrote in message
    news:OCm%...
    > 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?
    >
    >
     
    George Durzi, Jun 25, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Myers

    ASP.Net Worker Process Drops Out Yet Again!

    Richard Myers, Dec 2, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    1,420
    Richard Myers
    Dec 3, 2004
  2. Bob

    Font Sizes (yet again)

    Bob, Dec 28, 2003, in forum: HTML
    Replies:
    36
    Views:
    1,251
    Scott_From_PA
    Feb 11, 2005
  3. Christopher Benson-Manica

    streambuf, yet again

    Christopher Benson-Manica, Feb 26, 2004, in forum: C++
    Replies:
    3
    Views:
    347
    John Harrison
    Feb 26, 2004
  4. Steven T. Hatton

    Header Files and Interfaces Yet Again

    Steven T. Hatton, Jun 9, 2004, in forum: C++
    Replies:
    11
    Views:
    506
    Kai-Uwe Bux
    Jun 16, 2004
  5. Berehem
    Replies:
    4
    Views:
    580
    Lawrence Kirby
    Apr 28, 2005
Loading...

Share This Page