简体   繁体   English

Excel Interop:改变大量单元格中文本部分颜色的最快方法

[英]Excel Interop: Fastest way to change color of portions of text in a huge range of cells

There some articles about the fastest way to write data using Excel interop assigning directly an Array of data to the value of the range. 有些文章介绍了使用Excel互操作直接将数据数组分配给范围值的最快方式。 Like: 喜欢:

string[,] multidimensionalArrayData = new string[200, 3];
    // (...) Fill multidimensionalArrayData with your data
dataSheet.Range["A1:C200"].Value = multidimensionalArrayData;

There are also some articles about how to change the Font color of a specific portion of text, for example (VB this time): 还有一些关于如何更改文本特定部分的字体颜色的文章,例如(这次是VB):

With ActiveCell.Characters(Start:=3, Length:=3).Font
    .Name = "Arial"
    .FontStyle = "Regular"
    .Size = 10
    .Color = "Red"
    .ThemeFont = xlThemeFontNone
End With

The question now is, what would be the fastest way to change the color of specific portions of text for thousands of cells? 现在的问题是,改变数千个单元格的特定文本部分颜色的最快方法是什么? Currently, in my C# code, I have to do it cell by cell, with a horrible performance hit. 目前,在我的C#代码中,我必须逐个单元地进行,性能受到惊吓。 Is there a way to fill an array of 'Characters' objects in C# and pass that array to a range in one go? 有没有办法在C#中填充'Characters'对象数组并将该数组一次性传递给一个范围? Any other solutions? 还有其他方法吗?

Operations using Excel Interop is always slower, more memory consume, not recommended. 使用Excel Interop的操作总是更慢,更多的内存消耗,不推荐。

Below are some of the Open Source, yet faster way to do what you need and without the need of installation of Excel: 下面是一些开源,但更快的方式来做你需要的,而不需要安装Excel:

http://closedxml.codeplex.com/ http://closedxml.codeplex.com/
http://epplus.codeplex.com/ http://epplus.codeplex.com/
http://code.google.com/p/excellibrary/ http://code.google.com/p/excellibrary/
http://npoi.codeplex.com http://npoi.codeplex.com

Released by Microsoft: Open XML 2.0 由Microsoft发布: Open XML 2.0
Another faster way to do what you want. 另一种更快的方式来做你想要的。
Download: http://www.microsoft.com/en-us/download/details.aspx?id=5124 下载: http//www.microsoft.com/en-us/download/details.aspx?id = 5124
Introduction: http://blog.stuartwhiteford.com/?p=49 简介: http//blog.stuartwhiteford.com/?p = 49

I am not sure that you will gain a large performance increase with this code (as hacking into individual cells is a time consuming operation). 我不确定你会通过这个代码获得大幅度的性能提升(因为黑客入侵单个单元是一项耗时的操作)。 Characters is apparently a range property, so you don't have to specify a particular cell. 字符显然是范围属性,因此您不必指定特定单元格。

You can use the following to avoid looping 您可以使用以下内容来避免循环

With Range("A1:A3").Characters(1, 2).Font
    .Name = "Arial"
    .Size = 6
End With

Great question.... I have had a number of use cases for formatting partial cells but it is a pain to do manually. 很棒的问题....我有一些用于格式化部分单元格的用例,但手动操作很痛苦。

After some months of struggling with different libraries for Excel writing, I must say that I finally went back to Excel Interop, believe it or not. 经过几个月与Excel不同的Excel写作斗争,我必须说我终于回到了Excel Interop,信不信由你。

I tried ClosedXML, EPPlus and SpreadsheetLite with all the hassle of learning each library and adapting my code. 我尝试了ClosedXML,EPPlus和SpreadsheetLite,学习了每个库并调整了我的代码。 After a long journey of turnarounds, bugs, sometimes generating an Excel that could not be open, sometimes having horrible performance issues or out of memory crashes here and there, I decided to try with Excel Interop again and was surprised to see that it was faster and without errors for my scenario. 经过长途跋涉,错误,有时会产生一个无法打开的Excel,有时会出现可怕的性能问题或内存崩溃,我决定再次尝试使用Excel Interop,并惊讶地发现它更快我的方案没有错误。

If you want to try the libraries in mjb answer, do it, but I recommend you to write your Interop code as best as you can first, it might surprise you how fast it is at the end if you do it right. 如果你想尝试使用mjb中的库,那就去做吧,但是我建议你尽可能地编写你的Interop代码,如果你做得对,它可能会让你感到惊讶。

The best option for my case was: 我案例的最佳选择是:

Use Excel Interop. 使用Excel Interop。 Write all Excel data at once using a 2 dimensional array, then format the rich text of those cells that needed it in a second pass. 使用二维数组一次写入所有Excel数据,然后在第二遍中格式化需要它的那些单元格的富文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM