检查我的代码我正在从 vb.net 中的 excel 自动上传这是我的代码
进口声明
Imports Excel = Microsoft.Office.Interop.Excel
释放对象函数
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
按钮点击事件
Protected Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonUpload.Click
If IsPostBack Then
Dim xlApp As Excel.Application
Dim xlWorkBooks As Excel.Workbooks
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim range As Excel.Range
connStr = ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
conn = New SqlConnection(connStr)
dat = System.DateTime.Now
Filepath = Path.GetFullPath(fileUploadBOM.PostedFile.FileName)
sFileName = Path.GetFileName(fileUploadBOM.PostedFile.FileName)
FileFormat = Path.GetExtension(Filepath)
v_bom_type = "IMPORT"
If FileFormat.Equals(".xls") Or FileFormat.Equals(".xlsx") Then
System.IO.File.Delete("C:\inetpub\wwwroot\Uploads\" & sFileName)
fileUploadBOM.PostedFile.SaveAs(sFileDir + sFileName)
Try
xlApp = New Excel.ApplicationClass
xlWorkBooks = xlApp.Workbooks
xlWorkBook = xlWorkBooks.Open(sFileDir + sFileName)
xlWorkSheet = xlWorkBook.Worksheets("BOM for Import")
range = xlWorkSheet.Cells
Catch ex As Exception
releaseObject(xlApp)
'GetWindowThreadProcessId(xlApp.Hwnd, processID)
'release(processID)
End Try
一些如果条件像..
Dim R As String
R = CType(range.Cells(4, 2), Excel.Range).Value()
If Not R Is Nothing Then
If (R.Trim = "") Then
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "SetStatusText", "<script type='text/javascript'> alert('please enter the OEM for logistics cost'); </script>")
releaseObject(range)
releaseObject(xlWorkSheet)
xlWorkBook.Save()
xlWorkBook.Close()
releaseObject(xlWorkBook)
xlWorkBooks.Close()
releaseObject(xlWorkBooks)
xlApp.Quit()
releaseObject(xlApp)
'GetWindowThreadProcessId(xlApp.Hwnd, processID)
'MsgBox(processID)
'release(processID)
Exit Sub
Else
v_logiccost = CType(range.Cells(4, 2), Excel.Range).Value()
End If
Else
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "SetStatusText", "<script type='text/javascript'> alert('please enter the OEM for logistics cost'); </script>")
releaseObject(range)
releaseObject(xlWorkSheet)
xlWorkBook.Save()
xlWorkBook.Close()
releaseObject(xlWorkBook)
xlWorkBooks.Close()
releaseObject(xlWorkBooks)
xlApp.Quit()
releaseObject(xlApp)
'GetWindowThreadProcessId(xlApp.Hwnd, processID)
'MsgBox(processID)
'release(processID)
Exit Sub
End If
'' No of yrs of support
Dim P As String
P = CType(range.Cells(6, 2), Excel.Range).Value()
releaseObject(range)
If Not P Is Nothing Then
If (IsNumeric(P) = True) Then
v_year = P
Else
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "SetStatusText", "<script type='text/javascript'> alert('No Of Years Support Should Be A Number.'); </script>")
releaseObject(range)
releaseObject(xlWorkSheet)
xlWorkBook.Save()
xlWorkBook.Close()
releaseObject(xlWorkBook)
xlWorkBooks.Close()
releaseObject(xlWorkBooks)
xlApp.Quit()
releaseObject(xlApp)
'GetWindowThreadProcessId(xlApp.Hwnd, processID)
'MsgBox(processID)
'release(processID)
Exit Sub
End If
End If
最后调用释放的函数
releaseObject(range)
releaseObject(xlWorkSheet)
xlWorkBook.Save()
xlWorkBook.Close()
releaseObject(xlWorkBook)
xlWorkBooks.Close()
releaseObject(xlWorkBooks)
xlApp.Quit()
releaseObject(xlApp)
这是我的整个代码我想问题出在 Range.cells 对 RCW 的引用建议我在哪里做错了