行と列の自動調整
自動フィッティング
Aspose.Cells はワークブックMicrosoft Excel ファイルを表すクラス。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。ワークシートは、ワークシートクラス。のワークシートクラスには、ワークシートを管理するためのさまざまなプロパティとメソッドが用意されています。この記事では、ワークシート行または列を自動調整するクラス。
行の自動調整 - シンプル
行の幅と高さを自動サイズ変更する最も簡単な方法は、ワークシートクラス行の自動調整方法。の行の自動調整メソッドは、(サイズ変更する行の) 行インデックスをパラメーターとして受け取ります。
// 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(); |
Cells の範囲で行を自動調整
行は多くの列で構成されています。 Aspose.Cells を使用すると、開発者は、オーバーロードされたバージョンの行の自動調整方法。次のパラメータを取ります。
- 行インデックス、自動調整しようとしている行のインデックス。
- 最初の列のインデックス、行の最初の列のインデックス。
- 最後の列のインデックス、行の最後の列のインデックス。
の行の自動調整メソッドは、行内のすべての列の内容をチェックしてから、行を自動調整します。
// 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(); |
Cells の範囲で列を自動調整
列は多くの行で構成されています。オーバーロードされたバージョンの列の自動調整次のパラメータを取るメソッド:
- 列インデックス、自動調整しようとしている列のインデックス。
- 最初の行のインデックス、列の最初の行のインデックス。
- 最後の行のインデックス、列の最後の行のインデックス。
の列の自動調整メソッドは、列内のすべての行の内容をチェックしてから、列を自動調整します。
// 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(); |
マージされた Cells の行の自動調整
Aspose.Cells を使用すると、AutoFitterOptions API. AutoFitterOptionsクラスが提供するAutoFitMergedCellsType結合されたセルの行を自動調整するために使用できるプロパティ。AutoFitMergedCellsType受け入れるAutoFitMergedCellsType次のメンバーを持つ列挙可能。
- なし: 結合されたセルを無視します。
- 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選択した行/列を目的の行に自動調整しますAutoFitterOptionsによると。
前述のメソッドのシグネチャは次のとおりです。
- AutoFitRows(int startRow, int endRow,AutoFitterOptionsオプション)
- AutoFitColumns(int firstColumn, int lastColumn,AutoFitterOptionsオプション)