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. Hey thnx. Solved my "inconvinience" ;P
View Tutorial          By: Hesam Ossanloo at 2013-12-19 15:13:31

2. Your post helped me to resolve the SSH exception..
View Tutorial          By: Ashwini at 2008-07-14 12:47:32

3. Thanks,It worked.I tried with Microsoft Visual C++
View Tutorial          By: gaurav at 2013-12-04 08:27:35

4. not work
View Tutorial          By: kkbb at 2011-03-12 03:52:06

5. Wow, Problem resolved ....
View Tutorial          By: Muhammad Farrukh at 2011-08-29 21:02:09

6. Rattling explanation .....thnx brother
View Tutorial          By: zorro at 2011-06-16 05:05:43

7. Finally! You enlighted me in a small problem: it w
View Tutorial          By: Fernando Martin at 2011-02-25 12:54:58

8. Dude,
Atleast indent and try to make sense

View Tutorial          By: Phani at 2015-03-19 16:51:57

9. Hi,
I've tried this but got a problem. Firs

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

10. NICE EXPLANATION >>>THANK UUUUUU
View Tutorial          By: PraVeen at 2012-07-05 10:12:25