Vb.net data import excel Code. This is a simple example of using vb.net to import database data to the execel instance. If you are looking for relevant data. net to import data to a data table, this is a complete available code.
Vb.net data import excel Code
This is a simple way to import the data of the database tutorial to the execel instance by using vb.net. If you are looking for related data. net to import data to a data table, this is a complete available code.
Imports system. data
Imports system. data. sqlclient
Public class form1
Dim app as new excel. application
Dim exbook as excel. workbook
Dim exsheet as excel. worksheet
Dim exrange as excel. range
Private sub button#click (byval sender as system. object, byval e as system. eventargs) handles button1.click
Exbook = app. workbooks. open ("d: www.bKjia.c0mc.xls ")
Exsheet = exbook. sheets (1)
Dim myconnection as new sqlconnection ("data source = 192.168.0.1; initial catalog = test; user id = sa; password = abc ")
Dim mycommand as sqlcommand = new sqlcommand ()
Myconnection. open ()
Mycommand. connection = myconnection
Mycommand. commandtext = "select un001, un002, un003 from bcun"
Mycommand. commandtype = commandtype. text
Dim myreader as sqldatareader = mycommand.exe cutereader
Dim I as integer = 1
Do while myreader. read ()
Exsheet. cells (I, 1) = myreader. getstring (0)
Exsheet. cells (I, 2) = myreader. getstring (1)
Exsheet. cells (I, 3) = myreader. getstring (2)
I = I + 1
Loop
Myreader. close ()
Myconnection. close ()
Exbook. save ()
Exbook. close ()
App. workbooks. close ()
App. quit ()
Mycommand. connection. close ()
End sub
End class
/*
Please note that the com component is added to the Reference: microsoft excel 11.0 object library (not necessarily 11.0, depending on your excel version)