简体   繁体   English

如何在VSTO / C#中使用行号和列号获取Excel范围?

[英]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". “在包含单元格A1A5填充文本的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: 我认为有两种选择:

  1. Avoid deleting the content of the cell, preferring deletion of the whole row (right click in the row number, then "delete row". 避免删除单元格的内容,更喜欢删除整行(右键单击行号,然后“删除行”)。
  2. Use CurrentRegion instead of UsedRange property as follow: 使用CurrentRegion而不是UsedRange属性如下:

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.

相关问题 如何获取 Excel VSTO C# 中已删除范围的值 - How do I get the values of a deleted range in Excel VSTO C# C#Excel VSTO:get_Range()和Range()之间的区别 - C# Excel VSTO: Difference between get_Range() and Range() 如何通过使用VSTO获得有效的Excel范围? - How to get the active excel range by using VSTO? 如何在 C# / VSTO 中为 Excel 数据 Model 连接指定使用 XlCmdType.xlCmdTableCollection 的表 - How do I specify the tables using XlCmdType.xlCmdTableCollection for an Excel Data Model connection in C# / VSTO 在 Excel 和 C# 中给定起点的情况下,如何获得包含 X 行和 Y 行的范围? - How can I get a Range incorporating X rows and Y rows given a starting point in Excel with VSTO & C#? 如何让用户在 c# vsto excel 加载项中选择单元格(范围输入)? - How to get user to select cells(Range input) in c# vsto excel add-in? 使用C#在Excel / VSTO中的列号为列号 - Column No to Column Letter in Excel/VSTO using C# 如何在Excel中仅读取具有特定范围内的值的单元格-使用VSTO C# - How to only read cells with values with in a specific range in Excel - using VSTO C# 如何使用c#中的行索引和列索引从datagridview获取单元格值? - How do I get the cell value from a datagridview using row index and column index in c#? c#如何使用OleDbCommand在指定的列名称下将指定的值添加到Excel工作表的下一个可用行 - c# How do I add specified values to the next available row of an Excel worksheet under specified column names using OleDbCommand
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM