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. General Programming
  3. Visual Basic
  4. Exporting a Datagrid to Excel

Exporting a Datagrid to Excel

Scheduled Pinned Locked Moved Visual Basic
csharpvisual-studiobeta-testinghelpworkspace
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.
  • R Offline
    R Offline
    raysot777
    wrote on last edited by
    #1

    Hello, I am trying to export the contents of a datagrid to Excel. Here's the code I have so far: 'Fire up an Excel instance Dim ExcelApp As New Excel.Application ExcelApp.Visible = True Dim workbooks As Workbooks = ExcelApp.Workbooks Dim workbook As _Workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet) Dim sheets As Sheets = workbook.Worksheets Dim worksheet As _Worksheet = CType(sheets.Item(1), _Worksheet) 'Put some data on the Worksheet -- This doesn't work, but throws no errors Dim nCell As Integer = 5 Dim Range2 As Range = worksheet.Range("A1", "E1") Dim array2(nCell) As Integer Dim i As Integer For i = 0 To (array2.GetLength(0)) - 1 array2(i) = i + 1 Next Dim args2(1) As Object args2(0) = array2 Range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, Range2, args2) Everything works except the last line. Actually, the only thing I have been successful at doing was getting the Excel app to fire up. Beyond that I am left looking at beautiful, although empty, white cells. I'm using VS.Net 2005 latest beta. It seems simple enough... Read a datagrid, send the results to an Excel worksheet. I would use the more accepted method of using Response.Write, but due to our website configuration, things get all screwed up with using a Master page and

    tags. If anyone can help me out of this bind, I would be a very happy programmer.

    D 1 Reply Last reply
    0
    • R raysot777

      Hello, I am trying to export the contents of a datagrid to Excel. Here's the code I have so far: 'Fire up an Excel instance Dim ExcelApp As New Excel.Application ExcelApp.Visible = True Dim workbooks As Workbooks = ExcelApp.Workbooks Dim workbook As _Workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet) Dim sheets As Sheets = workbook.Worksheets Dim worksheet As _Worksheet = CType(sheets.Item(1), _Worksheet) 'Put some data on the Worksheet -- This doesn't work, but throws no errors Dim nCell As Integer = 5 Dim Range2 As Range = worksheet.Range("A1", "E1") Dim array2(nCell) As Integer Dim i As Integer For i = 0 To (array2.GetLength(0)) - 1 array2(i) = i + 1 Next Dim args2(1) As Object args2(0) = array2 Range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, Range2, args2) Everything works except the last line. Actually, the only thing I have been successful at doing was getting the Excel app to fire up. Beyond that I am left looking at beautiful, although empty, white cells. I'm using VS.Net 2005 latest beta. It seems simple enough... Read a datagrid, send the results to an Excel worksheet. I would use the more accepted method of using Response.Write, but due to our website configuration, things get all screwed up with using a Master page and

      tags. If anyone can help me out of this bind, I would be a very happy programmer.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      AFAICT, you can't bind an array to an Excel Range. You have to inster the values into the cells, one at a time. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        AFAICT, you can't bind an array to an Excel Range. You have to inster the values into the cells, one at a time. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        R Offline
        R Offline
        raysot777
        wrote on last edited by
        #3

        ...that seems to be where I am stuck at - inserting the values one at a time. Using the code: Dim args1(1) As Object args1(0) = nCell Range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, Range1, args1) ..I get an error that states: "Public member 'GetType' on type 'Range' not found." Since I'm using VS.Net 2005, I am wondering if there is a library I am missing or if the function has been removed altogether. That is where I am at. Inserting the data, even a single cell value is proving to be my ticket to insanity. So close, yet so far.

        D 1 Reply Last reply
        0
        • R raysot777

          ...that seems to be where I am stuck at - inserting the values one at a time. Using the code: Dim args1(1) As Object args1(0) = nCell Range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, Nothing, Range1, args1) ..I get an error that states: "Public member 'GetType' on type 'Range' not found." Since I'm using VS.Net 2005, I am wondering if there is a library I am missing or if the function has been removed altogether. That is where I am at. Inserting the data, even a single cell value is proving to be my ticket to insanity. So close, yet so far.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          First, the Range object is a little more difficult to use when filling in cells like this. The Cells object lets you index rows and columns sperately. Second, I'm wondering why your going through all the InvokeMember stuff when a simple assignment will do:

          For I As Integer = 1 to 5
          worksheet.Cells(I, 1) = I
          Next

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          R 1 Reply Last reply
          0
          • D Dave Kreskowiak

            First, the Range object is a little more difficult to use when filling in cells like this. The Cells object lets you index rows and columns sperately. Second, I'm wondering why your going through all the InvokeMember stuff when a simple assignment will do:

            For I As Integer = 1 to 5
            worksheet.Cells(I, 1) = I
            Next

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            R Offline
            R Offline
            raysot777
            wrote on last edited by
            #5

            I had no idea it could be that simple. This is a project for work (I'm normally a DBA by trade) and I'll plug in first thing Monday morning and give this a whirl. You have been incredibly helpful, Dave and I really appreciate your expertise. (I was just about to give up on this whole Excel thing and tell my boss if he wants the info on the website, just copy/paste it... :-) I'll let you know Monday how it all works out. If I can get just one cell to reflect info off the ASP page, I'll consider this a success. Ray

            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