调整行高和列宽
Contents
[
Hide
]
使用电子表格并将数据添加到行或列时,您可能需要更改行高或列宽。有时,对行或列应用格式意味着当前的高度或宽度需要更改才能显示数据。通常,用户使用 Microsoft Excel 在所见即所得的环境中调整行高和列宽。但是,使用 Aspose.Cells 开发人员可以在运行时执行这些操作。行和列的索引将从 0 开始。
使用行
调整行高
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)方法采用以下参数:
- 行索引,您要更改其高度的行的索引。
- 行高应用于行的行高。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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收藏的设置标准高度方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)方法采用以下参数:
- 列索引,您要更改其宽度的列的索引。
- 列宽所需的列宽。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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收藏的设置标准宽度方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |