自动调整行和列
汽车配件
Aspose.Cells提供了一个类,工作簿,代表一个 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。
工作表由工作表班级。这工作表类提供了广泛的属性和方法来管理工作表。本文着眼于使用工作表自动调整行或列的类。
自动调整行 - 简单
自动调整行的宽度和高度的最直接的方法是调用工作表班级'[自动调整行](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitRow(int)) 方法。这[自动调整行](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitRow(int)方法将(要调整大小的行的)行索引作为参数。
// 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(AutoFitRowsandColumns.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); | |
// Auto-fitting the 2nd row of the worksheet | |
worksheet.autoFitRow(1); | |
// Auto-fitting the 1st column of the worksheet | |
worksheet.autoFitColumn(0); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "AutoFitRowsandColumns_out.xls"); | |
// Print message | |
System.out.println("Row and Column auto fit successfully."); |
Cells 范围内的 AutoFit 行
一行由许多列组成。 Aspose.Cells 允许开发人员通过调用[自动调整行](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitRow(int,%20int,%20int)) 方法。它采用以下参数:
- 行索引,即将被自动调整的行的索引。
- 第一列索引,该行第一列的索引。
- 最后一列索引,该行最后一列的索引。
这自动调整行 方法检查行中所有列的内容,然后自动调整行。
// 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(AutoFitRowsinaRangeofCells.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); | |
// Auto-fitting the row of the worksheet | |
worksheet.autoFitRow(1, 0, 5); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "AutoFitRowsinaRangeofCells_out.xls"); | |
// Print message | |
System.out.println("Row auto fit successfully."); |
自动调整列 - 简单
自动调整列的宽度和高度的最简单方法是调用工作表班级'[自动调整列](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumn(int)) 方法。这[自动调整列](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumn(int)方法将(即将调整大小的列的)列索引作为参数。
// 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(AutoFitRowsandColumns.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); | |
// Auto-fitting the 2nd row of the worksheet | |
worksheet.autoFitRow(1); | |
// Auto-fitting the 1st column of the worksheet | |
worksheet.autoFitColumn(0); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "AutoFitRowsandColumns_out.xls"); | |
// Print message | |
System.out.println("Row and Column auto fit successfully."); |
Cells 范围内的 AutoFit 列
一列由许多行组成。通过调用重载版本,可以根据列中单元格范围内的内容自动调整列[自动调整列](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumn(int,%20int,%20int)采用以下参数的方法:
- 列索引代表内容需要自动适应的列的索引
- 第一行索引代表该列第一行的索引
- 最后一行索引代表该列最后一行的索引
这[自动调整列](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumn(int,%20int,%20int)方法检查列中所有行的内容,然后自动调整列。
// 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(AutoFitColumnsinaRangeofCells.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); | |
// Auto-fitting the Column of the worksheet | |
worksheet.autoFitColumn(4, 4, 6); | |
// Saving the modified Excel file in default (that is Excel 2003) format | |
workbook.save(dataDir + "AutoFitColumnsinaRangeofCells_out.xls"); | |
// Print message | |
System.out.println("Columns auto fit successfully."); |
合并的 AutoFit 行 Cells
使用 Aspose.Cells,即使对于已使用合并的单元格,也可以自动调整行AutoFitter选项 API. AutoFitter选项类提供AutoFitMergedCellsType可用于自动调整合并单元格行的属性。AutoFitMergedCellsType接受AutoFitMergedCellsType具有以下成员的可枚举。
// 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(AutofitRowsforMergedCells.class) + "RowsAndColumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range A1:B1 | |
Range range = worksheet.getCells().createRange(0, 0, 1, 2); | |
// Merge the cells | |
range.merge(); | |
// Insert value to the merged cell A1 | |
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end"); | |
// Create a style object | |
Style style = worksheet.getCells().get(0, 0).getStyle(); | |
// Set wrapping text on | |
style.setTextWrapped(true); | |
// Apply the style to the cell | |
worksheet.getCells().get(0, 0).setStyle(style); | |
// Create an object for AutoFitterOptions | |
AutoFitterOptions options = new AutoFitterOptions(); | |
// Set auto-fit for merged cells | |
options.setAutoFitMergedCellsType(AutoFitMergedCellsType.EACH_LINE); | |
// Autofit rows in the sheet(including the merged cells) | |
worksheet.autoFitRows(options); | |
// Save the Excel file | |
workbook.save(dataDir + "AutofitRowsforMergedCells_out.xlsx"); |
您也可以使用的重载版本自动调整行 & [自动调整列](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumns() 方法接受一系列的行/列和一个实例AutoFitter选项使用所需的自动调整选定的行/列AutoFitter选项因此。
上述方法的签名如下:
- autoFitRows(int startRow,int endRow,AutoFitter选项选项)
- autoFitColumns(int firstColumn,int lastColumn,AutoFitter选项选项)