行および列の自動調整
自動調整
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."); |
セル範囲の列オートフィット
1つの列は多くの行で構成されています。列内の範囲のセルの内容に基づいて、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"); |
選択した行/列を所望のAutoFitterOptions で自動的にフィットするために、範囲の行/列とAutoFitterOptions インスタンスを受け入れるautoFitRows & autoFitColumns メソッドのオーバーロードされたバージョンも使用できます。
上記のメソッドのシグネチャは次の通りです:
- autoFitRows(int startRow, int endRow, AutoFitterOptions options)
- autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)