Excel 导入时出错
我正在尝试将大约 1500 个 Excel 文件导入到我的系统中。 该代码循环运行,我可以打开并导入大约 600 个 Excel 文件。之后,我收到一条错误消息,例如:在第 55 行调用外部对象函数打开时出错.....
我真的遇到了这个问题,如果有人可以提供帮助,我将不胜感激。
回复评论中发布的代码:
For ll_LoopCnt = 1 To Dw_1.rowcount( )
Ls_File_Name = Dw_1.getitemstring( ll_LoopCnt, "file_name")
Ls_Path =Dw_1.getitemstring( ll_LoopCnt, "file_path")
ll_Sr_No= Dw_1.getitemNumber( ll_LoopCnt, "sr_no")
ldt_File_Date= Dw_1.getitemDateTime( ll_LoopCnt, "file_date")
Excel.Application.DisplayAlerts = "False"
Excel.WorkBooks.Open( Ls_Path )
Excel.Application.Visible = False
Excel.windowstate = 2 // 1 : Normal, 2 : Minimize, 3 : Maximize
Excel.Application.CutCopyMode = False
Lb_sheet_rtn = excel.worksheets(7).Activate
Ls_ClipBoard = clipboard()
Excel.Application.ActiveWorkbook.Save()
Excel.Worksheets(7).UsedRange.Copy
ll_cnt = ds_1.importclipboard()
IF ll_cnt <= 1 THEN
Messagebox("Error", "Could not find.")
Else
Dw_1.Scrolltorow( ll_LoopCnt )
Dw_1.SetItem( ll_LoopCnt, "status", 'Success')
For ll_Inner_LoopCnt = 1 To Ds_1.RowCount( )
Ds_1.Object.file_path[ll_Inner_LoopCnt] = Ls_Path
Ds_1.Object.file_name[ll_Inner_LoopCnt] = Ls_File_Name
Ds_1.Object.file_sr_no[ll_Inner_LoopCnt] = ll_Sr_No
Ds_1.Object.file_date[ll_Inner_LoopCnt] = ldt_File_Date
Next
END IF
Clipboard(ls_ClipBoard)
Ds_1.Reset( ) //Reset the data store
Excel.Application.ActiveWorkbook.Save()
Excel.Application.ActiveWorkbook.Close(False);
Excel.Application.Quit
Excel.Application.CutCopyMode = False
IF ll_LoopCnt = ll_Excel_Cnt Then //--->> After 100 files reset the memmory
ll_Excel_Cnt = ll_LoopCnt + 100
Excel.DisConnectObject()
DESTROY excel
DESTROY TEst_Excel
GarbageCollect ( )
Excel = Create OLEObject
Test_Excel = Create OLEObject
Li_rtn = excel.ConnectToNewObject("excel.application")
IF li_rtn <> 0 THEN
MessageBox('Excel error','can not run Excel Program')
DESTROY Excel
RETURN 0
END IF
End IF
Next
Excel.displayalerts = False
Excel.Application.Quit
Excel.displayalerts = True
Excel.DisConnectObject()
DESTROY Excel
DESTROY Test_Excel /* This is the code i written i dont think the OLE is crashing i think the connnectto the OLE is getting lost after some time, but stile its going fine for almost 600 records.. */
I am trying to import around 1500 Excel files to my system.
The code is working in a loop and I am able open and import around 600 Excel files. After that I am getting an error message like: Error calling external object function open at line 55.....
I really stuck with this issue, if anyone can help that will be grateful.
Code posted in reply comments:
For ll_LoopCnt = 1 To Dw_1.rowcount( )
Ls_File_Name = Dw_1.getitemstring( ll_LoopCnt, "file_name")
Ls_Path =Dw_1.getitemstring( ll_LoopCnt, "file_path")
ll_Sr_No= Dw_1.getitemNumber( ll_LoopCnt, "sr_no")
ldt_File_Date= Dw_1.getitemDateTime( ll_LoopCnt, "file_date")
Excel.Application.DisplayAlerts = "False"
Excel.WorkBooks.Open( Ls_Path )
Excel.Application.Visible = False
Excel.windowstate = 2 // 1 : Normal, 2 : Minimize, 3 : Maximize
Excel.Application.CutCopyMode = False
Lb_sheet_rtn = excel.worksheets(7).Activate
Ls_ClipBoard = clipboard()
Excel.Application.ActiveWorkbook.Save()
Excel.Worksheets(7).UsedRange.Copy
ll_cnt = ds_1.importclipboard()
IF ll_cnt <= 1 THEN
Messagebox("Error", "Could not find.")
Else
Dw_1.Scrolltorow( ll_LoopCnt )
Dw_1.SetItem( ll_LoopCnt, "status", 'Success')
For ll_Inner_LoopCnt = 1 To Ds_1.RowCount( )
Ds_1.Object.file_path[ll_Inner_LoopCnt] = Ls_Path
Ds_1.Object.file_name[ll_Inner_LoopCnt] = Ls_File_Name
Ds_1.Object.file_sr_no[ll_Inner_LoopCnt] = ll_Sr_No
Ds_1.Object.file_date[ll_Inner_LoopCnt] = ldt_File_Date
Next
END IF
Clipboard(ls_ClipBoard)
Ds_1.Reset( ) //Reset the data store
Excel.Application.ActiveWorkbook.Save()
Excel.Application.ActiveWorkbook.Close(False);
Excel.Application.Quit
Excel.Application.CutCopyMode = False
IF ll_LoopCnt = ll_Excel_Cnt Then //--->> After 100 files reset the memmory
ll_Excel_Cnt = ll_LoopCnt + 100
Excel.DisConnectObject()
DESTROY excel
DESTROY TEst_Excel
GarbageCollect ( )
Excel = Create OLEObject
Test_Excel = Create OLEObject
Li_rtn = excel.ConnectToNewObject("excel.application")
IF li_rtn <> 0 THEN
MessageBox('Excel error','can not run Excel Program')
DESTROY Excel
RETURN 0
END IF
End IF
Next
Excel.displayalerts = False
Excel.Application.Quit
Excel.displayalerts = True
Excel.DisConnectObject()
DESTROY Excel
DESTROY Test_Excel /* This is the code i written i dont think the OLE is crashing i think the connnectto the OLE is getting lost after some time, but stile its going fine for almost 600 records.. */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看代码行会有所帮助,但此错误消息通常(在 OLE 上下文中,我猜就是这种情况)来自 PowerBuilder 进行 OLE 调用,而 OLE 主机拒绝了该调用。根据您提供的信息,无法判断 OLE 主机是否已崩溃并且不再响应,或者您是否已使 OLE 主机进入这些功能不再适用的状态,或者 OLE 对象是否已经失效了,或者什么的。
如果是我,并且这种情况持续发生,我会在调试器中运行应用程序以到达错误即将发生的状态(您可以在断点中设置高级属性,以便每次都不会激活断点)通过)并尝试询问 OLE 对象。我希望您还必须添加一些测试代码,因为我不确定您想要测试的所有内容都可供调试器使用。
2 月 21 日新
我还会更改正在处理的文件集,以便我可以判断崩溃的关键是特定文件,还是处理的文件数量。例如,如果您删除了前 100 个文件,那么它是否仍然会在第 600 个文件(相同数量)或第 500 个文件(相同文件)时崩溃?
一种可能性是您的内存不足。 OLE 引用(属性访问、方法调用)中的每个“点”都会在内存中创建一个对象,该对象在垃圾收集之前不会被销毁。您发布的代码片段永远不会进入调用 GarbageCollect() 的块(ll_Excel_Cnt 永远不会初始化),因此您可能需要确保该部分正常工作。我也会摆脱不必要的电话。例如,您有多个调用在循环内维护 Excel 的状态(例如 Excel.Application.Visible),而这些调用只需要调用一次。从代码片段中我也不清楚为什么你需要调用 Save() ,无论何时;这也可能是消耗性的。这种清理还应该使您的代码运行得更快。
祝你好运,
特里
Seeing the line of code would help, but this error message typically (in the context of OLE, which I'm guessing is the case here) comes from PowerBuilder making an OLE call which the OLE host rejects. From the information you've supplied, it's impossible to tell if the OLE host has crashed and isn't responding anymore, or if you've got the OLE host into a state where these functions are no longer applicable, or if the OLE object has become invalid, or what.
If it were me, and it was happening consistently, I'd run the app in the debugger to get to the state where the error is about to happen (you can set advanced attributes in breakpoints to not have a breakpoint activate every time it is passed) and try interrogating the OLE objects. I'd expect you'd also have to throw in some test code, since I'm not confident everything you'd want to test would be available to the debugger.
New Feb 21
I'd also change the set of files being processed, so that I could tell if the key to the crash is a specific file, or the quantity of files processed. For example, if you get rid of the first 100 files, does it still crash on the 600th file (same quantity) or the 500th file (same file)?
One possibility is that you're running out of memory. Each "dot" in an OLE reference (attribute access, method call) creates an object in memory that isn't destroyed until the garbage collect. The code clip you've posted will never enter the block where the GarbageCollect() is called (ll_Excel_Cnt is never initialized), so you might want to make sure that part is working. I'd also get rid of unnecessary calls. For example, you've got several calls that maintain the state of Excel within the loop (e.g. Excel.Application.Visible), when they only need to be called once. I'm also not clear from the code clip why you'd need to call a Save(), either time; this could be expendable as well. This clean up should also make your code run faster.
Good luck,
Terry
在第 30 行左右,
您不应该在那里调用
Excel.Application.Quit
。另外,我总是建议将任何使用 OLE 的内容放入 Try..Catch 块中并捕获 OleRuntimeError 和 RuntimeError。Around line 30 you have
You shouldn't call
Excel.Application.Quit
there. Also, I always recommend to put anything that uses OLE inside a Try..Catch block and catch OleRuntimeError and RuntimeError.