Hi , I have the following code to export a datagrid to excel
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
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
hr
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:
ExportToExcel(DataGrid1.DataSource)
Thanks