Comment on Tutorial - How to export from database to excel using VB.net By Issac



Comment Added by : John McCode

Comment Added at : 2013-05-24 07:57:31

Comment on Tutorial : How to export from database to excel using VB.net By Issac
you do the same as after
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cnn As SqlConnection
Dim sql As String
Dim i, j As Integer

Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value

xlApp = New Excel.Application
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
cnn = Connection.getConnection()
cnn.Open()
sql = "SELECT * FROM tblNVcanhan"
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet
dscmd.Fill(ds)
For i = 0 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Cells(1, i + 1) = ds.Tables(0).Columns(i).ToString()
Next
For j = 0 To ds.Tables(0).Rows.Count - 1
xlWorkSheet.Cells(j + 2, 1) = ds.Tables(0).Columns(j).ToString()
For i = 0 To ds.Tables(0).Columns.Count - 1
xlWorkSheet.Cells(j + 2, i + 1) = ds.Tables(0).Rows(j).Item(i).ToString()
Next
Next
xlApp.Columns.AutoFit()
'xlWorkSheet.SaveAs("D:\vbexcel.xlsx")
'xlWorkBook.Close()
MsgBox("You can find the file D:\vbexcel.xlsx")
xlApp.Visible = True

releaseObject(xlApp)
releaseObject(xlWorkBook)
releaseObject(xlWorkSheet)

cnn.Close()

End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub


View Tutorial



Subscribe to Tutorials

Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. Guys, txtWeb is back with their National Level Dev
View Tutorial          By: rahul.del at 2012-08-16 10:01:09

2. Thanks for the Information. Its very useful
View Tutorial          By: Husain at 2009-03-04 13:30:38

3. i want to need SCJD Dums. so please help me that w
View Tutorial          By: Dayamay singh at 2009-01-03 00:07:06

4. Dude, that LOOP wont work i cant even understand w
View Tutorial          By: Rohan at 2012-02-16 17:04:06

5. Is it possible to create a Reader class without lo
View Tutorial          By: bs at 2010-10-11 05:42:34

6. like
View Tutorial          By: po!X!on at 2011-05-05 01:30:51

7. it is a good example to understand.
View Tutorial          By: nivedha at 2011-07-12 05:15:39

8. Hello all..
can anyone please help me.. i

View Tutorial          By: Christoven at 2011-06-15 05:55:16

9. pls help me how to solve the timeout problem

View Tutorial          By: nisha at 2010-09-20 22:29:17

10. code givr an error when sending the Email:

View Tutorial          By: Ravi Shanker Yadav at 2013-05-08 12:36:59