C# Interop.Excel: Die Beschriftung der x-Achse soll im Format tt.mm.jj sein. Absturz

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von INOPIAE.

    C# Interop.Excel: Die Beschriftung der x-Achse soll im Format tt.mm.jj sein. Absturz

    Hallo zusammen,
    ich schreibe in einer Win-Forms-Anwendung und mittels Interop.Excel in eine Exceldatei. Nachdem ich die Werte reingeschrieben habe – Datumsangaben (Format 15.01.2023 17:34:00) und eine Ganzzahl – soll ein Diagramm gezeichnet werden. Alles funktioniert prima, bis auf die Zeile, die die x-Achsen-Beschriftung aufs Format
    dd.mm.yy setzen soll. Es kommt eine System.Reflection.TargetInvocationException. Ich habe mal diese Änderung in Excel selbst vorgenommen und den Makrorekorder aufzeichnen lassen. Dies sind die nötigen Befehle... Ich verstehe nicht, warum das nicht geht. Ich habe es mit und ohne axis.Select(); versucht.
    Ich zeige euch die komplette Methode:


    C#-Quellcode

    1. private static void Make_new_Chart(Worksheet sheet, string from, string to_)
    2. {
    3. Chart chart_ = sheet.Shapes.AddChart2(Style: 240,
    4. XlChartType: XlChartType.xlXYScatter,
    5. Left: 335,
    6. Top: 2,
    7. Width: 950,
    8. Height: 540,
    9. NewLayout: true).Chart;
    10. chart_.HasTitle = false;
    11. chart_.HasLegend = false;
    12. chart_.SetSourceData(sheet.Range[from, to_]);
    13. chart_.Parent.Border.Color = 0;
    14. chart_.Parent.Border.Weight = 3;
    15. int cnt = 1;
    16. foreach (Axis axis in chart_.Axes())
    17. {
    18. if (cnt == 2) // x-Achse
    19. {
    20. axis.HasTitle = true;
    21. axis.AxisTitle.Text = "Datum";
    22. axis.TickLabels.Font.Size = 8;
    23. axis.TickLabels.Font.Bold = false;
    24. axis.Select();
    25. axis.TickLabels.NumberFormat = "TT.MM.JJ;@";
    26. }
    27. else // y-Achse
    28. {
    29. axis.HasTitle = true;
    30. axis.AxisTitle.Text = "Anzahl";
    31. axis.TickLabels.Font.Size = 11;
    32. axis.TickLabels.Font.Bold = true;
    33. axis.MinimumScale = 0;
    34. }
    35. cnt++;
    36. axis.AxisTitle.Font.Size = 11;
    37. axis.AxisTitle.Font.Bold = true;
    38. axis.AxisTitle.Font.Name = "Segoe UI";
    39. axis.TickLabels.Font.Name = "Segoe UI";
    40. axis.MajorTickMark = XlTickMark.xlTickMarkCross;
    41. axis.Border.Color = 0; // schwarz
    42. axis.Border.LineStyle = 0;
    43. axis.Border.Weight = 2;
    44. }
    45. System.Drawing.Color black = System.Drawing.Color.FromArgb(0, 0, 0);
    46. chart_.SeriesCollection(1).MarkerBackgroundColor = black;
    47. chart_.SeriesCollection(1).MarkerForegroundColor = black;
    48. chart_.SeriesCollection(1).Format.Line.Visible = false;
    49. }


    Edit: Ich hab den Fehler gesehen, jetzt, wo ich diese Frage gestellt habe: es muss

    C#-Quellcode

    1. axis.TickLabels.NumberFormat = "dd.mm.yy;@";
    heißen. Na, was soll's... Hat noch jemand Verbesserungsvorschläge für die Methode?
    Bilder
    • x-Achsenbeschriftung Format laut Excelvorschlag.jpg

      15,78 kB, 310×259, 37 mal angesehen

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Bartosz“ ()