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:56:04

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. Hi,
I've tried this but got a problem. Firs

View Tutorial          By: Hendrik Jabs at 2009-12-10 02:46:13

2. hey..
shouldn't the 2nd java code have an o

View Tutorial          By: izzie at 2010-04-06 11:34:34

3. There was no sharing of volatile variable in the p
View Tutorial          By: Rajesh Poppoppu at 2011-12-30 13:04:42

4. gud example, we welcome you
View Tutorial          By: jasim Khalid almurakh meherbani at 2010-09-20 03:14:25

5. Abstract class is a class that can not be instanti
View Tutorial          By: Abstract at 2011-07-19 01:14:09

6. For Jaime,

Maybe using JavaScript f

View Tutorial          By: Jair Aviles at 2011-11-22 05:49:05

7. can i connect mobile phone to usb port instead of
View Tutorial          By: Nevil at 2010-01-11 00:19:17

8. Wow! Thank you!
View Tutorial          By: David at 2013-04-24 15:52:45

9. Till now, I have'nt done any certifications. I am
View Tutorial          By: Subhash at 2012-03-15 10:48:29

10. I wont to know about every thing of java
View Tutorial          By: susantha at 2009-10-11 09:35:40