在线程中打开EXCEL文件的问题

youcaicai 2009-07-28 10:01:42
我在线程中打开EXCEL文件进行操作,系统报错:错误信息:System.Runtime.InteropServicesCOMException(0X800706BE)远程调用失败。(异常来自HRESULT:0X800706BE)
在Microsoft.Office.Interop.Excel.WorkBooks.Open(……)

注:括号里的太多了,我省略了。

我的源码如下,功能是导入客户发过来的预备订单,程序中有用到委托和存储过程,因问题是发生在打开EXCEL文件的操作,委托部分和存储过程部分,我就不贴上来了。在XP和2003的系统中软件运行正常,W2K的系统有进会报以上错误。


主线程

'导入数据线程
Private Sub ImportData()
Dim app As New Microsoft.Office.Interop.Excel.Application
Dim ibook As Microsoft.Office.Interop.Excel.Workbook
Dim db As New DataBase
Dim strSQL As String

'标记运行状态
Me.isRun = True

Try
Me.FaildCount = 0

Dim dg2 As New DgSetFrmStatus(AddressOf Me.SetFrmStatus)
Me.Invoke(dg2, False)

For i As Integer = 0 To DG.Rows.Count - 1
Dim mydg As dgSetPro = New dgSetPro(AddressOf Me.SetPro)
pro.Invoke(mydg, 0)
Dim dg1 As dgCurrentRow = New dgCurrentRow(AddressOf Currentrow)
DG.Invoke(dg1, i, 1)

ibook = app.Workbooks.Open(DG.Rows(i).Cells("长文件名").Value.ToString.Trim)

Dim flag As Integer
flag = Me.ReadFCFile(ibook)
ibook.Close()

'导入成功,把文件名存入数据库
If flag <> 3 Then
strSQL = "insert into FC订单文件(文件名,导入时间,用户名) values("
strSQL += "'" + DG.Rows(i).Cells("文件名").Value.ToString().Trim() + "',"
strSQL += "getdate(),"
strSQL += "'" + Me.userName + "')"
db.RunDelOrInsSQL(strSQL)

End If
'设置记录导入结果的图片
DG.Invoke(dg1, i, flag)
Next

db.Dispose()

Me.Invoke(dg2, True)
Catch ex As Exception
MessageBox.Show("错误信息;" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
Me.isRun = False
Win32.killExcel(app)
End Try

End Sub



读取文件函数

'读取FC文件
Private Function ReadFCFile(ByVal ibook As Microsoft.Office.Interop.Excel.Workbook) As Integer
Dim db As New DataBase
Dim dv As DataView
Dim strSQL As String
Dim myflag As Integer = 2

Dim rowscount As Integer
rowscount = 3
Dim s As String

Do
s = CStr(ibook.Sheets(1).cells(rowscount, 5).value)
If s = "" Then
rowscount -= 1
Exit Do
End If
rowscount += 1
Loop

Dim Itemno As String
Dim deliveryDate As String
Dim qty As Integer
Dim clientID, clientName As String
Dim flag As String

For i As Integer = 3 To rowscount
Itemno = CStr(ibook.Sheets(1).cells(i, 5).value)
Dim iat, iat2 As Integer
iat = InStr(Itemno, "-+-")
iat2 = InStr(Itemno, "Y")
If iat > 0 Then
Itemno = Microsoft.VisualBasic.Left(Itemno, iat - 1)
ElseIf iat2 > 0 Then
Itemno = Microsoft.VisualBasic.Left(Itemno, iat2 - 1)
End If
Itemno = Trim(Itemno)
deliveryDate = CStr(ibook.Sheets(1).cells(i, 8).value)
qty = CInt(ibook.Sheets(1).cells(i, 11).value)
flag = Trim(CStr(ibook.Sheets(1).cells(i, 7).value))
Dim b As Boolean = False
For h As Integer = 0 To dtClientAndFlag.Rows.Count - 1
If dtClientAndFlag.Rows(h)("标志").ToString.Trim = flag Then
clientName = dtClientAndFlag.Rows(h)("客户名").ToString.Trim
b = True
Exit For
End If
Next

If b = False Then
MessageBox.Show("读取数据出错:未能识别的标志", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
myflag = 3
Return myflag
End If

b = False

strSQL = "select 仓库号,仓库名 from 仓库清单"
dv = New DataView()
dv = db.RunSelectSQL(strSQL)

For h As Integer = 0 To dv.Table.Rows.Count - 1
If dv.Table.Rows(h)("仓库名").ToString.Trim = clientName Then
clientID = dv.Table.Rows(h)("仓库号").ToString.Trim
b = True
Exit For
End If
Next

If b = False Then
MessageBox.Show("读取数据出错:未能识别的标志", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
myflag = 3
Return myflag
End If


strSQL = "exec 保存FC订单 @clientID=" + clientID + ","
strSQL += "@itemno='" + Itemno.Trim() + "',"
strSQL += "@deliverydate='" + deliveryDate + "',"
strSQL += "@qty=" + qty.ToString() + ","
strSQL += "@username='" + Me.userName + "'"

dv = db.RunSelectSQL(strSQL)

Dim ss As String = dv.Table.Rows(0)(0).ToString.Trim()
'返回结果1 和3 表示成功
If ss <> "1" And ss <> "3" Then
Me.FaildCount += 1
strSQL = "insert into FC订单导入失败记录(客户编号,部番,纳期,受注数,操作员,操作时间,备注) "
strSQL += " values("
strSQL += clientID + ","
strSQL += "'" + Itemno.Trim() + "',"
strSQL += "'" + deliveryDate + "',"
strSQL += qty.ToString() + ","
strSQL += "'" + Me.userName.Trim() + "',"
strSQL += "getdate(),"
strSQL += "'" + dv.Table.Rows(0)(1).ToString().Trim() + "')"

db.RunDelOrInsSQL(strSQL)

End If
'设置进度
Dim ii As Single
ii = CSng(i) / CSng(rowscount) * 100
Dim idg As dgSetPro = New dgSetPro(AddressOf Me.SetPro)
pro.Invoke(idg, CInt(ii))
Next

db.Dispose()
Return myflag
End Function

...全文
383 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
youcaicai 2009-08-03
  • 打赏
  • 举报
回复
如果使用 App.Quit()的话,EXCEL进程很多时候是关闭不了的,我这里采用API函数,通过获取运行中EXEL进程的句柄来关闭EXCEL。另外,提一下,app=null;是C#的写法,在VB里应该写成app=nothing。我试过app.quit() 和app=nothing 这两种方法都不理想。使用API函数要好一点
shadowjl 2009-07-30
  • 打赏
  • 举报
回复
到哪里出错
shadowjl 2009-07-30
  • 打赏
  • 举报
回复
你应该在调用完后app.Quit()一下。最好再app=null;主动回收垃圾
youcaicai 2009-07-30
  • 打赏
  • 举报
回复
错误信息:System.Runtime.InteropServicesCOMException(0X800706BE)远程调用失败。(异常来自HRESULT:0X800706BE)
在Microsoft.Office.Interop.Excel.WorkBooks.Open(……)

也就是到程序的

ibook = app.Workbooks.Open(DG.Rows(i).Cells("长文件名").Value.ToString.Trim)
这里报错

我的问题现在基本解决了,就是在导入的过程中不能打开EXCEL,如果打开就会出错。我现在的解决方法是在导入之前,把系统中运行的所有EXCEL进程杀死,但始终觉得这种方法不太好。在XP以上的系统中不关闭EXCEL进程,不会出错。不知是否有W2K下,不关闭现有EXCEL进程的解决方法
youcaicai 2009-07-28
  • 打赏
  • 举报
回复
补充一下,程序中的DG 是DataGridView
youcaicai 2009-07-28
  • 打赏
  • 举报
回复
?
竟然没人回贴

16,718

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧