行および列の自動調整

Contents
[ ]

自動調整

Workbookクラスを提供しています。このクラスはMicrosoft Excelファイルを表します。Workbook クラスには、Excelファイル内の各ワークシートにアクセスするWorksheets コレクションが含まれています。

ワークシートは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.");

セルの範囲内のコンテンツに基づいて行を自動的に調整するためには、autoFitRow メソッドのオーバーロードバージョンを使用して行内のセル範囲で行を自動調整することができます。このメソッドには以下のパラメータが必要です:

行は多くの列で構成されています。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.");

結合されたセルの行のAutoFit

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のインスタンスを指定して、選択した行または列を自動調整することもできます。

上記のメソッドのシグネチャは次の通りです:

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

重要なこと