vb点虐 数据导入 用vb将excel数据导入access
vb点虐 怎样把excel内容导入到datagridview
解题思路:
为成华等地区用户提供了全套网页设计制作服务,及成华网站建设行业解决方案。主营业务为网站建设、做网站、成华网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
把EXCEL看做数据源来连接。
用一个list来显示EXCEL中的烂弊所有表,选择指定表名,清历册打开EXCEL到DG
——————————如下:
引用ADODB 2.8
导入:
Imports System.Data
Imports System.Data.Odbc
Imports System.Data.OleDb
申明
Private Excelpath As String
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
创建conn
Public Sub connDB(ByVal filePath As String)
On Error Resume Next
Dim strConnString As String = "Provider=microsoft.ace.oledb.12.0;Data Source=" filePath ";Extended Properties='Excel 12.0; HDR=yes;IMEX=1';"
conn.ConnectionString = strConnString
conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
conn.Open()
End Sub
Public Function 获取电子表集合(filePath) As List(Of String)
Dim ConnStr As String = "Provider=microsoft.ace.oledb.12.0;Data Source=" filePath ";Extended Properties='Excel 12.0; HDR=no;IMEX=1';"
Dim list As New List(Of String)()
Dim Conn2 As New OleDbConnection(ConnStr)
Try
If Conn2.State = ConnectionState.Closed Then
Conn2.Open()
End If
Dim dt As DataTable = Conn2.GetSchema("Tables")
For Each row As DataRow In dt.Rows
If row(3).ToString() = "TABLE" Then
list.Add(row(2).ToString())
End If
Next
Catch e As Exception
'Throw e
Finally
If Conn2.State = ConnectionState.Open Then
Conn2.Close()
End If
Conn2.Dispose()
End Try
Return list
End Function
//答宏////////////////
开始打开指定EXCEL到DG
Call connDB(Excelpath)
Dim sqlstr As String = "select * from [" Me.ListBox1.Text "]"
With rs
If rs.State 0 Then rs.Close()
.CursorLocation = ADODB.CursorLocationEnum.adUseClient
.Open(sqlstr, conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
If Not rs.EOF And Not rs.BOF Then
Dim da As New System.Data.OleDb.OleDbDataAdapter
Dim ds As New DataSet
da.Fill(ds, rs, "注册表")
DG.DataSource = Nothing
DG.DataSource = ds.Tables(0)
DG.Refresh()
End If
End With
如何将已有的txt格式的文件数据导入VB.NET程序中,(也就是在VB.NET中读取所需的txt文
一个按钮,一个文本框,把文本框设置成多行显示。
代码如下:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim r As New StreamReader(Application.StartupPath "\ABC.txt", System.Text.Encoding.Default) '用StreamReader打开丛盯文本文件
Dim s As String
TextBox1.Text = ""
Do While r.Peek -1 '是否到文件尾禅纳
s = r.ReadLine '从打开的文件中读取一行内容
TextBox1.Text = TextBox1.Text s vbCrLf '添加到文本框渗袭和TextBox1.Text的后面并回车
Loop
r.Close() '关闭对象
End Sub
End Class
vb点虐 将csv 数据导入datagridview
本例子在窗体上添加一个按钮,一个空的DataGridView控件;
在按钮的单击事件里编写代码如下:
'读取CSV文件到DataGridView控件
Dim r As New StreamReader("C:\工作簿1.csv", System.Text.Encoding.Default) 轿没'用StreamReader打开文件
Dim MyRows(0) As String
Dim d() As String
Dim i As Integer = -1
Dim j As Integer = 0
旁帆乱 Do While r.Peek -1 '是否到文件尾
i = i + 1
ReDim Preserve MyRows(i)
MyRows(i) = r.ReadLine '从打开的文件中读取一行内容
Loop
r.Close() 运档 '关闭对象
'获得数据的列数
d = Split(MyRows(1), ",")
'首先在DataGridView控件中添加列
DataGridView1.ColumnCount = UBound(d) + 1
'然后在DataGridView控件中添加行
DataGridView1.Rows.Add(UBound(MyRows))
'在DataGridView控件中添加数据
For i = 0 To UBound(MyRows)
d = Split(MyRows(i), ",")
For j = 0 To UBound(d)
DataGridView1.Item(j, i).Value = d(j)
Next
Next
本文名称:vb点虐 数据导入 用vb将excel数据导入access
文章地址:http://myzitong.com/article/ddpopph.html