473,406 Members | 2,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How can I export the data from datagrid to Excel using Visual Basic?

Hi, I am new to visual basic.
In my vb system, there is a dynamic datagrid to represent different reocrds in our database. Today I am gonna export the data to Excel file. I found most examples code in Google but all didn't work. Does anyone can help me? I am using visual basic 2010. I dont know how can get the value of the cells in the datagrid and the number of rows of the datagrid. Thanks everyone!
Nov 17 '10 #1
2 3482
NeoPa
32,556 Expert Mod 16PB
Access can export its objects directly into Excel format. There doesn't appear to be any reason to know what rows and columns are included from your question.
Nov 17 '10 #2
A simple way would be exporting it to a tab-delimited text file, and just importing that into excel. Here's some code that will export to tab or csv depending on parameters.

Expand|Select|Wrap|Line Numbers
  1.    Sub DataTable2Tab(ByVal table As System.Data.DataTable, ByVal filename As String, _
  2.     ByVal sepChar As String)
  3.         Dim writer As System.IO.StreamWriter
  4.         Try
  5.             writer = New System.IO.StreamWriter(filename)
  6.  
  7.             ' first write a line with the columns name
  8.             Dim sep As String = ""
  9.             Dim builder As New System.Text.StringBuilder
  10.             For Each col As DataColumn In table.Columns
  11.                 builder.Append(sep).Append(col.ColumnName)
  12.                 sep = sepChar
  13.             Next
  14.             writer.WriteLine(builder.ToString())
  15.  
  16.             ' then write all the rows
  17.             For Each row As DataRow In table.Rows
  18.                 sep = ""
  19.                 builder = New System.Text.StringBuilder
  20.  
  21.                 For Each col As DataColumn In table.Columns
  22.                     builder.Append(sep).Append(row(col.ColumnName))
  23.                     sep = sepChar
  24.                 Next
  25.                 writer.WriteLine(builder.ToString())
  26.             Next
  27.         Finally
  28.             If Not writer Is Nothing Then writer.Close()
  29.         End Try
  30.     End Sub
  31.  
You can call it like this:
Expand|Select|Wrap|Line Numbers
  1. DataTable2Tab(finalTable, filePath, vbTab)
  2.  
Nov 17 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: C | last post by:
Hi, Does anyone know of any sites that have sample code that shows how to export data to Excel using VS Tools for Office? What is the advantage of using VS Tools for Office over uisng Interop?...
7
by: Vanessa | last post by:
hi Everyone, I have two questions on exporting data to Excel using ASP (w/o converting formatted excel file into web page and then plug in the dynamic data): 1. Can we export data into...
5
by: Chase Kang #52 | last post by:
Using VB.NET, I've created a report that takes data from a stored procedure, which goes into some aspx page controls, and inserts that data into a pre-formatted Excel spreadsheet template by using...
1
by: forumaic | last post by:
Hello, I am trying to export data to excel from datagrid, and I am getting an error: "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." ...
25
wassup
by: wassup | last post by:
Hi all, This my 1st time to get help at forum. So my problem is I have a form with some textbox inside and I want save the data that key in inside textbox to the Excel format that I create and the...
4
by: bonkbc | last post by:
hi! how can we take textbox data from a visual basic 6.0 form and write it to an excel worksheet once a command button is clicked? I've gotten very close to completing this on my own, here is...
1
by: jameswambura | last post by:
how to read abitmap image from access table and display in visual basic interface
0
by: Galore | last post by:
Hi All, I am a new member. I am trying to run a query (e.g. select * from SybaseTable where SybaseCol = '12') directly from Excel. I am hoping to be able to define the Data Source (define...
0
by: suresh_punniyakkodi | last post by:
Hellow Friends, I have one doubt, please help me... In Excel, i have lot of rows and coloumns, i need to read all cell values with in rows and coloumn limit... At the time...
0
by: bdhassan87 | last post by:
how can i insert data to datagrid from text box using visual basic 6.0
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.