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
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Export to Excel

Export to Excel

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpcsscomquestion
11 Posts 4 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.
  • L Offline
    L Offline
    leppie
    wrote on last edited by
    #1

    public FileContentResult ExportToExcel(object datasource)
    {
    var grid = new GridView();
    grid.DataSource = datasource;
    grid.DataBind();

      var sw = new StringWriter();
      var htw = new HtmlTextWriter(sw);
    
      grid.RenderControl(htw);
    
      byte\[\] excelFileBytesContent = this.Response.ContentEncoding.GetBytes(sw.ToString());
      var excelFileContentResult = new FileContentResult(excelFileBytesContent, "application/vnd.ms-excel");
    
      return excelFileContentResult;
    

    }

    It kinda works, but WTF??? :doh:

    IronScheme
    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

    G B 2 Replies Last reply
    0
    • L leppie

      public FileContentResult ExportToExcel(object datasource)
      {
      var grid = new GridView();
      grid.DataSource = datasource;
      grid.DataBind();

        var sw = new StringWriter();
        var htw = new HtmlTextWriter(sw);
      
        grid.RenderControl(htw);
      
        byte\[\] excelFileBytesContent = this.Response.ContentEncoding.GetBytes(sw.ToString());
        var excelFileContentResult = new FileContentResult(excelFileBytesContent, "application/vnd.ms-excel");
      
        return excelFileContentResult;
      

      }

      It kinda works, but WTF??? :doh:

      IronScheme
      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

      G Offline
      G Offline
      GibbleCH
      wrote on last edited by
      #2

      Excel can load Html tables no problem. There are some CSS issues that arise from it. But it really does work quite well. And while it doesn't appear that in this case you're going to have any styles being applied, if you did apply formatting to the GridView, they would be reflected in the excel document.

      L 1 Reply Last reply
      0
      • G GibbleCH

        Excel can load Html tables no problem. There are some CSS issues that arise from it. But it really does work quite well. And while it doesn't appear that in this case you're going to have any styles being applied, if you did apply formatting to the GridView, they would be reflected in the excel document.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        GibbleCH wrote:

        Excel can load Html tables no problem.

        I agree, but for a business requirement? :doh:

        IronScheme
        ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

        G 1 Reply Last reply
        0
        • L leppie

          GibbleCH wrote:

          Excel can load Html tables no problem.

          I agree, but for a business requirement? :doh:

          IronScheme
          ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

          G Offline
          G Offline
          GibbleCH
          wrote on last edited by
          #4

          I'm actually using a similar technique in a recent app we have written. However, the HTML passed to excel comes from the browser. This allows us to take the data, render HTML, the user then has the ability to hide/show columns, sort, filter, etc, then export the result to excel. If I don't pass the HTML to excel (well, a per-processor that cleans it up), then I have to look at it, determine what they are currently showing, query the db all over, then try to create an excel document that mimics the displayed data WITH all the current formatting, which is a duplication of effort since all that work has already been done in rendering the HTML. It also allows us to just modify the rendering of the HTML, and the export to excel is almost always working automatically without also having to modify the code that generates the excel document.

          L 1 Reply Last reply
          0
          • G GibbleCH

            I'm actually using a similar technique in a recent app we have written. However, the HTML passed to excel comes from the browser. This allows us to take the data, render HTML, the user then has the ability to hide/show columns, sort, filter, etc, then export the result to excel. If I don't pass the HTML to excel (well, a per-processor that cleans it up), then I have to look at it, determine what they are currently showing, query the db all over, then try to create an excel document that mimics the displayed data WITH all the current formatting, which is a duplication of effort since all that work has already been done in rendering the HTML. It also allows us to just modify the rendering of the HTML, and the export to excel is almost always working automatically without also having to modify the code that generates the excel document.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            It is all good an well till you get to text that looks like numeric data to excel. Eg: a telephone number with a leading 0. You end up with a number without the leading zero. Or a long number (say ID or SS nr), displays in exponential form.

            IronScheme
            ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

            S G 2 Replies Last reply
            0
            • L leppie

              It is all good an well till you get to text that looks like numeric data to excel. Eg: a telephone number with a leading 0. You end up with a number without the leading zero. Or a long number (say ID or SS nr), displays in exponential form.

              IronScheme
              ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

              S Offline
              S Offline
              Sundance Kid
              wrote on last edited by
              #6

              Yea.. had the problem with the 0 in the excel part too. But we used it too, because the calc method had a lot of logic to output HTML, so we reused it for the excel outtput. Worked like a bomb (but the 0 was problematic)

              1 Reply Last reply
              0
              • L leppie

                public FileContentResult ExportToExcel(object datasource)
                {
                var grid = new GridView();
                grid.DataSource = datasource;
                grid.DataBind();

                  var sw = new StringWriter();
                  var htw = new HtmlTextWriter(sw);
                
                  grid.RenderControl(htw);
                
                  byte\[\] excelFileBytesContent = this.Response.ContentEncoding.GetBytes(sw.ToString());
                  var excelFileContentResult = new FileContentResult(excelFileBytesContent, "application/vnd.ms-excel");
                
                  return excelFileContentResult;
                

                }

                It kinda works, but WTF??? :doh:

                IronScheme
                ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #7

                That's actually relatively neat. I can't think of a more concise way of getting data into an Excel readable format without using interop or a library like Aspose which costs money (and sticking it on the clipboard and reading it back doesn't count, that has a big nasty side-effect).

                L 1 Reply Last reply
                0
                • B BobJanova

                  That's actually relatively neat. I can't think of a more concise way of getting data into an Excel readable format without using interop or a library like Aspose which costs money (and sticking it on the clipboard and reading it back doesn't count, that has a big nasty side-effect).

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  Not really. Use a free one like A Very Easy to Use Excel XML Import-Export Library[^] And have happiness with text that look like numbers :)

                  IronScheme
                  ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                  B 1 Reply Last reply
                  0
                  • L leppie

                    Not really. Use a free one like A Very Easy to Use Excel XML Import-Export Library[^] And have happiness with text that look like numbers :)

                    IronScheme
                    ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                    B Offline
                    B Offline
                    BobJanova
                    wrote on last edited by
                    #9

                    Well, using a whole library just for that is kind of overkill, still. That's a really useful looking library, though.

                    1 Reply Last reply
                    0
                    • L leppie

                      It is all good an well till you get to text that looks like numeric data to excel. Eg: a telephone number with a leading 0. You end up with a number without the leading zero. Or a long number (say ID or SS nr), displays in exponential form.

                      IronScheme
                      ((λ (x) `(,x ',x)) '(λ (x) `(,x ',x)))

                      G Offline
                      G Offline
                      GibbleCH
                      wrote on last edited by
                      #10

                      I generate my cells with classnames, then run the data through a filter, and setup proper mso-number-format styles on the cells prior to sending it to excel. It works well

                      S 1 Reply Last reply
                      0
                      • G GibbleCH

                        I generate my cells with classnames, then run the data through a filter, and setup proper mso-number-format styles on the cells prior to sending it to excel. It works well

                        S Offline
                        S Offline
                        Sundance Kid
                        wrote on last edited by
                        #11

                        Thanks for the tip. Yea.. that project was done in record time about a year ago. But will keep it in mind!

                        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