大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本例子在窗体上添加一个按钮,一个空的DataGridView控件;
创新互联自2013年起,是专业互联网技术服务公司,拥有项目成都网站建设、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元新城做网站,已为上家服务,为新城各地企业和个人服务,联系电话:028-86922220
在按钮的单击事件里编写代码如下:
'读取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
解题思路:
把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
你好,可以在机器上装上office2010,重新设置一番。或者找一台安装了office2010的机器,把代码和环境拷过去,远程调试就可以了,请参考!
直接用Open语句即可!
app.Workbooks.Open(filename:="D:\xx.txt")
如果要指定分隔符,比如“a-b-c”,将a,b,c三个字符分别存储到三个不同单元格,那么就要设置分隔符:
app.Workbooks.Open(filename:="D:\xx.txt",Format:=6,Delimiter:="-")
Format:=6表示允许自定义分隔符
Delimiter:="-",如果Format:参数设置为6,那么指定分隔符为"-"