vb.net data import Excel code
This is a simple vb.net to the database tutorial data into the Execel example Oh, if you are looking for the relevant. NET to import data into the datasheet, this is a complete code available.
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 Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Exbook = App.workbooks.open ("D:www.111cn.netc.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.executereader
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
/*
Note Add a COM component to a reference: Microsoft Excel 11.0 Object Library (not necessarily 11.0, which is originally set by your Excel version)