使用COM Interop的c#backgroundworker线程编写多个excel .xlsx文件 [英] c# backgroundworker thread using COM Interop writing multiple excel .xlsx files

查看:107
本文介绍了使用COM Interop的c#backgroundworker线程编写多个excel .xlsx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要问如何从c#backgroundworker线程上的数组列表有问题地创建多个新的Excel电子表格文件

目前,我有一个函数,它通过每个循环和:

xlApp =新的Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet =(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

然后处理excel对象

通过在backgroundworker线程中执行此操作,将导致各种问题,从空白工作表到多个工作表上的数据拆分.

希望获得有关此操作的任何反馈,基本上想在操作完成时显示选取框进度条,因此如果不使用线程,则需要使用backgroundworker线程,否则进度条将不会移动,导致GUI线程上所有处理的过程

I need to ask how to create multiple new excel spreadsheet files problematically from an arraylist on the c# backgroundworker thread

currently I have a function that goes through a for each loop and :

xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

then it disposes of the excel object

by doing this in a backgroundworker thread, it causes all sorts of problems from blank worksheets to data split on multiple worksheets.

Would appreciate any feedback on how to go about this, basically want to show a marquee progressbar when the operation is being done, so I need to use the backgroundworker thread else if I don''t use threading, the progressbar does not move, cause of all processing on the main GUI thread

推荐答案

感谢Dave,

我设法使用Excel OLEDB使用后台工作程序线程进行写操作,然后与互操作一起返回以进行格式化(这是不占用大量CPU/进程的方式!)

我的下一个难题是如何确定安装了哪个Office 2010 32/64位版本,然后在启动时将cpu版本调整为该版本,以便Ace OLEDB驱动程序不会失败!

谢谢,

Jase
Thanks to Dave,

I managed to use Excel OLEDB to write using background worker thread then go back with interop to format (which is way less cpu/process intensive !)

My next dilemma is how to determine which Office 2010 32/64 bit version is installed then adjust the cpu build on start up to that version so the Ace OLEDB driver does not fail !

Thanks,

Jase


考虑到Excel对象模型根本不是线程安全的,我会说您不能做自己想做的事情. Excel无法从后台线程运行.它在主线程上,或者您遇到描述的问题.

不过,我只与Excel进行过一次互操作.

我不敢相信我在说这句话,但是您可以将Application.DoEvents放在ProgressBar更新代码中.只是非常非常小心,不要在操作过程中给用户任何可点击的东西.如果您不这样做,那么启用这些按钮或其他按钮仍将起作用!不要陷入重新进入陷阱,因为单击按钮可以启动长时间运行的工作,并且您没有禁用按钮.
Considering the Excel object model is not at all thread-safe I would say that you cannot do what you want. Excel doesn''t work from background threads. It''s on the main thread or you run into the problems you''ve described.

Though, I''ve only done interop with Excel once.

I cant believe I''m saying this, but you could put a Application.DoEvents in the ProgressBar update code. Just be very very careful and not give the user anything to click on during this operation. If you don''t, those buttons or whatnot will still work if they are enabled! Don''t get caught in a re-entrancy trap where a button click kicks off long running work and you didn''t disable the button.


这篇关于使用COM Interop的c#backgroundworker线程编写多个excel .xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆