扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
解题思路:
成都创新互联公司是一家专业提供铅山企业网站建设,专注与网站设计、网站制作、H5场景定制、小程序制作等业务。10年已为铅山众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
把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
假设文本文件如你例子,每行2个数据,数据之间用逗号分隔;
'用StreamReader打开指定的文本文件进行读操作
Dim r As New System.IO.StreamReader(Application.StartupPath "\123.TXT")
Dim i As Integer
Dim s As String
Dim d() As String
i = -1
Do While r.Peek -1 '是否到文件尾
s = r.ReadLine '从打开的文件中读取一行内容
d = Split(s, ",")
DataGridView1.Rows.Add()
i = i + 1
'把一行数据写入网格控件的一个行里
DataGridView1.Item(0, i).Value = d(0)
DataGridView1.Item(1, i).Value = d(1)
Loop
r.Close() '关闭对象
给你看一段我的代码
Public xlApp As Excel.Application
Public xlBook As Excel.Workbook
Public xlSheet As Excel.Worksheet
xlApp = CreateObject("Excel.Application") '创建EXCEL对象
xlBook = xlApp.Workbooks.Open(OpenFileDialog1.FileName) '打开已经存在的EXCEL工件簿文件
xlSheet = xlBook.Worksheets(xlBook.Sheets(1).name) '读取excel
'****************************读取excel数据,显示在数据表上****************************************
DataGridView1.Rows.Clear() '清空记录
For i = 5 To 19
If xlSheet.Cells(i, 1).value "" Then
DataGridView1.Rows.Add(xlSheet.Cells(2, 9).value, xlSheet.Cells(i, 1).value, xlSheet.Cells(2, 2).value, xlSheet.Cells(2, 7).value, xlSheet.Cells(2, 4).value, CDate(DateTimePicker1.Value), "", xlSheet.Cells(i, 5).value, Combo_CZZ.Text, Combo_MacName.Text, "", xlSheet.Cells(1, 3).value, xlSheet.Cells(1, 6).value, "", Combo_BanCi.Text, "")
End If
Next
用System.IO.StreamReader类可以读各类文本文件,用System.IO.StreamWriter 类则可以写各类文本文件,从而实现导入导出文本文件功能。这两个类均支持各类编码格式(默认的是Unicode和UTF-8)。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流