How do I get an Excel range using row and column numbers in VSTO / C#?

后端 未结 9 1931
萌比男神i
萌比男神i 2020-12-05 13:07

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 th

相关标签:
9条回答
  • 2020-12-05 13:33

    If you want like Cells(Rows.Count, 1).End(xlUp).Row , you can do it.

    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;
    
    0 讨论(0)
  • 2020-12-05 13:36

    The given answer will throw an error if used in Microsoft Excel 14.0 Object Library. Object does not contain a definition for 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;
    
    0 讨论(0)
  • 2020-12-05 13:36

    If you are getting an error stating that "Object does not contain a definition for 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;
    
    0 讨论(0)
  • 2020-12-05 13:40

    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.

    0 讨论(0)
  • 2020-12-05 13:41

    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
    
    0 讨论(0)
  • 2020-12-05 13:43

    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];
    
    0 讨论(0)
提交回复
热议问题