【发布时间】:2013-04-15 03:48:52
【问题描述】:
检查我的代码,我正在 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 的引用 建议我哪里做错了
【问题讨论】:
-
尝试颠倒释放对象的顺序,以便按照依赖性递减的顺序删除它们
-
我也试过了,但没用。这是我多次使用的 Range.cells 的问题
-
为什么要在
If中释放对象,为什么不在最后呢?阅读您的代码也非常困难。你能在它自己的子目录中正确缩进它吗?我的意思是R = CType(range.Cells(4, 2), Excel.Range).Value()行在end sub之后如何开始你能发布整个子,正确缩进,正如我上面建议的那样。 -
另外,我刚刚注意到您在所有
IFs的 Else 部分和其他一些地方释放对象。如果代码进入了您未发布的代码部分,会发生什么?那你是在释放对象吗? (我在代码中看不到) -
Tushar...如果您希望我帮助您,这是我对
LAST时间的请求...您能否更新您的帖子,而不是在 cmets 中输入代码?这种方式真的很难理解。