检查我的代码我在vb.net中自动从excel上传 这是我的代码
import statement
Imports Excel = Microsoft.Office.Interop.Excel
releaseObject函数
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 建议我在哪里做错了
答案 0 :(得分:2)
很少有事情
A)在编码方面,我一直坚信1个入口点和1个退出点。在您的代码中,您有几个退出点。您的代码现在看起来像这样
If A = B Then
'
'~~> Rest of the code
'
'release object
Exit Sub
Else
End If
If C = D Then
Else
'
'~~> Rest of the code
'
'release object
Exit Sub
End If
releaseObject(xlrange)
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
如果我们正确关闭对象,则很难跟踪。最好的方法是组合它们,然后在离开sub之前释放对象。您的上述代码可以重写为
If A = B Then
'
'~~> Rest of the code
'
Else
If C = D Then
Else
'
'~~> Rest of the code
'
End If
End If
releaseObject(xlrange)
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
B)正如亚历克斯和克里斯所提到的,以正确的顺序释放你的对象(你最终在做的但不是在代码的中间)。喜欢这个
releaseObject(xlrange)
releaseObject(xlWorkSheet)
releaseObject(xlWorkBook)
releaseObject(xlApp)
如果你加入了我的第一个建议,那么你不必在任何地方维护发布对象代码,只是最后。
C)我看到您已将Excel对象声明为PUBLIC
您是否在ButtonUpload_Click
以外的其他地方使用它们
Form Load
,请确保您正确发布它们ButtonUpload_Click
如果您使用上述建议,那么在使用TWO DOT规则时,我发现在发布对象时没有问题。
休息我发现现有代码没有任何问题。它对我来说很好。
答案 1 :(得分:0)
Thanx一切都解决了
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
Catch ex As Exception
Finally
obj = Nothing
End Try
End Sub