Zeilen und Spalten automatisch anpassen

Automatische Anpassung

Aspose.Cells stellt eine Workbook-Klasse bereit, die eine Microsoft Excel-Datei darstellt. Die Workbook-Klasse enthält eine Worksheets-Sammlung, die den Zugriff auf jede Arbeitsmappe in einer Excel-Datei ermöglicht. Eine Arbeitsmappe wird durch die Worksheet-Klasse dargestellt. Die Worksheet-Klasse bietet eine breite Palette von Eigenschaften und Methoden zur Verwaltung einer Arbeitsmappe. Dieser Artikel beschäftigt sich mit der Verwendung der Worksheet-Klasse zum automatischen Anpassen von Zeilen oder Spalten.

AutoFit Zeile - Einfach

Der einfachste Ansatz, um die Breite und Höhe einer Zeile automatisch anzupassen, ist das Aufrufen der Methode AutoFitRow der Klasse Worksheet. Die Methode AutoFitRow erhält einen Zeilenindex (der zu ändernden Zeile) als Parameter.

// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Wie man eine Zeile in einem Zellenbereich automatisch anpasst

Eine Zeile besteht aus vielen Spalten. Aspose.Cells ermöglicht es Entwicklern, eine Zeile basierend auf dem Inhalt in einem Zellenbereich innerhalb der Zeile automatisch anzupassen, indem eine überladene Version der AutoFitRow-Methode aufgerufen wird. Sie nimmt die folgenden Parameter an:

  • Zeilenindex, der Index der zu automatisch anzupassenden Zeile.
  • Erster Spaltenindex, der Index der ersten Spalte der Zeile.
  • Letzter Spaltenindex, der Index der letzten Spalte der Zeile.

Die Methode AutoFitRow prüft die Inhalte aller Spalten in der Zeile und passt dann die Zeile automatisch an.

// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1, 0, 5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Wie man eine Spalte in einem Zellenbereich automatisch anpasst

Eine Spalte besteht aus vielen Zeilen. Es ist möglich, eine Spalte basierend auf dem Inhalt in einem Zellenbereich in der Spalte automatisch anzupassen, indem eine überladene Version der Methode AutoFitColumn aufgerufen wird, die die folgenden Parameter verwendet:

  • Spaltenindex, der Index der zu automatisch anzupassenden Spalte.
  • Erster Zeilenindex, der Index der ersten Zeile der Spalte.
  • Letzter Zeilenindex, der Index der letzten Zeile der Spalte.

Die AutoFitColumn-Methode überprüft den Inhalt aller Zeilen in der Spalte und passt die Spalte dann automatisch an.

// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Auto-fitting the Column of the worksheet
worksheet.AutoFitColumn(4, 4, 6);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

Wie man Zeilen für zusammengeführte Zellen automatisch anpasst

Mit Aspose.Cells ist es möglich, Zeilen auch für Zellen, die mit der AutoFitterOptions-API zusammengeführt wurden, automatisch anzupassen. Die AutoFitterOptions-Klasse bietet eine AutoFitMergedCellsType-Eigenschaft, die zum automatischen Anpassen von Zeilen für zusammengeführte Zellen verwendet werden kann. AutoFitMergedCellsType akzeptiert eine AutoFitMergedCellsType-Aufzählung, die die folgenden Elemente enthält.

  • None: Zusammengeführte Zellen ignorieren.
  • FirstLine: Erweitert nur die Höhe der ersten Zeile.
  • LastLine: Erweitert nur die Höhe der letzten Zeile.
  • EachLine: Erweitert nur die Höhe jeder Zeile.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiate a new Workbook
Workbook wb = new Workbook();
// Get the first (default) worksheet
Worksheet _worksheet = wb.Worksheets[0];
// Create a range A1:B1
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2);
// Merge the cells
range.Merge();
// Insert value to the merged cell A1
_worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end";
// Create a style object
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle();
// Set wrapping text on
style.IsTextWrapped = true;
// Apply the style to the cell
_worksheet.Cells[0, 0].SetStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine;
// Autofit rows in the sheet(including the merged cells)
_worksheet.AutoFitRows(options);
// Save the Excel file
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx");

Wichtig zu wissen

Erweiterte Themen