Arbeitsblätter von Microsoft Excel Dateien verwalten

Aspose.Cells bietet eine Klasse, Workbook, die eine Excel-Datei repräsentiert. Die Klasse Workbook enthält eine Sammlung von Worksheets, die den Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht.

Ein Arbeitsblatt wird durch die Klasse Worksheet repräsentiert. Die Klasse Worksheet bietet eine Vielzahl von Eigenschaften und Methoden zum Verwalten von Arbeitsblättern.

Arbeitsblätter zu einer neuen Excel-Datei hinzufügen

Um programmgesteuert eine neue Excel-Datei zu erstellen:

  1. Erstellen Sie ein Objekt der Klasse Workbook.
  2. Rufen Sie die Methode Add der Klasse WorksheetCollection auf. Ein leeres Arbeitsblatt wird automatisch zur Excel-Datei hinzugefügt. Es kann durch Übergeben des Blattindex des neuen Arbeitsblatts an die Sammlung Worksheets referenziert werden.
  3. Holen Sie sich eine Arbeitsblatt-Referenz.
  4. Arbeiten Sie an den Arbeitsblättern.
  5. Speichern Sie die neue Excel-Datei mit neuen Arbeitsblättern, indem Sie die Methode Save der Klasse Workbook aufrufen.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
// Saving the Excel file
workbook.Save(dataDir + "output.out.xls");

Arbeitsblätter zu einem Designer-Arbeitsblatt hinzufügen

Der Prozess zum Hinzufügen von Arbeitsblättern zu einem Designer-Arbeitsblatt ist derselbe wie das Hinzufügen eines neuen Arbeitsblatts, mit der Ausnahme, dass die Excel-Datei bereits existiert und daher geöffnet werden sollte, bevor Arbeitsblätter hinzugefügt werden. Ein Designer-Arbeitsblatt kann durch die Klasse Workbook geöffnet werden.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Setting the name of the newly added worksheet
worksheet.Name = "My Worksheet";
// Saving the Excel file
workbook.Save(dataDir + "output.xlsx");

Zugriff auf Arbeitsblätter mithilfe des Blattnamens

Greifen Sie auf jedes Arbeitsblatt zu, indem Sie dessen Namen oder Index angeben.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Sheet1"];
Cell cell = worksheet.Cells["A1"];
Console.WriteLine(cell.Value);

Arbeitsblätter anhand des Blattnamens entfernen

Um Arbeitsblätter aus einer Datei zu entfernen, rufen Sie die Methode RemoveAt der Klasse WorksheetCollection auf. Geben Sie den Blattnamen an die Methode RemoveAt an, um ein bestimmtes Arbeitsblatt zu entfernen.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Removing a worksheet using its sheet name
workbook.Worksheets.RemoveAt("Sheet1");
// Save workbook
workbook.Save(dataDir + "output.out.xls");

Arbeitsblätter anhand des Blattindex entfernen

Das Entfernen von Arbeitsblättern nach Namen funktioniert gut, wenn der Name des Arbeitsblatts bekannt ist. Wenn Sie den Namen des Arbeitsblatts nicht kennen, verwenden Sie eine überladene Version der Methode RemoveAt, die den Blattindex des Arbeitsblatts anstelle seines Blattnamens verwendet.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Removing a worksheet using its sheet index
workbook.Worksheets.RemoveAt(0);
// Save workbook
workbook.Save(dataDir + "output.out.xls");

Aktivierung von Tabellen und Markierung einer aktiven Zelle im Arbeitsblatt

Manchmal benötigen Sie ein bestimmtes Arbeitsblatt, das aktiv und angezeigt wird, wenn ein Benutzer eine Microsoft Excel-Datei in Excel öffnet. Ebenso möchten Sie möglicherweise eine bestimmte Zelle aktivieren und die Bildlaufleisten so einstellen, dass die aktive Zelle angezeigt wird. Aspose.Cells ist in der Lage, all diese Aufgaben zu erledigen.

Ein aktives Tabellenblatt ist ein Blatt, an dem Sie arbeiten: Der Name des aktiven Blattes auf der Registerkarte ist standardmäßig fett gedruckt.

Eine aktive Zelle ist eine ausgewählte Zelle, in die Daten eingegeben werden, wenn Sie mit der Eingabe beginnen. Es ist jeweils nur eine Zelle aktiv. Die aktive Zelle ist durch einen starken Rahmen hervorgehoben.

Aktivierung von Tabellen und Markierung einer Zelle als aktiv

Aspose.Cells bietet spezifische API-Aufrufe zur Aktivierung eines Blattes und einer Zelle an. Zum Beispiel ist die Eigenschaft Aspose.Cells.WorksheetCollection.ActiveSheetIndex nützlich, um das aktive Blatt in einer Arbeitsmappe festzulegen. Ebenso wird die Eigenschaft Aspose.Cells.Worksheet.ActiveCell verwendet, um eine aktive Zelle im Arbeitsblatt festzulegen und abzurufen.

Um sicherzustellen, dass die horizontalen oder vertikalen Bildlaufleisten auf die Zeilen- und Spaltenindexposition eingestellt sind, um bestimmte Daten anzuzeigen, verwenden Sie die Eigenschaften Aspose.Cells.Worksheet.FirstVisibleRow und Aspose.Cells.Worksheet.FirstVisibleColumn.

Das folgende Beispiel zeigt, wie ein Arbeitsblatt aktiviert und eine aktive Zelle darin markiert wird. In der generierten Ausgabe werden die Bildlaufleisten gescrollt, um die 2. Zeile und 2. Spalte als erste sichtbare Zeile und Spalte zu zeigen.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.Worksheets[0];
// Get the cells in the worksheet.
Cells cells = worksheet1.Cells;
// Input data into B2 cell.
cells[1, 1].PutValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1;
// Save the excel file.
workbook.Save(dataDir + "output.xls");

Erweiterte Themen