消除使用“2点”使用Excel互操作COM对象时 - C# [英] Eliminating use of '2 dots' when using Excel Interop Com Objects - C#

查看:184
本文介绍了消除使用“2点”使用Excel互操作COM对象时 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法释放,这是造成我的C#应用​​程序,当我试图保存并关闭通过Excel中创建的Interop Excel工作簿崩溃Excel的互操作COM对象。我觉得问题是,在某些情况下,我使用'2点'与从我读过什么是不允许的Excel互操作COM对象。我已经消除了2个点,从代码的大部分线路,但我有troulbe找出一种方法来重新创建下面的代码行,以便他们只用一个点。如果任何人有任何建议,我将不胜感激。

  =工作簿(Excel.Workbook)app.Workbooks.Open(startForm。 excelFileLocation,); 

簿=(Excel.Workbook)app.Workbooks.Add(1);

workSheet_range.Font.Color = System.Drawing.Color.FloralWhite.ToArgb();

workSheet_range.Font.Bold =字体;

workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();


解决方案

使用两个点是不是禁止,但它肯定能有性能的影响,在紧凑循环中运行时尤其如此。



每个点是一个COM呼叫到Excel库,它可以比正常CLR对象显著慢访问。一般情况下,要减少COM调用的次数,以尽可能少的。



这两个点一个通过拆分降低了两行是不会有任何冲击的除非的你重复使用相同的变量。例如,改变

  workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb(); 



  VAR内饰= workSheet_range.Interior; 
interior.Color = System.Drawing.Color.Red.ToArgb();



ZERO 对性能的影响,甚至可能被优化回到原来的单内胆如果您不要再使用内部变量。



但是,更改

  VAR字体= workSheet_range.Font; 
font.Color = System.Drawing.Color.FloralWhite.ToArgb();
font.Bold =字体;



将有 workSheet_range.Font 所以你会看到一个增量效益。



底线



我不会太在意改变的每个的二点通话,而是使用一个很好的分析工具,以确定你的代码是花费时间最多的,那么首先解决该区域。


I am having trouble releasing Excel Interop Com Objects which is causing my c# application to crash when I attempt to save and then close an Excel workbook created via Excel Interop. I feel the issue is that in some cases I am using '2 dots' with excel interop COM objects which from what I've read is not allowed. I have eliminated 2 dots from most lines of code, but I am having troulbe figuring out a way to recreate the following lines of code so that they only use one dot. If anyone has any suggestions I would greatly appreciate it.

workbook = (Excel.Workbook)app.Workbooks.Open(startForm.excelFileLocation,);

workbook = (Excel.Workbook)app.Workbooks.Add(1);

workSheet_range.Font.Color = System.Drawing.Color.FloralWhite.ToArgb();

workSheet_range.Font.Bold = font;

workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();

解决方案

Using two dots is not "disallowed" but it certainly can have performance impacts, especially when running in a tight loop.

Each "dot" is a COM call to the Excel library, which can be significantly slower than normal CLR object access. In general, you want to reduce the number of COM calls to as few as possible.

Reducing from two dots to one by splitting into two lines is not going to have any impact unless you reuse the same variable. For example, changing

workSheet_range.Interior.Color = System.Drawing.Color.Red.ToArgb();

to

var interior = workSheet_range.Interior;
interior.Color = System.Drawing.Color.Red.ToArgb();

will have ZERO impact on performance, and may even be "optimized" back to the original single-liner if you don't re-use the interior variable.

However, changing

var font = workSheet_range.Font;
font.Color = System.Drawing.Color.FloralWhite.ToArgb();
font.Bold = font;

will have one fewer call to workSheet_range.Font so you'll see an incremental benefit.

BOTTOM LINE

I wouldn't be too concerned about changing every two-dot call but instead use a good profiling tool to determine where your code is spending the most time, then tackle that area first.

这篇关于消除使用“2点”使用Excel互操作COM对象时 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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