Excel Interop:按数字引用,而不是按字符串引用

发布于 2024-11-09 14:14:48 字数 419 浏览 1 评论 0原文

早上好。

阅读有关优化 Excel Interop 的问题的答案后,我发现使用 worksheet.Range["A1:C3"] 引用单元格(与worksheet.get_range("A1:C3")) 不太方便。我想以某种方式使用整数/长数字引用单元格,并且我不想将列号 {1, 2, 3, ...} 映射到列字母 {"A", "B", "C “,...}。

注意:我知道 .Cells,但这不是一个选项,因为这仅返回单个单元格。

有什么想法吗?

问候

Good morning.

After having read the answer on a question about optimizing Excel Interop, I found that referencing a cell using worksheet.Range["A1:C3"] (same as worksheet.get_range("A1:C3")) isn't very handy. I'd like to reference the cell somehow using integer/long numbers, and I wouldn't want to map column numbers {1, 2, 3, ...} to column letters {"A", "B", "C", ...}.

Note: I know about .Cells, but this isn't an option as this only returns single cells AFAIK.

Any idea?

regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海的爱人是光 2024-11-16 14:14:48

在 Excel 宏 (VBA) 中,您可以使用这种方式:

Dim rngStart As Range
Set rngStart = Range("A1")

Dim rngEnd As Range
Set rngEnd = rngStart.Rows(3).Columns(4)

Dim rngFinal As Range
Set rngFinal = Range(rngStart, rngEnd)

rngFinal.Select

将其重写为 C#/VB.NET 应该很容易。

In Excel macro (VBA) you can use this way:

Dim rngStart As Range
Set rngStart = Range("A1")

Dim rngEnd As Range
Set rngEnd = rngStart.Rows(3).Columns(4)

Dim rngFinal As Range
Set rngFinal = Range(rngStart, rngEnd)

rngFinal.Select

It should be easy rewrite it to C#/VB.NET.

冧九 2024-11-16 14:14:48

您可以使用 Cells 属性创建一个 Range 对象,该对象可用作 Range 属性的参数。

查看此处的示例: http://msdn.microsoft.com/en-us /library/bb178282.aspx 在页面中间有一个示例,其中使用 Cells 属性获取两个范围对象,并将其传递给 Range 属性而不是传递字符串:

With Worksheets(1)
    .Range(.Cells(1, 1), _
        .Cells(10, 10)).Borders.LineStyle = xlThick
End With

一般来说,Cells 属性返回一个 Range 对象,您可以使用它执行任何您想要的操作:http://msdn. microsoft.com/en-us/library/bb148836.aspx

You can use the Cells property to create a Range object that can be used as argument to the Range property.

Check out the example here: http://msdn.microsoft.com/en-us/library/bb178282.aspx In the middle of the page you have an example where you use the Cells property to get two range objects that you pass to the Range property instead of passing strings:

With Worksheets(1)
    .Range(.Cells(1, 1), _
        .Cells(10, 10)).Borders.LineStyle = xlThick
End With

In general, the Cells property returns a Range object that you can do whatever you want with: http://msdn.microsoft.com/en-us/library/bb148836.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文