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. //Convert numbers to word as per Indian number sys
View Tutorial          By: Abhishek at 2011-09-05 10:42:20

2. Hi Everyone, i tried the code. im using this code.
View Tutorial          By: sourabh at 2011-09-09 12:42:02

3. i installed the code in ecllipse,but i have one do
View Tutorial          By: vamsi at 2015-05-19 10:56:25

4. A very good example to kickstart my preparation on
View Tutorial          By: Javaenthu at 2013-07-28 06:35:44

5. error is main fuuction declaration
void mai

View Tutorial          By: T.M.Prakash.MCA. at 2012-09-20 12:26:52

6. C++ is more than nyc & fun, de most interestin
View Tutorial          By: Themba at 2010-09-12 09:11:04

7. very simple to understand
View Tutorial          By: chermaraja at 2010-05-26 00:00:05

8. could u pls tell me whcih class is implemted by it
View Tutorial          By: AMIT at 2009-10-04 00:21:30

9. Thanks for giving this basic example of Abstract c
View Tutorial          By: Yogendra Kirar at 2010-08-07 07:48:34

10. Hi

Where puts this code in ror app

View Tutorial          By: martin at 2013-01-31 08:51:12