读书人

各位大大在线!等!

发布时间: 2012-05-05 17:21:10 作者: rapoo

各位大大,在线求助!急等!!!
Imports Microsoft.Office.Interop

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


Dim xlAppExcelFile As Excel.Application = New Excel.Application()
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim intColNum As Integer
Dim intRowNum As Integer
Dim fileDialog As OpenFileDialog = New OpenFileDialog()
Dim fileName As String

fileDialog.Filter = "Microsoft Excel files (*.xlsx)|*.xlsx"
If fileDialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

If fileDialog.FileName = Nothing Then
MsgBox("请选择要导入的excel文件", , "提示")
End If

' xlAppExcelFile = New Microsoft.Office.Interop.Excel.Application()

fileName = fileDialog.FileName
xlBook = xlAppExcelFile.Workbooks.Open(fileName)
xlSheet = xlBook.Sheets(1)
'DataGridView1.ColumnCount.GetType()
intRowNum = xlSheet.UsedRange.Rows.Count
intColNum = xlSheet.UsedRange.Columns.Count
intRowNum = 0
intColNum = 1
Do While intRowNum > 0
DataGridView1.Rows.Add()
'If (DataGridView1.Rows.Count > 1 And DataGridView1.Rows.Count < 2) Then
'If (DataGridView1.Rows.Count < 2) Then
intRowNum = intRowNum + 1
intColNum = intColNum + 1


DataGridView1.Rows(intColNum).Cells(0).Value = xlSheet.Cells(intRowNum, 1).Nothing
'DataGridView1.Rows(intColNum).Cells(1).Value = xlSheet.Cells(intRowNum, 2).Nothing
'DataGridView1.Rows(intColNum).Cells(2).Value = xlSheet.Cells(intRowNum, 3).Nothing
'DataGridView1.Rows(intColNum).Cells(3).Value = xlSheet.Cells(intRowNum, 4).Nothing
'DataGridView1.Rows(intColNum).Cells(4).Value = xlSheet.Cells(intRowNum, 5).Nothing
'DataGridView1.Rows(intColNum).Cells(5).Value = xlSheet.Cells(intRowNum, 6).Nothing
'DataGridView1.Rows(intColNum).Cells(6).Value = xlSheet.Cells(intRowNum, 7).Nothing
'DataGridView1.Rows(intColNum).Cells(7).Value = xlSheet.Cells(intRowNum, 8).Nothing




'End If

Loop




xlAppExcelFile = Nothing
xlSheet = Nothing
xlBook = Nothing


If DataGridView1.Rows.Count >= 1 Then
MessageBox.Show("导入成功")
Else
MessageBox.Show("导入失败")
End If
End Sub

==========================================================================================================
意图是将EXCEL 文件到入到DataGridView1 中,执行的结果是 导入失败!

[解决办法]
你这个语句的逻辑有问题吧,先是赋值为0
intRowNum = 0
然后写又大于0再循环,
Do While intRowNum > 0
那就等于是循环里面根本没执行嘛
当然导入失败了。

读书人网 >VB Dotnet

热点推荐