行と列の自動調整

自動フィッティング

Aspose.Cells はクラスを提供し、ワークブック、Microsoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。

ワークシートは、ワークシートクラス。のワークシートクラスには、ワークシートを管理するためのさまざまなプロパティとメソッドが用意されています。この記事では、ワークシート行または列を自動調整するクラス。

行の自動調整 - シンプル

行の幅と高さを自動サイズ変更する最も簡単な方法は、ワークシートクラス'[autoFitRow](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitRow(int)) 方法。の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.");

Cells の範囲で行を自動調整

行は多くの列で構成されています。 Aspose.Cells を使用すると、開発者は、オーバーロードされたバージョンの[autoFitRow](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitRow(int,%20int,%20int)) 方法。次のパラメータを取ります。

  • 行インデックス、自動調整しようとしている行のインデックス。
  • 最初の列のインデックス、行の最初の列のインデックス。
  • 最後の列のインデックス、行の最後の列のインデックス。

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

列の自動調整 - シンプル

列の幅と高さを自動サイズ変更する最も簡単な方法は、ワークシートクラス'[autoFitColumn](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#autoFitColumn(int)) 方法。の[autoFitColumn](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 の範囲で列を自動調整

列は多くの行で構成されています。オーバーロードされたバージョンのautoFitColumn 以下のパラメータを取るメソッド:

  • 列インデックス、内容を自動調整する必要がある列のインデックスを表します
  • 最初の行のインデックス、列の最初の行のインデックスを表します
  • 最後の行のインデックス、列の最後の行のインデックスを表します

の[autoFitColumn](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.");

マージされた Cells の行の自動調整

Aspose.Cells を使用すると、AutoFitterOptions API. AutoFitterOptionsクラスが提供するAutoFitMergedCellsType結合されたセルの行を自動調整するために使用できるプロパティ。AutoFitMergedCellsType受け入れるAutoFitMergedCellsType次のメンバーを持つ列挙可能。

  • なし: 結合されたセルを無視します。
  • 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オプション)
  2. autoFitColumns(int firstColumn, int lastColumn,AutoFitterOptionsオプション)

知っておくべき重要事項