Anpassen der Zeilenhöhe und Spaltenbreite
Arbeiten mit Zeilen
Anpassen der Zeilenhöhe
Aspose.Cells bietet eine Klasse, Workbook, die eine Microsoft Excel-Datei darstellt. Die Workbook Klasse enthält eine WorksheetCollection, die den Zugriff auf jede Arbeitsmappe in der Excel-Datei ermöglicht. Eine Arbeitsmappe wird durch die Worksheet-Klasse repräsentiert. Die Worksheet-Klasse stellt eine Cells-Sammlung dar, die alle Zellen in der Arbeitsmappe repräsentiert.
Die Cells-Sammlung bietet verschiedene Methoden zur Verwaltung von Zeilen oder Spalten in einer Arbeitsmappe. Einige davon werden unten detaillierter besprochen.
Einstellen der Zeilenhöhe
Es ist möglich, die Höhe einer einzelnen Zeile durch Aufruf der Cells-Sammlungsmethode setRowHeight festzulegen. Die setRowHeight-Methode hat die folgenden Parameter:
- Zeilenindex, der Index der Zeile, deren Höhe geändert wird.
- Zeilenhöhe, die auf die Zeile anzuwendende Zeilenhöhe.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingHeightOfRow.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
Cells cells = worksheet.getCells(); | |
// Setting the height of the second row to 13 | |
cells.setRowHeight(1, 13); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingHeightOfRow_out.xls"); |
Einstellen der Höhe aller Zeilen
Um die gleiche Zeilenhöhe für alle Zeilen in einer Arbeitsmappe festzulegen, verwenden Sie die Cells-Sammlungsmethode setStandardHeight.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingHeightAllRows.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
Cells cells = worksheet.getCells(); | |
// Setting the height of all rows in the worksheet to 15 | |
worksheet.getCells().setStandardHeight(15f); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingHeightAllRows_out.xls"); |
Arbeiten mit Spalten
Einstellen der Breite einer Spalte
Legen Sie die Breite einer Spalte fest, indem Sie die Cells-Sammlungsmethode setColumnWidth aufrufen. Die setColumnWidth-Methode hat die folgenden Parameter:
- Spaltenindex, der Index der Spalte, deren Breite geändert wird.
- Spaltenbreite, die gewünschte Spaltenbreite.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingWidthOfColumn.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
Cells cells = worksheet.getCells(); | |
// Setting the width of the second column to 17.5 | |
cells.setColumnWidth(1, 17.5); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingWidthOfColumn_out.xls"); |
Einstellen der Breite aller Spalten
Um die gleiche Spaltenbreite für alle Spalten in einer Arbeitsmappe festzulegen, verwenden Sie die Cells-Sammlungsmethode setStandardWidth.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(SettingWidthOfAllColumns.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
Cells cells = worksheet.getCells(); | |
// Setting the width of all columns in the worksheet to 20.5 | |
worksheet.getCells().setStandardWidth(20.5f); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "SettingWidthOfAllColumns_out.xls"); |