行と列の自動調整
自動調整
Aspose.CellsはMicrosoft Excelファイルを表すWorkbookクラスを提供します。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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto-fitting the 3rd row of the worksheet | |
worksheet.AutoFitRow(1); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
セル範囲内の行を自動調整する方法
行は複数の列から構成されています。Aspose.Cellsでは、AutoFitRowメソッドのオーバーロードバージョンを呼び出すことで、セル範囲内のコンテンツに基づいて行を自動調整することが可能です。このメソッドは、以下のパラメータを受け取ります:
- 行インデックス:自動調整される行のインデックス。
- 最初の列インデックス:行の最初の列のインデックス。
- 最後の列インデックス:行の最後の列のインデックス。
AutoFitRowメソッドは、行のすべての列の内容を確認してから行を自動調整します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto-fitting the 3rd row of the worksheet | |
worksheet.AutoFitRow(1, 0, 5); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
セル範囲内の列を自動調整する方法
列は複数の行から構成されています。列内のセル範囲のコンテンツに基づいて列を自動調整することが可能です。このためには、以下のパラメータを受け取るAutoFitColumnメソッドのオーバーロードバージョンを呼び出すことができます:
- 列インデックス:自動調整される列のインデックス。
- 最初の行インデックス:列の最初の行のインデックス。
- 最後の行インデックス:列の最後の行のインデックス。
AutoFitColumnメソッドは、列のすべての行の内容を確認してから列を自動調整します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
string InputPath = dataDir + "Book1.xlsx"; | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(InputPath, FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Auto-fitting the Column of the worksheet | |
worksheet.AutoFitColumn(4, 4, 6); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
結合セルの行を自動調整する方法
Aspose.Cellsでは、AutoFitterOptions APIを使用して結合されたセルの行を自動的にサイズ変更することが可能です。AutoFitterOptionsクラスは、結合セルの行を自動調整するために使用できるAutoFitMergedCellsTypeプロパティを提供しています。AutoFitMergedCellsTypeはAutoFitMergedCellsType列挙体を受け入れ、以下のメンバーを持っています。
- None: 結合セルを無視します。
- FirstLine: 最初の行の高さのみを拡張します。
- LastLine: 最後の行の高さのみを拡張します。
- EachLine: 各行の高さのみを拡張します。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Instantiate a new Workbook | |
Workbook wb = new Workbook(); | |
// Get the first (default) worksheet | |
Worksheet _worksheet = wb.Worksheets[0]; | |
// Create a range A1:B1 | |
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2); | |
// Merge the cells | |
range.Merge(); | |
// Insert value to the merged cell A1 | |
_worksheet.Cells[0, 0].Value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end"; | |
// Create a style object | |
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle(); | |
// Set wrapping text on | |
style.IsTextWrapped = true; | |
// Apply the style to the cell | |
_worksheet.Cells[0, 0].SetStyle(style); | |
// Create an object for AutoFitterOptions | |
AutoFitterOptions options = new AutoFitterOptions(); | |
// Set auto-fit for merged cells | |
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine; | |
// Autofit rows in the sheet(including the merged cells) | |
_worksheet.AutoFitRows(options); | |
// Save the Excel file | |
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx"); |
また、選択した行/列を自動調整するために、AutoFitRowsおよびAutoFitColumnsメソッドのオーバーロードバージョンを使用し、必要に応じてAutoFitterOptionsインスタンスを指定することもできます。
前述のメソッドのシグネチャは次のとおりです:
- AutoFitRows(int startRow, int endRow, AutoFitterOptions options)
- AutoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)