English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Methode zur Erstellung von Excel-Mehrebenengruppen in C#

If you can group and display data with multiple levels of detail in Excel, it can clearly show the overall structure of the data table, making the entire document have a sense of hierarchy. According to your needs, set the display or hide detailed information under the category data, which is convenient for data viewing and management while also making the document more beautiful. So, how to create a multi-level grouping display of Excel data in C#? The following will be elaborated in detail. The free version component is used in the method.Free Spire.XLS for .NETDownload and install this component, create a console application, and add the dll file under the component package to the project, and add the corresponding namespace at the same time. The following is a specific code operation step for reference.

Step 1:Create a Wordbook class object and get the first worksheet

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];

Step 2:Write data to the cell

sheet.Range["A1"].Value = "Company department";
sheet.Range["A3"].Value = "General department";
sheet.Range["A4"].Value = "Administration";
sheet.Range["A5"].Value = "Personnel";
sheet.Range["A6"].Value = "Marketing department";
sheet.Range["A7"].Value = "Business department";
sheet.Range["A8"].Value = "Customer service department";
sheet.Range["A9"].Value = "Technology department";
sheet.Range["A10"].Value = "Technology development";
sheet.Range["A11"].Value = "Technical support";
sheet.Range["A12"].Value = "Pre-sales support";
sheet.Range["A13"].Value = "After-sales support";

Step 3:Set the IsSummaryRowBelow property to false, so the summary row is displayed above the detail rows

sheet.PageSetup.IsSummaryRowBelow = false;

Step 4:Select rows for grouping, the parameter false indicates to expand the current group, set it to true if you want to hide

//Wählen Sie Zeilen für die erste Ebenengruppe aus
sheet.GroupByRows(2, 13, false);
//Wählen Sie Zeilen für die zweite Ebenengruppe aus
sheet.GroupByRows(4,5 , false);
sheet.GroupByRows(7, 8, false);
sheet.GroupByRows(10,13 , false);
//Wählen Sie Zeilen für die dritte Ebenengruppe aus
sheet.GroupByRows(12,13, true);

Step 5:Define a CellStyle object, set and apply the format to the font inside the cell

CellStyle style = workbook.Styles.Add("style");
style.Font.IsBold = true;
style.Color = Color.LawnGreen;
sheet.Range["A1"]').CellStyleName = style.Name;
sheet.Range["A3"]').CellStyleName = style.Name;
sheet.Range["A6"]').CellStyleName = style.Name;
sheet.Range["A9"]').CellStyleName = style.Name;

Step 6:Setzen Sie den Stil der Bereichsrandart

sheet.Range["A4:A5"]').BorderAround(LineStyleType.Thin);
sheet.Range["A4:A5"].BorderInside(LineStyleType.Thin);
sheet.Range["A7:A8"]').BorderAround(LineStyleType.Thin);
sheet.Range["A7:A8"].BorderInside(LineStyleType.Thin);
sheet.Range["A10:A13"]').BorderAround(LineStyleType.Thin);
sheet.Range["A10:A13"].BorderInside(LineStyleType.Thin);

Step 7:Dokument speichern

workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013);

Run the program to generate the document (the generated document can be found in the bin>Debug folder of the project folder)

Effect display:

All code:

using System;
using System.Drawing;
using Spire.Xls;
namespace MultilevelGroup_XLS
{
 class Program
 {
  static void Main(string[] args)
  {
   //Create a Wordbook class object and get the first worksheet
   Workbook workbook = new Workbook();
   Worksheet sheet = workbook.Worksheets[0];
   //Write data to the cell
   sheet.Range["A1"].Value = "Company department";
   sheet.Range["A3"].Value = "General department";
   sheet.Range["A4"].Value = "Administration";
   sheet.Range["A5"].Value = "Personnel";
   sheet.Range["A6"].Value = "Marketing department";
   sheet.Range["A7"].Value = "Business department";
   sheet.Range["A8"].Value = "Customer service department";
   sheet.Range["A9"].Value = "Technology department";
   sheet.Range["A10"].Value = "Technology development";
   sheet.Range["A11"].Value = "Technical support";
   sheet.Range["A12"].Value = "Pre-sales support";
   sheet.Range["A13"].Value = "After-sales support";
   //Set IsSummaryRowBelow to false, so the summary row is displayed above the detail rows
   sheet.PageSetup.IsSummaryRowBelow = false;
   //Wählen Sie Zeilen für die erste Ebenengruppe aus
   //Der Parameter false bedeutet, dass die aktuelle Gruppe geöffnet wird. Um sie zu verbergen, stellen Sie ihn auf true ein
   sheet.GroupByRows(2, 13, false);
   //Wählen Sie Zeilen für die zweite Ebenengruppe aus
   sheet.GroupByRows(4,5 , false);
   sheet.GroupByRows(7, 8, false);
   sheet.GroupByRows(10,13 , false);
   //Wählen Sie Zeilen für die dritte Ebenengruppe aus
   sheet.GroupByRows(12,13, true);
   //Definieren Sie ein CellStyle-Objekt und passen Sie das Zellenformat an
   CellStyle style = workbook.Styles.Add("style");
   style.Font.IsBold = true;
   style.Color = Color.LawnGreen;
   sheet.Range["A1"]').CellStyleName = style.Name;
   sheet.Range["A3"]').CellStyleName = style.Name;
   sheet.Range["A6"]').CellStyleName = style.Name;
   sheet.Range["A9"]').CellStyleName = style.Name;
   //Setzen Sie den Stil der Bereichsrandart
   sheet.Range["A4:A5"]').BorderAround(LineStyleType.Thin);
   sheet.Range["A4:A5"].BorderInside(LineStyleType.Thin);
   sheet.Range["A7:A8"]').BorderAround(LineStyleType.Thin);
   sheet.Range["A7:A8"].BorderInside(LineStyleType.Thin);
   sheet.Range["A10:A13"]').BorderAround(LineStyleType.Thin);
   sheet.Range["A10:A13"].BorderInside(LineStyleType.Thin);
   //Dokument speichern
   workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013);
   System.Diagnostics.Process.Start("output.xlsx");
  }
 }
}

Dies sind alle Operationsschritte zur Einrichtung von mehrstufigen Gruppen in Excel.

Das ist der gesamte Inhalt dieses Artikels. Wir hoffen, dass er Ihnen bei Ihrem Lernen hilft und wir hoffen, dass Sie die呐喊教程大力支持。

Erklärung: Der Inhalt dieses Artikels wurde aus dem Internet übernommen und gehört dem jeweiligen Urheber. Der Inhalt wurde von Internetbenutzern freiwillig beigesteuert und hochgeladen. Diese Website besitzt keine Eigentumsrechte und hat den Inhalt nicht manuell bearbeitet. Sie übernimmt auch keine rechtlichen Verantwortlichkeiten. Wenn Sie urheberrechtlich beanstandete Inhalte finden, freuen wir uns über eine E-Mail an: notice#oldtoolbag.com (Bitte ersetzen Sie # durch @, wenn Sie eine Meldung senden, und fügen Sie relevante Beweise bei. Sobald überprüft, wird die Website die beanstandeten urheberrechtlichen Inhalte sofort löschen.)

Dir gefällt vielleicht auch