请帮助我们,这是我第一次尝试将excel文件导入vb.net中的datagridview。方案是我的excel文件有这么多数据,我只需要将从B列开始的值导入到第一列,并且开始行必须在B列中,单词“START”,并在行中以“结束”。到目前为止,这是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv"
If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Try
Dim fi As New FileInfo(OpenFileDialog1.FileName)
Dim sConnectionStringz As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName
Dim objConn As New OleDbConnection(sConnectionStringz)
objConn.Open()
'DataGridView1.TabIndex = 1
Dim objCmdSelect As New OleDbCommand("SELECT * FROM " & fi.Name, objConn)
Dim objAdapter1 As New OleDbDataAdapter
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet
objAdapter1.Fill(objDataset1)
'--objAdapter1.Update(objDataset1) '--updating
DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
objConn.Close()
Finally
End Try
End If
End Sub
此代码导入/显示datagridview中的所有数据。我已经到处搜索,但似乎没有任何东西适合。不管怎样,谢谢..
这是示例文件:
CLIENT-RD/OT HC 8 7 8 10 11 12 10
C-RD/OT 6:00 0 0 0 0 0 0 0
C-RD/OT 7:30 0 0 0 0 0 0 0
C-RD/OT 8:00 0 0 0 0 0 0 0
C-RD/OT 9:00 8 7 8 10 11 12 10
C-RD/OT 10:00 0 0 0 0 0 0 0
C-RD/OT 12:00 0 0 0 0 0 0 0
C-RD/OT 1:30 0 0 0 0 0 0 0
ML 0 0 0 0 0 0 0
VL/SIL 2 4 1 1 1 2 2
SL 1 0 1 1 1 0 0
START 20-Jan 21-Jan 22-Jan 23-Jan 24-Jan 25-Jan 26-Jan
CEBU MON TUE WED THU FRI SAT SUN
Team Edna - Esca
Adarayan, Dianne RD RD 6:00 AM 6:00 AM 6:00 AM 6:00 AM 1:30 PM
Bacalla, Catherine VL VL RD/OT 7:30 7:30 AM 7:30 AM 7:30 AM RD
Dela Cruz, Mary Ann 6:00 AM 7:30 AM RD RD 7:30 AM 7:30 AM 6:00 AM
Dumana, John Carlos 1:30 PM 1:30 PM 1:30 PM RD RD 1:30 PM 1:30 PM
Macabodbod, Evelyn 9:00 AM 9:00 AM 9:00 AM 9:00 AM RD RD/OT 9:00 9:00 AM
Mariano, Kristine 1:30 PM RD/OT 1:30 1:30 PM 1:30 PM 1:30 PM RD 7:30 AM
Santiago, Gretchen RD/OT 7:30 6:00 AM 7:30 AM 7:30 AM 1:30 PM 1:30 PM RD
END
答案 0 :(得分:1)
试试这个:
Dim objCmdSelect As New OleDbCommand("SELECT * FROM [Sheet1$B14:I24]", objConn)
假设 B14 是您的starting ROW
而 I24 是您的最后一栏/行记录。
<强>更新:强>
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
OpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv"
If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Try
Dim fi As New FileInfo(OpenFileDialog1.FileName)
Dim sConnectionStringz As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & fi.DirectoryName & ";" & "Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"""
Dim objConn As New OleDbConnection(sConnectionStringz)
objConn.Open()
Dim objAdapter1 As New OleDbDataAdapter("SELECT * FROM [Sheet1$B14:I24]", objConn)
Dim objDataset1 As New DataSet
objAdapter1.Fill(objDataset1)
DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
objConn.Close()
Finally
End Try
End If
End Sub