Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Export to Excel

Export to Excel

Scheduled Pinned Locked Moved ASP.NET
wpfwcfdesignhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Horscht i
    wrote on last edited by
    #1

    Hello! Most of you are familiar with this piece of code which exports a GridView/DetailsView etc. to Excel: protected void Export2ExcelClick(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AppendHeader("content-disposition", "inline; filename=test.xls"); Response.ContentType = "application/ms-excel"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); GridView_EffortCostsOverview.RenderControl(htmlWrite); Response.End(); } I have bound the GridView to an SqlDataSource in .aspx file. I tried binding it dynamically in CodeBehing, but the problem remained the same: Not only the GridView is exported, the code exports the whole webform to an excel sheet, so the htmlWriter seems to be full with the hole webform... But the writer is first generated in this Export2ExcelClick function. It does not appear anywhere else. A quick solution will be highly appreciated!

    A 1 Reply Last reply
    0
    • H Horscht i

      Hello! Most of you are familiar with this piece of code which exports a GridView/DetailsView etc. to Excel: protected void Export2ExcelClick(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AppendHeader("content-disposition", "inline; filename=test.xls"); Response.ContentType = "application/ms-excel"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); GridView_EffortCostsOverview.RenderControl(htmlWrite); Response.End(); } I have bound the GridView to an SqlDataSource in .aspx file. I tried binding it dynamically in CodeBehing, but the problem remained the same: Not only the GridView is exported, the code exports the whole webform to an excel sheet, so the htmlWriter seems to be full with the hole webform... But the writer is first generated in this Export2ExcelClick function. It does not appear anywhere else. A quick solution will be highly appreciated!

      A Offline
      A Offline
      albCode
      wrote on last edited by
      #2

      check out this http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx http://www.gridviewguy.com/ArticleDetails.aspx?articleID=86 _____________________ Proud to be Albanian _____________________

      H 1 Reply Last reply
      0
      • A albCode

        check out this http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx http://www.gridviewguy.com/ArticleDetails.aspx?articleID=86 _____________________ Proud to be Albanian _____________________

        H Offline
        H Offline
        Horscht i
        wrote on last edited by
        #3

        Those are the articles I have the above code snippet from... so I tried something else and it works. Thanks for your help!

        A 1 Reply Last reply
        0
        • H Horscht i

          Those are the articles I have the above code snippet from... so I tried something else and it works. Thanks for your help!

          A Offline
          A Offline
          albCode
          wrote on last edited by
          #4

          Never mind you are welcome... _____________________ Proud to be Albanian _____________________

          H 1 Reply Last reply
          0
          • A albCode

            Never mind you are welcome... _____________________ Proud to be Albanian _____________________

            H Offline
            H Offline
            Horscht i
            wrote on last edited by
            #5

            I post my code, if anyone has also problems with Excel export: string FileName = "";// Filename for opening and saving object ReadOnly = false; object IsVisible = true; object ExcelSheet; object FileFilter = "Microsoft Excel (*.xls), *.xls"; // Use for parameters you do not want do declare or do not know :-) object missing = System.Reflection.Missing.Value; // Params for AutoFormat object format_true = true; object format_false = false; Excel.ApplicationClass ExcelApp = new Excel.ApplicationClass(); try { // Open a new workbook Excel.Workbook ExcelBook = ExcelApp.Workbooks.Add(missing); ExcelSheet = ExcelBook.Worksheets.get_Item(1); ExcelBook.Activate(); int ExcelRow = 4; int ExcelCol = 1; //Write column names while (ExcelCol < GridView_Name.Columns.Count - 1) { ExcelApp.Cells[ExcelRow, ExcelCol] = GridView_Name.Columns[ExcelCol].ToString(); ExcelCol++; } // Write data ExcelRow = 5; ExcelCol = 1; int row = 0; while (row < GridView_Name.Rows.Count) { while (ExcelCol < GridView_Name.Columns.Count - 2) { ExcelApp.Cells[ExcelRow + row, ExcelCol] = GridView_Name.Rows[row].Cells[ExcelCol].Text.ToString().Trim(); ExcelCol++; } row++; ExcelCol = 1; } // switch statement which selects the right functions of Excel according to the installed version of Excel on the client computer. // Only AutoFormat, SaveAs and Quit functions are used, so this switch statement is at the end of this Export2Excel function. switch (ExcelApp.Version.ToString()) { case "9.0": // Format workbook using Excel AutoFormats ExcelApp.get_Range("A1", missing).AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatList3, format_true, format_true, format_false, format_true, format_true, format_false); ExcelApp.get_Range("A4", missing).AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormatList1, format_true, format_true, format_false, format_true, format_true, format_false);

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups