VB.net中excel导入数据到Datagridview
请各位高手帮我看看。PrivateSubButton5_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs...
请各位高手帮我看看。
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
Dim FileName As String
fileDialog.Filter = "Microsoft Excel files (*.xls)|*.xls"
If fileDialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
If fileDialog.FileName = Nothing Then
MsgBox("请选择要导入的excel文件", , "提示")
End If
FileName = fileDialog.FileName
xlBook = excel.Application.Workbooks.Open(FileName)
xlSheet = xlBook.Application.Worksheets(1)
Dim col As Integer = 0
Dim i As Integer = 2
Me.dataGridView1.Rows.Clear()
Do While i < 5
dataGridView1.Rows.Add()
dataGridView1.Rows(col).Cells(0).Value = xlSheet.Cells(i, 1).ToString
dataGridView1.Rows(col).Cells(1).Value = xlSheet.Cells(i, 2).ToString
dataGridView1.Rows(col).Cells(2).Value = xlSheet.Cells(i, 3).ToString
i = i + 1
col = col + 1
Loop
excel.ActiveWorkbook.Close(False)
xlSheet = Nothing
xlBook = Nothing
excel = Nothing
If dataGridView1.Rows.Count >= 1 Then
MessageBox.Show("导入成功")
Else
MessageBox.Show("导入失败")
End If
End Sub
运行结果是,gridviewview中的数据全部显示“System.__ComObject”,我实在找不出来原因,希望各位能帮帮忙,分有点少,希望不要嫌弃哈。 展开
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim excel As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
Dim FileName As String
fileDialog.Filter = "Microsoft Excel files (*.xls)|*.xls"
If fileDialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
If fileDialog.FileName = Nothing Then
MsgBox("请选择要导入的excel文件", , "提示")
End If
FileName = fileDialog.FileName
xlBook = excel.Application.Workbooks.Open(FileName)
xlSheet = xlBook.Application.Worksheets(1)
Dim col As Integer = 0
Dim i As Integer = 2
Me.dataGridView1.Rows.Clear()
Do While i < 5
dataGridView1.Rows.Add()
dataGridView1.Rows(col).Cells(0).Value = xlSheet.Cells(i, 1).ToString
dataGridView1.Rows(col).Cells(1).Value = xlSheet.Cells(i, 2).ToString
dataGridView1.Rows(col).Cells(2).Value = xlSheet.Cells(i, 3).ToString
i = i + 1
col = col + 1
Loop
excel.ActiveWorkbook.Close(False)
xlSheet = Nothing
xlBook = Nothing
excel = Nothing
If dataGridView1.Rows.Count >= 1 Then
MessageBox.Show("导入成功")
Else
MessageBox.Show("导入失败")
End If
End Sub
运行结果是,gridviewview中的数据全部显示“System.__ComObject”,我实在找不出来原因,希望各位能帮帮忙,分有点少,希望不要嫌弃哈。 展开
2个回答
2009-07-23
展开全部
xlsheet.cells(1,2)tostring
改为xlsheet.cells(1,2).value
其他也是
改为xlsheet.cells(1,2).value
其他也是
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询