Keresés

Új hozzászólás Aktív témák

  • pmonitor

    aktív tag

    válasz pmonitor #10199 üzenetére

    Ha a kijelölt cellák sor- és oszlopszámát szeretnéd lekérdezni C#-ból (Interop DLL nélkül), akkor az alábbi COM Wrapper alapú megoldás működik:

    var selection = ComInvoker.GetProperty<object>(excel!, "Selection");
    var areas = ComInvoker.GetProperty<object>(selection, "Areas");
    int areaCount = ComInvoker.GetProperty<int>(areas, "Count");

    for (int a = 1; a <= areaCount; a++)
    {
    var area = ComInvoker.GetProperty<object>(areas, "Item", new object[] { a });
    var cellsInArea = ComInvoker.GetProperty<object>(area, "Cells");
    int count = ComInvoker.GetProperty<int>(cellsInArea, "Count");

    for (int i = 1; i <= count; i++)
    {
    var cell = ComInvoker.GetProperty<object>(cellsInArea, "Item", new object[] { i });
    string address = ComInvoker.GetProperty<string>(cell, "Address"); // pl. "$B$3"

    var match = Regex.Match(address, @"\$([A-Z]+)\$(\d+)");
    if (match.Success)
    {
    string colLetter = match.Groups[1].Value;
    int row = int.Parse(match.Groups[2].Value);
    int col = ColumnLetterToNumber(colLetter);
    Console.WriteLine($"Cell #{i}: Row={row}, Column={col}");
    }
    }
    }
    . És a "ColumnLetterToNumber" metódus: int ColumnLetterToNumber(string col)
    {
    int sum = 0;
    foreach (char c in col)
    {
    sum *= 26;
    sum += (char.ToUpper(c) - 'A' + 1);
    }
    return sum;
    }
    .
    Ez a megoldás a [ComAutoWrapper] NuGet-csomagot használja (Interop DLL nélkül is működik).Ezt a ChatGpt oldotta meg több próbálkozás után.

Új hozzászólás Aktív témák