我是vb.net的新手,我在学校创建了论文项目。我想从我的excel文件中获取数据,其中单元格是合并的,并且它继续弹出类型' Range'输入' String'无效。这是我复制并归功于他们的代码:
Try
con.Open()
workbook = app.Workbooks.Open(fileOpener.SelectedPath + "\ahrmt.xlsx")
worksheet = workbook.Worksheets("Sheet1")
curbook = 0
Me.Text = String.Format("{0:F0}%", ((curbook / books) * 100)) + " of records has been imported."
Dim cmd As New MySqlCommand
Dim maxrow As Integer = 9
Dim noRecs, AYear As Integer
Dim semester, course As String
Me.lblWait.Visible = True
'=====================================
Dim str As String
str = worksheet.Range("A6").Text
If str.Contains("FIRST SEMESTER") = True Then
semester = "First Semester"
Else
semester = "Second Semester"
End If
Dim exAY = Regex.Replace(worksheet.Cells(6, 1), "\D", "")
AYear = exAY
Dim AcadY As New System.Text.StringBuilder()
For i As Integer = 0 To exAY.Length - 1
AcadY.Append(exAY(i))
If i Mod 4 = 3 AndAlso i > 0 AndAlso i < exAY.Length - 1 Then
AcadY.Append(" - ")
End If
Next
course = "Associate in Hotel and Restaurant Management Technology"
'=======================================
'--------to get the total rows
For x As Integer = 9 To worksheet.Rows.Count
If worksheet.Cells(x, 2).Value = Nothing Then
Exit For
Else
maxrow += 1
End If
Next
'----------for inserting records
For i As Integer = 9 To worksheet.Rows.Count
If worksheet.Cells(i, 2).Value = Nothing Then
Exit For
Else
Me.ProgressBar1.Visible = True
Me.ProgressBar1.Value += 1
Me.ProgressBar1.Maximum = maxrow - 9
Me.lblImport.Text = String.Format("{0:F0}%", ((ProgressBar1.Value / ProgressBar1.Maximum) * 100))
cmd.Connection = con
cmd.CommandText = "INSERT INTO tblsif(IDNo, Status, FName, MName, LName, Gender, YearLevel, Semester, AcadYear, PresCourse) " & _
"VALUES('" & worksheet.Cells(i, 2).Value & "','" & enrolledStat & "','" & worksheet.Cells(i, 5).Value & "','" & worksheet.Cells(i, 6).Value & "'," & _
"'" & worksheet.Cells(i, 4).Value & "','" & worksheet.Cells(i, 10).Value & "','" & worksheet.Cells(i, 1).Value & "','" & semester & "','" & AcadY & "','" & course.ToString & "')"
cmd.ExecuteNonQuery()
noRecs += 1
Me.lblTotalRec.Text = "Importing " + noRecs.ToString + " records from AHRMT Course."
End If
Next
'workbook.Save()
workbook.Close()
app.Quit()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Me.ProgressBar1.Value = 0
Me.ProgressBar1.Visible = False
Me.lblTotalRec.Text = Nothing
Me.lblImport.Text = Nothing
con.Dispose()
con.Close()
curbook = 1
Me.Text = String.Format("{0:F0}%", ((curbook / books) * 100)) + " of records has been imported."
希望你能帮助我。