调整行高和列宽

使用行

调整行高

Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表集合允许访问 Excel 文件中的每个工作表。工作表由工作表班级。这工作表类提供了Cells代表工作表中所有单元格的集合。

Cellscollection 提供了多种方法来管理工作表中的行或列。下面将更详细地讨论其中的一些。

设置行高

可以通过调用来设置单行的高度Cells收藏的[设置行高](https://reference.aspose.com/cells/java/com.aspose.cells/cells#setRowHeight(int,%20double)) 方法。这[设置行高](https://reference.aspose.com/cells/java/com.aspose.cells/cells#setRowHeight(int,%20double)方法采用以下参数:

  • 行索引,您要更改其高度的行的索引。
  • 行高应用于行的行高。
// 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");

设置所有行的高度

要为工作表中的所有行设置相同的行高,请使用Cells收藏的设置标准高度方法。

// 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");

使用列

设置列宽

通过调用设置列的宽度Cells收藏的[设置列宽](https://reference.aspose.com/cells/java/com.aspose.cells/cells#setColumnWidth(int,%20double)) 方法。这[设置列宽](https://reference.aspose.com/cells/java/com.aspose.cells/cells#setColumnWidth(int,%20double)方法采用以下参数:

  • 列索引,您要更改其宽度的列的索引。
  • 列宽所需的列宽。
// 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");

设置所有列的宽度

要为工作表中的所有列设置相同的列宽,请使用Cells收藏的设置标准宽度方法。

// 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");