简体   繁体   English

如何在 Excel 中使用 C# 更改数组内容的 Font.Bold?

[英]How to change Font.Bold of an array content with C# in Excel?

With Excel VBA I can write/set array of range for example Range("A1:B2, C2:D3").Font.Bold = True, so it will make A1, A2, B1, B2, C2, C3, D2 and D3 Font->Bold.使用 Excel VBA,我可以编写/设置范围数组,例如 Range("A1:B2, C2:D3").Font.Bold = True,因此它将使 A1、A2、B1、B2、C2、C3、D2 和D3 字体->粗体。

But then I try this with C# i get "Exception from HRESULT:0x800A03EC", and I can't figure why?但是后来我用 C# 尝试这个,我得到“来自 HRESULT:0x800A03EC 的异常”,我不知道为什么?

I know range has some limits but considering speed and other stuff its best way to set Font.Bold as range at once rather make separate calls on each cell.我知道范围有一些限制,但考虑到速度和其他因素,最好将 Font.Bold 设置为范围,而不是对每个单元格进行单独调用。

Hope someone can help me :)希望可以有人帮帮我 :)

A range can be part of Range() parameters.范围可以是 Range() 参数的一部分。 Please give a try to below style -请尝试以下风格 -

Excel.Range range1 = _with.Range("A1:B2");
Excel.Range range2 = _with.Range("C2:D3");

_with.Range(range1,range2).Font.Bold = true;

Some speculation here, short of seeing your actual C#, but I believe this code will work:这里有一些猜测,没有看到您实际的 C#,但我相信这段代码会起作用:

Excel.Worksheet sheet;
sheet.Range["A1:B2,C2:D3"].Font.Bold = true;

So my only guess is that you used parentheses () instead of brackets [].所以我唯一的猜测是你使用了括号 () 而不是括号 []。

In VBA, function parameters and indexers both use parentheses.在 VBA 中,函数参数和索引器都使用括号。 In C#, function parameters use parentheses, but to invoke an indexer you need to use the square brackets.在 C# 中,函数参数使用括号,但要调用索引器,您需要使用方括号。

Does the code above give the same error?上面的代码是否给出了同样的错误?

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

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