[英]How do I get an Excel range using row and column numbers in VSTO / C#?
I think the question sums it up. 我认为这个问题总结了一下。 Given two integers for row and column or four integers for row and column for the two corners of a range, how do I get a range object for that range.
给定行和列的两个整数或者一个范围的两个角的行和列的四个整数,如何获得该范围的范围对象。
Where the range is multiple cells: 范围是多个单元格:
Excel.Worksheet sheet = workbook.ActiveSheet;
Excel.Range rng = (Excel.Range) sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[3,3]);
Where range is one cell: 范围是一个单元格:
Excel.Worksheet sheet = workbook.ActiveSheet;
Excel.Range rng = (Excel.Range) sheet.Cells[1, 1];
The given answer will throw an error if used in Microsoft Excel 14.0 Object Library. 如果在Microsoft Excel 14.0对象库中使用,给定的答案将引发错误。 Object does not contain a definition for get_range.
对象不包含get_range的定义。 Instead use
而是使用
int countRows = xlWorkSheetData.UsedRange.Rows.Count;
int countColumns = xlWorkSheetData.UsedRange.Columns.Count;
object[,] data = xlWorkSheetData.Range[xlWorkSheetData.Cells[1, 1], xlWorkSheetData.Cells[countRows, countColumns]].Cells.Value2;
you can retrieve value like this 你可以检索这样的价值
string str = (string)(range.Cells[row, col] as Excel.Range).Value2 ;
select entire used range 选择整个使用范围
Excel.Range range = xlWorkSheet.UsedRange;
source : 资源 :
http://csharp.net-informations.com/excel/csharp-read-excel.htm http://csharp.net-informations.com/excel/csharp-read-excel.htm
flaming 炽盛
Facing the same problem I found the quickest solution was to actually scan the rows of the cells I wished to sort, determine the last row with a non-blank element and then select and sort on that grouping. 面对同样的问题,我发现最快的解决方案是实际扫描我想要排序的单元格的行,用非空白元素确定最后一行,然后选择并对该分组进行排序。
Dim lastrow As Integer
lastrow = 0
For r = 3 To 120
If Cells(r, 2) = "" Then
Dim rng As Range
Set rng = Range(Cells(3, 2), Cells(r - 1, 2 + 6))
rng.Select
rng.Sort Key1:=Range("h3"), order1:=xlDescending, Header:=xlGuess, DataOption1:=xlSortNormal
r = 205
End If
Next r
If you are getting an error stating that "Object does not contain a definition for get_range." 如果您收到错误,指出“对象不包含get_range的定义”。
Try following. 试试以下。
Excel.Worksheet sheet = workbook.ActiveSheet;
Excel.Range rng = (Excel.Range) sheet.Range[sheet.Cells[1, 1], sheet.Cells[3,3]].Cells;
I found a good short method that seems to work well... 我找到了一个好的简短方法似乎运作良好......
Dim x, y As Integer
x = 3: y = 5
ActiveSheet.Cells(y, x).Select
ActiveCell.Value = "Tada"
In this example we are selecting 3 columns over and 5 rows down, then putting "Tada" in the cell. 在这个例子中,我们选择3列和5行,然后将“Tada”放入单元格中。
UsedRange work fine with "virgins" cells, but if your cells are filled in the past, then UsedRange will deliver to you the old value. UsedRange可与“virgins”单元格配合使用,但如果您的单元格在过去已填充,则UsedRange将向您提供旧值。
For example: 例如:
"Think in a Excel sheet that have cells A1 to A5 filled with text". “在包含单元格A1到A5填充文本的Excel工作表中思考”。 In this scenario, UsedRange must be implemented as:
在这种情况下, UsedRange必须实现为:
Long SheetRows;
SheetRows = ActiveSheet.UsedRange.Rows.Count;
A watch to SheetRows variable must display a value of 5 after the execution of this couple of lines. 在执行这几行之后,对SheetRows变量的监视必须显示值5。
Q1: But, what happen if the value of A5 is deleted?
Q1:但是,如果A5的值被删除会发生什么?
A1: The value of SheetRows would be 5
A1:SheetRows的值为5
Q2: Why this?
Q2:为什么这样?
A2: Because MSDN define UsedRange property as:
A2:因为MSDN将UsedRange属性定义为:
Gets a Microsoft.Office.Interop.Excel.Range object that represents all the cells that have contained a value at any time.
获取一个Microsoft.Office.Interop.Excel.Range对象, 该 对象表示 随时包含值的 所有 单元格。
So, the question is: Exist some/any workaround for this behavior? 所以,问题是:存在这种行为的一些/任何解决方法?
I think in 2 alternatives: 我认为有两种选择:
Long SheetRows;
SheetRows = ActiveSheet.Range("A1").CurrentRegion.Rows.Count;
If you want like Cells(Rows.Count, 1).End(xlUp).Row
, you can do it. 如果你想要像
Cells(Rows.Count, 1).End(xlUp).Row
,你可以做到。
just use the following code: 只需使用以下代码:
using Excel = Microsoft.Office.Interop.Excel;
string xlBk = @"D:\Test.xlsx";
Excel.Application xlApp;
Excel.Workbook xlWb;
Excel.Worksheet xlWs;
Excel.Range rng;
int iLast;
xlApp = new Excel.Application();
xlWb = xlApp.Workbooks.Open(xlBk, 0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWs = (Excel.Worksheet)xlWb.Worksheets.get_Item(1);
iLast = xlWs.Rows.Count;
rng = (Excel.Range)xlWs.Cells[iLast, 1];
iLast = rng.get_End(Excel.XlDirection.xlUp).Row;
Try this, works! 试试这个,有效!
Excel.Worksheet sheet = xlWorkSheet;
Excel.Series series1 = seriesCollection.NewSeries();
Excel.Range rng = (Excel.Range)xlWorkSheet.Range[xlWorkSheet.Cells[3, 13], xlWorkSheet.Cells[pp, 13]].Cells;
series1.Values = rng;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.