自动调整行和列

自动调整

Aspose.Cells提供了一个表示Microsoft Excel文件的类WorkbookWorkbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。

工作表由Worksheet类表示。Worksheet类提供了广泛的属性和方法来管理工作表。本文介绍如何使用Worksheet类来自动调整行或列。

自动调整行 - 简单

调整行的宽度和高度最直接的方法是调用Worksheet类的autoFitRow方法。autoFitRow方法将行索引(要调整大小的行)作为参数。

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

在单元格范围内自动调整行

一行由许多列组成。Aspose.Cells允许开发人员根据行内单元格范围中的内容自动调整行的大小,通过调用autoFitRow方法的重载版本。它接受以下参数:

  • 行索引,即要自动调整的行的索引。
  • 第一个列索引,即行的第一个列的索引。
  • 最后列索引,指行的最后一列的索引。

autoFitRow方法检查行中所有列的内容,然后自动调整行的大小。

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

自动调整列 - 简单

调整列宽度和高度最简单的方法是调用Worksheet类的autoFitColumn方法。autoFitColumn方法将列索引(要调整大小的列)作为参数。

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

自动调整单元格范围中的列宽

一列由许多行组成。可以调用 autoFitColumn 的重载版本方法,根据列中单元格范围的内容自动调整列的宽度,该方法接受以下参数:

  • 列索引,表示需要自动调整内容的列的索引
  • 第一行索引,表示列的第一行的索引
  • 最后一行索引,表示列的最后一行的索引

autoFitColumn 方法检查列中所有行的内容,然后自动调整列的宽度。

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

为合并单元格自动调整行高

使用 Aspose.Cells,甚至可以对已经合并的单元格进行行的自动调整,使用 AutoFitterOptions API。AutoFitterOptions 类提供 AutoFitMergedCellsType 属性,可以用于合并单元格的自动调整行。AutoFitMergedCellsType 接受 AutoFitMergedCellsType 枚举,其包括以下成员。

  • NONE: 忽略已合并的单元格。
  • FIRST_LINE: 只扩展第一行的高度。
  • LAST_LINE: 只扩展最后一行的高度。
  • EACH_LINE: 只扩展每行的高度。
// 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");

您也可以使用 autoFitRows 和 autoFitColumns 方法的重载版本,接受一系列行/列和 AutoFitterOptions 的实例,根据所需的 AutoFitterOptions 自动调整所选行/列。

上述方法的签名如下:

  1. autoFitRows(int startRow, int endRow, AutoFitterOptions options)
  2. autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)

重要知识