473,327 Members | 2,094 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,327 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 3470
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.