Excel: getting selected rows back

I am using Microsoft.Office.Interop.Excel and I cannot find a way to return the selected rows. What I mean by “selected” is the line numbers themselves, when you click on the row column on the left and select one or more adjacent or non-adjacent rows (which selects the entire row). This differs from the choice of region or region on the sheet itself.

So far, I have been browsing through app.Selection, app.Cells, app.Rows, and not one of them has shown me a string. app.Selection gives me the actual contents of the cell, which is NOT what I want.

Any ideas? Thank you

+5
source share
3 answers

, . , Excel.Range.Rows ( ), Excel.Range.Row .

, , Areas and Rows Range.Row. :

Excel.Range range = (Excel.Range)excelApp.Selection;

List<int> rowNumbers = new List<int>();

// Enumerate the Rows within each Area of the Range.
foreach (Excel.Range area in range.Areas)
{
    foreach (Excel.Range row in area.Rows)
    {
        rowNumbers.Add(row.Row);
    }
}

// Report the results.
foreach (int rowNumber in rowNumbers)
{
    MessageBox.Show(rowNumber.ToString());
}

Linq, .

:

IEnumerable<int> rowNumbers =
    from area in range.Areas.Cast<Excel.Range>()
    from row in area.Rows.Cast<Excel.Range>()
    select row.Row;

:

IEnumerable<int> rowNumbers =
    range.Areas.Cast<Excel.Range>()
    .SelectMany(area => area.Rows.Cast<Excel.Range>()
                        .Select(row => row.Row));

: :

( , , ).

(1) - Linq Distinct. :

:

IEnumerable<int> distinctRowNumbers =
    (from area in range.Areas.Cast<Excel.Range>()
     from row in area.Rows.Cast<Excel.Range>()
     select row.Row)
    .Distinct();

:

IEnumerable<int> distinctRowNumbers =
    range.Areas.Cast<Excel.Range>()
    .SelectMany(area => area.Rows.Cast<Excel.Range>()
                        .Select(row => row.Row))
    .Distinct();

(2) Linq, HashSet. :

Excel.Range range = (Excel.Range)excelApp.Selection;

HashSet<int> distinctRowNumbers = new HashSet<int>();

// Enumerate the Rows within each Area of the Range.
foreach (Excel.Range area in range.Areas)
{
    foreach (Excel.Range row in area.Rows)
    {
        distinctRowNumbers.Add(row.Row);
    }
}

// Report the results.
foreach (int rowNumber in distinctRowNumbers)
{
    MessageBox.Show(rowNumber.ToString());
}

(3) , , , . , HashSet Distinct.

Excel.Range.EntireRow, :

Linq:

List<int> distinctRowNumbers = new List<int>();

foreach (Excel.Range area in range.EntireRow.Areas)
{
    foreach (Excel.Range row in area.Rows)
    {
        distinctRowNumbers.Add(row.Row);
    }
}

Linq :

IEnumerable<int> distinctRowNumbers =
    from area in range.EntireRow.Areas.Cast<Excel.Range>()
    from row in area.Rows.Cast<Excel.Range>()
    select row.Row;

Linq :

IEnumerable<int> distinctRowNumbers =
    range.EntireRow.Areas.Cast<Excel.Range>()
    .SelectMany(area => area.Rows.Cast<Excel.Range>()
                        .Select(row => row.Row));

, Range.EntireRow , , , Excel '95 , . ( - Excel Excel '97.) , , Excel, HashSet, Linq Distinct, , .

, !

Mike

+8

:
xlApp.ActiveCell.Row :)

+1

, :

var rows = new SortedSet<int>();                 // distinct and sorted

foreach (Excel.Range a in app.Selection.Areas)
    foreach (int r in Enumerable.Range(a.Row, a.Rows.Count))
        rows.Add(r);
0

All Articles