且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用C#突出Excel中的数据范围是多少?

更新时间:2023-11-08 16:39:16

在VBA有一个RefEdit控件,可以让你做到这一点,并没有对如何在这里使用它的详细解说:的如何使用RefEdit控件使用用户窗体

In VBA there is a RefEdit control that can allow you to do this, and there is a detailed walkthrough on how to use it here: How to Use the RefEdit Control with a UserForm.

有没有内置的控制,可以让你做到这一点在C#中,很遗憾。你既可以使用VBA作为前端 - 我很怀疑你想做的事 - 或者你可以创建一个使用C#RefEdit控件等价的。对于如何做到这一点的文章,以及全功能的C#code,下面的例子:的如何通过Gabhan贝瑞为codeA RefEdit控件

There is no built-in control that can allow you to do this in C#, unfortunately. You could either use VBA as your front-end -- which I very much doubt that you'd want to do -- or you can create a RefEdit control equivalent using C#. There is an article on how to do this, along with a fully-functional C# code example, here: How to Code a RefEdit control by Gabhan Berry.

基于zzandy的评论更新:

其实这个问题是关于如何
  突出选择范围,而不是如何
  选择范围。输入框
  (csharp-examples.net/inputbox)是
  可从C#和允许选择
  范围。

"Actually the question is on how to highlight selected ranges, not on how to select range. The InputBox (csharp-examples.net/inputbox) is available from C# and allows to select ranges."

获取范围在多色彩突出显示将是不可能的。那种突出的仅在工作表上创建公式时,而不是在一个选择的范围用于任何其它目的发生在Excel中。只有这样,才能创建需要的子类,或挂钩的英雄量,我不会推荐它。

Getting the ranges to highlight in multi-colors will not be possible. That kind of highlighting only occurs in Excel when creating formulas on the worksheet, not when one is selecting ranges for any other purpose. The only way to create that would require an heroic amount of subclassing, or hooking, and I would not recommend it.

一些解决方案,但是,将让你至少有周围的选择选取框乐队(又名行进中的蚂蚁)。如果您使用VBA作为前端,你可以利用内置的 RefEdit控件的,我上面提到的,它不把帐篷周围频段的选择的范围。

Some solutions, however, will allow you to at least have a marquee band around the selection (aka "marching ants"). If you use VBA as a front end, you could make use of the built-in RefEdit control, that I mentioned above, which does put the marquee band around the selected range.

我给了,上面的C#示例将允许你使用C#窗体中的RefEdit控件般的控制,但它不包括选取框乐队进行选择的时候 - 它仅仅利用标准的外观时,通常选择的范围

The C# example I gave, above would allow you to use a RefEdit-like control within a C# form, but it would not include the marquee band when making selections -- it will simply utilize the standard look when a range is normally selected.

如果你愿意在窗体上使用一个简单的输入框的解决方案,而不是控制,那么的你给会工作的C#示例(如果您使用Form.Show(),但的的Form.ShowDialog()),但不会表现出任何特别的选取框带或其他突出。对于一个简单的输入框的的使用选取框波段选择,你可以利用的 Excel.Application.InputBox 方法,传入的8的值的类型,这表明由输入框方法的返回值应该是一个'Excel.Range 数据类型:

If you are willing to use a simple input box solution, instead of a control on a form, then that C# example you gave would work (if you use Form.Show() but not Form.ShowDialog()), but it would not show any special marquee band or other highlighting. For a simple input box that does use marquee band selection, you could make use of the Excel.Application.InputBox method, passing in a value of '8' for the 'Type', which indicates that the value returned by the 'InputBox' method should be an 'Excel.Range' data type:

Excel.Application excelApp = ...;

string prompt = "Please select the range.";
string title = "Input Range";
int returnDataType = 8;
String DefaultRange = oXL.Selection.Address();

Excel.Range myRange = excelApp.InputBox(
                          prompt,
                          title,
                          DefaultRange,
                          Type.Missing,
                          Type.Missing,
                          Type.Missing,
                          Type.Missing,
                          returnDataType)

希望这有助于...

Hope this helps...

迈克