Export to a Datagrid, YET AGAIN!

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Export to a Datagrid, YET AGAIN!

    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 Guest

  2. Similar Questions and Discussions

    1. Export Datagrid to PDF
      Okay, I have code here to export a datagrid to Excel. Can I modify it to export it to a PDF? Thanks, Function ExcelExport(ByVal DatagridID,...
    2. Export Datagrid to excel
      Hi, I have written the code to export my datagrid to excel. it is working fine. But i have one issue, when ever i click on export button, it...
    3. Export DataGrid
      Hi, Is there anyway to export the data in a DataGrid to excel file? Derek
    4. Display a Datagrid & Export a Datagrid to Excel
      I have a page displaying a datagrid and the user wants to have two options: 1. Export the current "Displayed" datagrid to Excel 2. Export another...
    5. DataGrid export to excel
      I've asked this question once before, but am still having problems. When I try to export this grid to excel i get the error message i've seen...
  3. #2

    Default Re: Export to a Datagrid, YET AGAIN!

    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" <MSNews@whoiskb.com> wrote in message
    news:OCm%23uK1ODHA.2228@tk2msftngp13.phx.gbl...
    > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139