我是靠谱客的博主 震动橘子,这篇文章主要介绍VB.net小技巧——使用DataGridView显示EXECL表格内容,现在分享给大家,希望可以做个参考。

VB.net小技巧——使用DataGridView显示EXECL表格内容

直接上代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click Dim fileDialog As OpenFileDialog = New OpenFileDialog() fileDialog.InitialDirectory = My.Computer.FileSystem.CurrentDirectory fileDialog.Filter = "Excel files(*.xls)|*.xls|Excel files(*.xlsx)|*.xlsx" fileDialog.FilterIndex = 1 fileDialog.RestoreDirectory = True If fileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim fileName As String Dim strConn As String fileName = fileDialog.FileName '建立EXCEL连接, 读入数据 If fileDialog.FilterIndex = 1 Then strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;" Else strConn = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;" End If Dim myDataset As New DataSet Dim da As New OleDb.OleDbDataAdapter("select * FROM [Sheet1$]", strConn) Dim bindin As BindingSource = New BindingSource Try DataGridView1.Columns.Clear() da.Fill(myDataset) bindin.DataSource = myDataset.Tables(0) DataGridView1.DataSource = bindin.DataSource Catch ex As Exception MessageBox.Show("EXECL文件载入失败") End Try End If End Sub

其中

例如有一个这样的execl文档
在这里插入图片描述
载入后:
在这里插入图片描述
行和列都是从0数字开始。

DataGridView1.Rows(0).Cells(0).Value = 0000008
DataGridView1.Rows(0).Cells(3).Value 是空白,可以写入数值进去
以上的例子是可以对接收到的协议数据进行地址辨别后,自动将相应的值填入表格中,不需要每次人为触发查询,且自动将原始数据进行公式换算后显示,非常的方便。

最后

以上就是震动橘子最近收集整理的关于VB.net小技巧——使用DataGridView显示EXECL表格内容的全部内容,更多相关VB内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部