我在VB.net中将数据从excel导入datagridview时遇到了问题。 我不知道或从哪里开始寻找问题。情况如下: 我有一个excel文件,
Column A(General) Column B(Time)
Adarayan, Dianne 6:00 AM
Bacalla, Cruz 1:30 PM
Mariano, Kevin RD
Roses, Alice VL
Ligris, Paul RD/OT 7:30
datagridview中的输出/显示:
Team Member TimeSchedule
Adarayan, Dianne 0.25
Bacalla, Cruz 0.5625
Mariano, Kevin RD
Roses, Alice VL
Ligris, Paul RD/OT 7:30
我不知道为什么会发生这种情况,当字符串就像显示浮动的前两行一样。 请帮帮我们。
这是我将数据从excel导入datagridview的代码。
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
OpenFileDialog1.Filter = "Excel Files 2007 (*.xlsx)|*.xlsx|Excel Files 1997-2003 (*.xls)|*.xls"
If OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Try
xlsApp = New Excel.Application
xlsWorkBook = xlsApp.Workbooks.Open(OpenFileDialog1.FileName)
xlsWorkSheet = xlsWorkBook.Worksheets("WORK SCHEDULE")
'unprotect file
'Get last row used
'lastUsedRow = xlsWorkSheet.Range("I" & xlsWorkSheet.Rows.Count).End(Excel.XlDirection.xlUp).Row
'Get last column
'lastUsedCol = xlsWorkSheet.Cells(1, xlsWorkSheet.Columns.Count).End(Excel.XlDirection.xlToLeft).Column
Dim fi As New FileInfo(OpenFileDialog1.FileName)
Dim sConnectionStringz As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & OpenFileDialog1.FileName & ";" & "Extended Properties=""Excel 8.0;HDR=NO;IMEX=1"""
Dim objConn As New OleDbConnection(sConnectionStringz)
objConn.Open()
Dim x As Integer = 0
Dim start As Integer
Dim ends As Integer
For Each c In xlsWorkSheet.Range("B1:B300").Cells
If IsNothing(c.value) Then
x = x + 1
ElseIf Not IsNothing(c.Value) Then
x = x + 1
If c.value.ToString.ToUpper <> "START" And c.value.ToString.ToUpper <> "END" Then
ElseIf c.value.ToString.ToUpper = "START" Then
start = x
ElseIf c.value.ToString.ToUpper = "END" Then
ends = x
End If
End If
Next
Dim campaign = start + 1
For Each c In xlsWorkSheet.Range("B" & campaign & ":B" & campaign).Cells
If Not IsNothing(c.value) Then
campaignName = c.value.ToString
End If
Next
Dim objAdapter1 As New OleDbDataAdapter("SELECT * FROM [WORK SCHEDULE$B" & start & ":I" & ends & "]", objConn)
Dim objDataset1 As New DataSet
objAdapter1.Fill(objDataset1)
DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Team Members"
.Columns(1).HeaderCell.Value = "Monday"
.Columns(2).HeaderCell.Value = "Tuesday"
.Columns(3).HeaderCell.Value = "Wednesday"
.Columns(4).HeaderCell.Value = "Thursday"
.Columns(5).HeaderCell.Value = "Friday"
.Columns(6).HeaderCell.Value = "Saturday"
.Columns(7).HeaderCell.Value = "Sunday"
End With
objConn.Close()
Finally
End Try
End If
答案 0 :(得分:0)
您的时间正在转换为十进制。您需要在一段时间内阅读它们,但这会导致列值为文本的问题,例如。 RD,VL,RD / OT 7:30 我建议你的excel电子表格将这些数据分成两列,例如Type和Time,这样你就可以......
Column A(General) Column B(Type) Column C(Time)
Adarayan, Dianne 6:00 AM
Bacalla, Cruz 1:30 PM
Mariano, Kevin RD
Roses, Alice VL
Ligris, Paul RD/OT 7:30 AM