Vb.net data import excel Code

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.