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. its useful code.
but i want to write in a f

View Tutorial          By: SARWAR at 2011-08-02 06:26:37

2. sir, i want get the SCJA certification. how to rig
View Tutorial          By: john basha at 2012-12-16 07:13:46

3. Hi!
I would like to send a string from a j2

View Tutorial          By: beetlebrain at 2010-03-26 06:02:00

4. Awesome............
View Tutorial          By: Amol at 2011-06-11 05:32:49

5. To 15:
Sometimes it is useful to return the

View Tutorial          By: Anderson Goulart at 2010-08-24 19:15:14

6. Bad article ! What about the explanation of Messag
View Tutorial          By: Manish at 2011-05-24 04:44:03

7. please explain importance of java and explain it's
View Tutorial          By: sandhi.vijayanth reddy at 2012-10-01 16:30:54

8. pretty good explanation
View Tutorial          By: Prithwi at 2009-11-26 07:28:33

9. hi....
i want to send sms to any mobile by

View Tutorial          By: prerna at 2010-07-09 20:15:48

10. dear sir, plz answer my following questions:

View Tutorial          By: hawa at 2012-01-29 08:29:39