Zeilen und Spalten formatieren

Arbeiten mit Zeilen

Anpassen der Zeilenhöhe

Aspose.Cells bietet eine Klasse,Arbeitsmappe , die eine Microsoft Excel-Datei darstellt. DasArbeitsmappe Klasse enthält aArbeitsblattsammlungdie den Zugriff auf jedes Arbeitsblatt in der Excel-Datei ermöglicht. Ein Arbeitsblatt wird durch dargestelltArbeitsblatt Klasse. DasArbeitsblatt Klasse bietet aCellsAuflistung, die alle Zellen im Arbeitsblatt darstellt.

DasCells-Sammlung bietet mehrere Methoden zum Verwalten von Zeilen oder Spalten in einem Arbeitsblatt. Einige davon werden weiter unten ausführlicher besprochen.

Festlegen der Höhe einer Zeile

Es ist möglich, die Höhe einer einzelnen Zeile durch Aufrufen von festzulegenCells SammlungSetRowHeight Methode. DasSetRowHeightDie Methode nimmt die folgenden Parameter wie folgt:

  • Zeilenindex, der Index der Zeile, deren Höhe Sie ändern.
  • Zeilenhöhe, die Zeilenhöhe, die auf die Zeile angewendet werden soll.
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the height of the second row to 13
worksheet.Cells.SetRowHeight(1, 13);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Festlegen der Höhe aller Zeilen in einem Arbeitsblatt

Wenn Entwickler für alle Zeilen im Arbeitsblatt dieselbe Zeilenhöhe festlegen müssen, können sie dies mithilfe von tunStandardhöhe Eigentum derCellsSammlung.

Beispiel:

// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the height of all rows in the worksheet to 15
worksheet.Cells.StandardHeight = 15;
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Arbeiten mit Spalten

Einstellen der Breite einer Spalte

Legen Sie die Breite einer Spalte fest, indem Sie die aufrufenCells SammlungSetColumnWidth Methode. DasSetColumnWidthDie Methode nimmt die folgenden Parameter an:

  • Spaltenindex, der Index der Spalte, deren Breite Sie ändern.
  • Spaltenbreite, die gewünschte Spaltenbreite.
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the width of the second column to 17.5
worksheet.Cells.SetColumnWidth(1, 17.5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Spaltenbreite in Pixel einstellen

Legen Sie die Breite einer Spalte fest, indem Sie die aufrufenCellsSammlungSetColumnWidthPixelMethode. DasSetColumnWidthPixelDie Methode nimmt die folgenden Parameter an:

  • Spaltenindex, der Index der Spalte, deren Breite Sie ändern.
  • Spaltenbreitedie gewünschte Spaltenbreite in Pixel.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
string outDir = RunExamples.Get_OutputDirectory();
//Load source Excel file
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx");
//Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Set the width of the column in pixels
worksheet.Cells.SetColumnWidthPixel(7, 200);
workbook.Save(outDir + "SetColumnWidthInPixels_Out.xlsx");

Festlegen der Breite aller Spalten in einem Arbeitsblatt

Um dieselbe Spaltenbreite für alle Spalten im Arbeitsblatt festzulegen, verwenden Sie dieCells SammlungStandardbreiteEigentum.

// 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);
// 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);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Setting the width of all columns in the worksheet to 20.5
worksheet.Cells.StandardWidth = 20.5;
// Saving the modified Excel file
workbook.Save(dataDir + "output.out.xls");
// Closing the file stream to free all resources
fstream.Close();

Themen vorantreiben