vb excel导入到datagridview中出现的问题
vb excel导入到datagridview中出现了一些问题,谁能帮我看看吗?导入代码如下:
Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
fileDialog.InitialDirectory = My.Computer.FileSystem.CurrentDirectory
fileDialog.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*"
fileDialog.FilterIndex = 2
fileDialog.RestoreDirectory = True
If fileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fileName As String
fileName = fileDialog.FileName
'建立EXCEL连接, 读入数据
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
Dim myDataset As New DataSet
Dim da As New OleDb.OleDbDataAdapter("SELECT 序号 as ProductionID,订单编号 as OrderNumber,机型代码 as EngineType,机型名称 as EngineName,计划数量 as Production_Plan,计划上线时间 as StartTime,描述 as Description FROM [Sheet1$]", strConn)
Try
da.Fill(myDataset)
DataGridView.DataSource = myDataset.Tables(0)
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End If
End Sub
excel表中数据如下:
序号 订单编号 机型代码 机型名称 计划数量 计划上线时间 描述
1 ddd 11 12 0 2012-3-28 13:51
2 121 fs 23 0 2012-3-28 13:51
3 aaa 23 www 0 2012-3-28 13:51
4 433 efwf 121 0 2012-3-29 9:30
5 sss 21 eee 0 2012-3-29 9:32
6 344 sfs 22 0 2012-3-29 9:32
7 ggg 222 rrr 0 2012-3-29 9:32
导入datagridview显示结果如下:
序号 订单编号 机型代码 机型名称 计划数量 计划上线时间 描述
1 ddd 11 12 0 2012-3-28 13:51
2 23 0 2012-3-28 13:51
3 aaa 23 0 2012-3-28 13:51
4 121 0 2012-3-29 9:30
5 sss 21 0 2012-3-29 9:32
6 22 0 2012-3-29 9:32
7 ggg 222 0 2012-3-29 9:32
--------------------编程问答-------------------- 出现的问题贴出来啊! --------------------编程问答-------------------- 我把代码改了一下,运行就正常了。我是vx2010。
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
fileDialog.InitialDirectory = My.Computer.FileSystem.CurrentDirectory
fileDialog.Filter = "excel files (*.xls)|*.xls|All files (*.*)|*.*"
fileDialog.FilterIndex = 2
fileDialog.RestoreDirectory = True
If fileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fileName As String
fileName = fileDialog.FileName
'建立EXCEL连接, 读入数据
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileName & "';Extended Properties=Excel 8.0;"
Dim myDataset As New DataSet
Dim da As New OleDb.OleDbDataAdapter("select * FROM [Sheet1$]", strConn)
Dim bindin As BindingSource = New BindingSource
da.Fill(myDataset)
bindin.DataSource = myDataset.Tables(0)
DataGridView6.DataSource = bindin.DataSource
End If
补充:.NET技术 , VB.NET