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 i need a sample program to draw table or grid
View Tutorial          By: pavi at 2012-10-15 04:27:27

2. can i using stack in microsoft access 2007
View Tutorial          By: pxin at 2015-03-24 05:45:56

3. Hi Rajita, in accordance to our 'Privacy Policy' w
View Tutorial          By: webmaster at 2008-03-25 18:49:39

4. nice article..
View Tutorial          By: yummy at 2010-01-05 21:18:35

5. @readers
this is an example of Hiding and N

View Tutorial          By: DevJ at 2011-03-18 05:41:52

6. Unfortunatelly this a BAD example of to communicat
View Tutorial          By: Mladen Covic at 2013-03-28 15:08:04

7. its great!!!!
View Tutorial          By: safrgdg at 2015-02-12 10:10:07

8. Float f=99.7f;
int i1=Integer.parseInt(f);<

View Tutorial          By: Ashwin at 2012-12-23 05:14:04

9. IT IS VERY USEFUL FOR BEGINNERS
View Tutorial          By: Adhi Rudran at 2007-04-16 02:58:47

10. To "need help":

String st

View Tutorial          By: alex at 2010-05-13 09:57:25