spacer
spacer

Visual Basic & VB.NET resources | VBprofs.com

Google
 
Main Menu
Home
IT News
Articles
Source Codes
Books
Calendar of Events
Publish your Content
Links Directory
Reviews and Recs
Premium Content
 VBprofs Premium Content Become a member to get access to our premium content! 

 Membership is entirely free, and allows you to:
  •  access programming projects posted by our partners;
  •  publish your articles, news, links;
  •  publish a project request;
  •  access our VB Careers section.
Polls
How did you reach VBprofs.com?
  
Which is your favorite programming language?
  
 
Home arrow Source Codes arrow .NET arrow Export DataGrid to Excel File with VB.NET

Export DataGrid to Excel File with VB.NET PDF Print E-mail
Source Codes - .NET
Written by Thomas Kaloyani   
.NET I have recently had a request to add export to excel file functionality on the datagrid lists of the system.

Here is a small and simple way that can be done.
The code is pretty simple so the output is simple. Nothing fancy!

Usage:
ExportToExcel(DataGrid1.DataSource)

Declarations:
Refence to Microsoft Excel should be added.


Code:
Public Sub ExportToExcel(ByVal dt As DataTable)
   Try
      Dim oApp As New Excel.Application
      Dim oBook As Excel.Workbook = oApp.Workbooks.Add
      Dim oSheet As Excel.Worksheet =  CType(oBook.Worksheets(1), Excel.Worksheet)

      oApp.Visible = False

      With oSheet
         Dim c As Long = Asc("A")
         For Each dc As DataColumn In dt.Columns
            .Range(C hr(c) & "1").Value = dc.ColumnName.ToString
            .Range(C hr(c) & "1").Font.Bold = True
            c += 1
         Next

         Dim i As Long = 2
         For Each dr As DataRow In dt.Rows
            c = Asc("A")
            For Each dc As DataColumn In dt.Columns
               .Range(C hr(c) & i.ToString).Value =  dr.Item(dc.ColumnName)
               c += 1
            Next
            i += 1
         Next

         oApp.Visible = True
      End With
   Catch ex As Exception
      MessageBox.Show("Source [" & ex.Source & "] Description  [" & ex.Message & "]")
   End Try
End Sub


Happy coding!




Thomas is an experienced Visual Basic developer, with expertise of 7+ years developing especially financial applications. His main IT skills are VB, SQL, Crystal Reports - should you need a Visual Basic developer for your projects feel free to contact Thomas through his personal website www.Kaloyani.com or through VBprofs.com - the newest Visual Basic and VB.NET resources portal.

Source:
www.VBprofs.com
< Prev   Next >

 
Antivirus Shop
BitDefender Antivirus v10
Newsflash

PowerRefresh 1.0 released!
A simple yet extremely useful tool for webmasters, publishers and SEO working bees: PowerRefresh allows you to automatically refresh your IE windows every x minutes. It can handle unlimited number of windows and, unlike similar applications, is using full browsers.


Get PowerRefresh from Kaloyani.com: download
Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one
Popular

VBprofs.com - online resources for Visual Basic and VB.NET professionals: Visual Basic and VB.NET articles, industry news and events, career tools, VB / VB.NET books, calendar and much more. VBprofs.com is an interactive web site with free membership.
(c) Copyright 2005 - 2006 by VBprofs.com
powered by Mambo Open Source Software
spacer