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. Export Data To Excel

Export Data To Excel

Scheduled Pinned Locked Moved Visual Basic
help
6 Posts 5 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.
  • M Offline
    M Offline
    mayhem_rules
    wrote on last edited by
    #1

    Hi, I need to export data from a dataset (datatable) into Excel or csv format. Can anyone help me out with this. With Best Regards, Mayur -- modified at 0:02 Thursday 16th February, 2006

    J 1 Reply Last reply
    0
    • M mayhem_rules

      Hi, I need to export data from a dataset (datatable) into Excel or csv format. Can anyone help me out with this. With Best Regards, Mayur -- modified at 0:02 Thursday 16th February, 2006

      J Offline
      J Offline
      Joshua Quick
      wrote on last edited by
      #2

      You should search the articles and forums on this website. I found some hits on my first try. Look at these... http://www.codeproject.com/aspnet/ExportClassLibrary.asp[^] http://www.codeproject.com/dotnet/ExportToExcel.asp[^] http://www.codeproject.com/useritems/excelxmlreport.asp[^]

      M 1 Reply Last reply
      0
      • J Joshua Quick

        You should search the articles and forums on this website. I found some hits on my first try. Look at these... http://www.codeproject.com/aspnet/ExportClassLibrary.asp[^] http://www.codeproject.com/dotnet/ExportToExcel.asp[^] http://www.codeproject.com/useritems/excelxmlreport.asp[^]

        M Offline
        M Offline
        mayhem_rules
        wrote on last edited by
        #3

        Are there any examples for vb.net With Best Regards, Mayur

        R L 2 Replies Last reply
        0
        • M mayhem_rules

          Are there any examples for vb.net With Best Regards, Mayur

          R Offline
          R Offline
          RichardBerry
          wrote on last edited by
          #4

          Hi You need to add a reference to Microsoft Excel 10 Object Library. There are other examples where you do not use the com object, which have the advantage that Excel does not need to be installed on the target machine. Then: Private Sub ExportExcel(ByRef dt As DataTable) Dim excelApp As New Excel.Application Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add Dim excelWorksheet As Excel.Worksheet = _ CType(excelBook.Worksheets(1), Excel.Worksheet) Dim dr As DataRow Dim Row As Integer = 1 Dim Col As Integer = 1 excelApp.Visible = False If dt.Rows.Count > 65536 Then MessageBox.Show("Warning - Excel sheet can only have 65536 Rows" _ & ControlChars.NewLine & ControlChars.NewLine _ & 65536 - dt.Rows.Count & " Rows will be omitted") End If If dt.Columns.Count > 256 Then MessageBox.Show("Warning - Excel sheet can only have 256 Rows" _ & ControlChars.NewLine & ControlChars.NewLine _ & 256 - dt.Columns.Count & " Columns will be omitted") End If With excelWorksheet 'Headings For Col = 0 To dt.Columns.Count - 1 .Cells(1, Col + 1).Value = dt.Columns(Col).ColumnName .Cells(1, Col + 1).Font.Bold = True .Cells(1, Col + 1).ColumnWidth = Len(dt.Columns(Col).ColumnName) + 3 Next .Range("A:Z").NumberFormat = "@" '(Text Format...) Row = 2 Col = 0 'Data Do While Row < dt.Rows.Count - 1 And Row < 65536 Do While Col <= dt.Columns.Count - 1 And Col < 255 .Cells(Row, Col + 1) = dt.Rows(Row - 2).Item(Col) Col += 1 Loop Col = 0 Row += 1 Loop End With excelApp.Visible = True End Sub -- modified at 4:37 Thursday 16th February, 2006

          K 1 Reply Last reply
          0
          • M mayhem_rules

            Are there any examples for vb.net With Best Regards, Mayur

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            mayhem_rules wrote:

            Are there any examples for vb.net

            http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx[^]

            1 Reply Last reply
            0
            • R RichardBerry

              Hi You need to add a reference to Microsoft Excel 10 Object Library. There are other examples where you do not use the com object, which have the advantage that Excel does not need to be installed on the target machine. Then: Private Sub ExportExcel(ByRef dt As DataTable) Dim excelApp As New Excel.Application Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add Dim excelWorksheet As Excel.Worksheet = _ CType(excelBook.Worksheets(1), Excel.Worksheet) Dim dr As DataRow Dim Row As Integer = 1 Dim Col As Integer = 1 excelApp.Visible = False If dt.Rows.Count > 65536 Then MessageBox.Show("Warning - Excel sheet can only have 65536 Rows" _ & ControlChars.NewLine & ControlChars.NewLine _ & 65536 - dt.Rows.Count & " Rows will be omitted") End If If dt.Columns.Count > 256 Then MessageBox.Show("Warning - Excel sheet can only have 256 Rows" _ & ControlChars.NewLine & ControlChars.NewLine _ & 256 - dt.Columns.Count & " Columns will be omitted") End If With excelWorksheet 'Headings For Col = 0 To dt.Columns.Count - 1 .Cells(1, Col + 1).Value = dt.Columns(Col).ColumnName .Cells(1, Col + 1).Font.Bold = True .Cells(1, Col + 1).ColumnWidth = Len(dt.Columns(Col).ColumnName) + 3 Next .Range("A:Z").NumberFormat = "@" '(Text Format...) Row = 2 Col = 0 'Data Do While Row < dt.Rows.Count - 1 And Row < 65536 Do While Col <= dt.Columns.Count - 1 And Col < 255 .Cells(Row, Col + 1) = dt.Rows(Row - 2).Item(Col) Col += 1 Loop Col = 0 Row += 1 Loop End With excelApp.Visible = True End Sub -- modified at 4:37 Thursday 16th February, 2006

              K Offline
              K Offline
              KreativeKai
              wrote on last edited by
              #6

              Can you give me some more input on not having Excel loading on the target machine. Below is a link to a question I had, the feedback I'm getting is you need to have Excel loaded. Can you give me more feedback on not having Excel loaded? http://www.codeproject.com/script/comments/forums.asp?msg=1396040&forumid=1646&mode=all&userid=387911#xx1396040xx[^] Lost in the vast sea of .NET

              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