『转』如何读取Excel文件并赋值给DataGridView对象
来源:互联网 发布:老版三国演义 知乎 编辑:程序博客网 时间:2024/05/19 05:01
如何读取Excel文件并赋值给DataGridView对象呢这里有两种方法第一种(推荐)Private Sub listExcel(ByVal dir) Dim connectionString As String = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source = " & dir & ";Extended Properties = 'Excel 12.0;HDR=YES';" Dim strSQL As String = "SELECT * FROM [Sheet1$]" Dim excelConnection As OleDbConnection = New OleDbConnection(connectionString) Dim dbCommand As OleDbCommand = New OleDbCommand(strSQL, excelConnection) Dim dataAdapter As OleDbDataAdapter = New OleDbDataAdapter(dbCommand) Try excelConnection.Open() dataAdapter.Fill(ds, "Excel") DataGridView1.DataSource = ds.Tables("Excel").DefaultView Catch sqlException As Exception MsgBox(sqlException.ToString() & Chr(13) & "出现异常,Excel文件打开失败") Exit Sub Finally dataAdapter.Dispose() dbCommand.Dispose() excelConnection.Close() excelConnection.Dispose() End TryEnd Sub第二种Private Sub listExcel(ByVal dir) Dim xlApp, xlBook, xlsheet Dim y As Integer = 1 Dim i As Integer Dim rows(18) As String xlApp = CreateObject("Excel.Application") Try xlBook = xlApp.Workbooks.Open(dir) Catch ioException As Exception MsgBox(ioException.ToString & Chr(13) & "出现异常,Excel文件打开失败") Exit Sub End Try xlsheet = xlBook.Worksheets(1) While (Convert.ToString(xlsheet.Cells(l, 1).Value)) <> "" y += 1 End While With DataGridView1 .EditMode = DataGridViewEditMode.EditOnEnter .ColumnCount = 19 .RowHeadersVisible = False .Columns(0).Name = "计费号码" .Columns(1).Name = "其它费" .Columns(2).Name = "合计" End With For i = 2 To y rows(0) = Convert.ToString(xlsheet.Cells(i, 1).Value) rows(1) = Convert.ToString(xlsheet.Cells(i, 15).Value) rows(2) = Convert.ToString(xlsheet.Cells(i, 16).Value) Me.DataGridView1.Rows.Add(rows) NextEnd Sub使用第一种方法速度非常快 *^_^*
- 『转』如何读取Excel文件并赋值给DataGridView对象
- 如何读取Excel文件并赋值给DataGridView对象
- PHP读取txt文件的内容并赋值给数组
- VB.NET 打开Excel文件,读取Excel内容,添加到DataGridView中并显示
- VB.NET 打开Excel文件,读取Excel内容,添加到DataGridView中并显示
- PHP 读取与写入txt文件并并赋值给数组
- JAVA-读取XML文件并将值赋给对象
- 读取Excel文件,加载数据到DataGridView
- C#读取Excel文件并用datagridview显示
- 如何读取数据库并绑定到dataGridview
- linux读取文件中的一行,解析字符串并赋值给变量
- PHP读取txt文件的内容并赋值给数组…
- C++ 读取文件内容赋值给变量
- 读取EXCEL文件并查询
- winform打开Excel读取数据并显示到datagridview中
- winform打开Excel读取数据并显示到datagridview中
- 如何动态给随机对象赋值
- dataGridView中值赋值给DataTable
- 程序编程中一点总结
- ADO.net整理收集
- CSS也有Framework了:Blueprint介绍
- java连接各种数据库的代码
- 60个精彩VS插件
- 『转』如何读取Excel文件并赋值给DataGridView对象
- 使用c#在vs2005下写的换桌面图片的小程序(from: http://www.wangchao.net.cn/bbsdetail_26766.html)
- SQL中的left outer join,inner join,right outer join用法
- 『转』数据库如何与DataTable同步
- Delphi中的线程类
- pringframework 2.0 与 ZK 混合开发实例
- 关于功率放大电路的几个基本概念
- 一个TThread类多线程实例
- 查询关系数据