行の高さと列の幅の調整

行で操作する

行の高さを調整

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

Cellsコレクションには、ワークシート内の行や列を管理するためのいくつかのメソッドが用意されています。これについて以下で詳しく説明します。

行の高さの設定

Cells コレクションのsetRowHeight メソッドを呼び出すことで、単一の行の高さを設定することができます。setRowHeight メソッドには以下のパラメータが必要です:

  • 行インデックス:高さを変更する行のインデックス。
  • 行の高さ:行に適用する行の高さ。
// 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 コレクションのsetStandardHeight メソッドを使用します。

// 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 コレクションのsetColumnWidth メソッドを呼び出します。setColumnWidth メソッドには以下のパラメータが必要です:

  • 列インデックス:幅を変更する列のインデックス。
  • 列の幅:設定したい列の幅。
// 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 コレクションのsetStandardWidth メソッドを使用します。

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