J
jonny
Hi I am trying to get this code to work so I can export gridview data
to excel. However I am running into a problem with this code:
Here is my immediate problem...VB.net 2008 does not like where I have
"GridView1(j, i).Value.ToString()"...see below.
For i = 0 To GridView1.Rows.Count - 2
For j = 0 To GridView1.Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
GridView1(j, i).Value.ToString()
Next
Next
Error says "Class 'System.Web.UI.WebControls.GridView' cannot be
indexed because it has no default property."
Here is the entire code below where I am trying to export gridview to
excel...any help on this is greatly appreciated:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click
Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String
connectionString = ConfigurationManager.ConnectionStrings
("JohnsConnection").ConnectionString
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = "SELECT * FROM ProductivityData"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
dscmd.Fill(ds)
GridView1.DataSource = ds.Tables(0)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To GridView1.Rows.Count - 2
For j = 0 To GridView1.Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
GridView1(j, i).Value.ToString()
Next
Next
xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file C:\vbexcel.xlsx")
End Sub
to excel. However I am running into a problem with this code:
Here is my immediate problem...VB.net 2008 does not like where I have
"GridView1(j, i).Value.ToString()"...see below.
For i = 0 To GridView1.Rows.Count - 2
For j = 0 To GridView1.Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
GridView1(j, i).Value.ToString()
Next
Next
Error says "Class 'System.Web.UI.WebControls.GridView' cannot be
indexed because it has no default property."
Here is the entire code below where I am trying to export gridview to
excel...any help on this is greatly appreciated:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
EventArgs) Handles Button1.Click
Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String
connectionString = ConfigurationManager.ConnectionStrings
("JohnsConnection").ConnectionString
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = "SELECT * FROM ProductivityData"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
dscmd.Fill(ds)
GridView1.DataSource = ds.Tables(0)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To GridView1.Rows.Count - 2
For j = 0 To GridView1.Columns.Count - 1
xlWorkSheet.Cells(i + 1, j + 1) = _
GridView1(j, i).Value.ToString()
Next
Next
xlWorkSheet.SaveAs("C:\vbexcel.xlsx")
xlWorkBook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)
MsgBox("You can find the file C:\vbexcel.xlsx")
End Sub