471,853 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Exporting datagrid to excel

Hi , I have the following code to export a datagrid to excel

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

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 =
.Range(C hr(c) & "1").Font.Bold = True
c += 1

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 =
c += 1
i += 1

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

However the code doesnt work , the following are the errors

Error 3 Property 'Range' is 'ReadOnly'.
Error 4 Comma, ')', or a valid expression continuation expected -- for

can someone help me out with this code , or suggest an alternative way
of exporting a datagrid to excel or sql.

this code is called as follows:



Jul 24 '07 #1
0 1191

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Hideyuki | last post: by
reply views Thread by Amber | last post: by
5 posts views Thread by Neil | last post: by
1 post views Thread by Mustufa Baig | last post: by
7 posts views Thread by Stephen Noronha | last post: by
6 posts views Thread by Opa | last post: by
4 posts views Thread by Tom | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by aboka | last post: by

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.