行と列の書式設定
Contents
[
Hide
]
スプレッドシートを操作して行または列にデータを追加する場合、行の高さまたは列の幅を変更する必要がある場合があります。行または列に書式を適用すると、データを表示するために現在の高さまたは幅を変更する必要がある場合があります。通常、ユーザーは Microsoft Excel を使用して WYSIWYG 環境で行の高さと列の幅を調整します。ただし、Aspose.Cells を使用すると、開発者は実行時にこれらの操作を実行できます。
行の操作
行の高さを調整する
Aspose.Cells はクラスを提供し、ワークブック、Microsoft Excel ファイルを表します。のワークブッククラスにはワークシート コレクションこれにより、Excel ファイル内の各ワークシートにアクセスできます。ワークシートは、ワークシートクラス。のワークシートクラスはCellsワークシート内のすべてのセルを表すコレクション。
のCellsコレクションには、ワークシートの行または列を管理するためのメソッドがいくつか用意されています。これらのいくつかについては、以下で詳しく説明します。
行の高さの設定
を呼び出して、単一の行の高さを設定することができます。CellsコレクションのSetRowHeight方法。のSetRowHeightメソッドは、次のように次のパラメーターを取ります。
- 行インデックス、高さを変更する行のインデックス。
- 行の高さ、行に適用する行の高さ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// 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]; | |
// Setting the height of the second row to 13 | |
worksheet.Cells.SetRowHeight(1, 13); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
ワークシートのすべての行の高さの設定
開発者がワークシートのすべての行に同じ行の高さを設定する必要がある場合は、標準高さのプロパティCellsコレクション。
例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// 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]; | |
// Setting the height of all rows in the worksheet to 15 | |
worksheet.Cells.StandardHeight = 15; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
列の操作
列の幅の設定
を呼び出して、列の幅を設定します。Cellsコレクションの列幅の設定方法。の列幅の設定メソッドは次のパラメータを取ります。
- 列インデックス、幅を変更する列のインデックス。
- 列幅、目的の列幅。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// 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]; | |
// Setting the width of the second column to 17.5 | |
worksheet.Cells.SetColumnWidth(1, 17.5); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
列幅をピクセル単位で設定する
を呼び出して、列の幅を設定します。CellsコレクションのSetColumnWidthPixel方法。のSetColumnWidthPixelメソッドは次のパラメータを取ります。
- 列インデックス、幅を変更する列のインデックス。
- 列幅、目的の列幅 (ピクセル単位)。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
string outDir = RunExamples.Get_OutputDirectory(); | |
//Load source Excel file | |
Workbook workbook = new Workbook(sourceDir + "Book1.xlsx"); | |
//Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Set the width of the column in pixels | |
worksheet.Cells.SetColumnWidthPixel(7, 200); | |
workbook.Save(outDir + "SetColumnWidthInPixels_Out.xlsx"); |
ワークシートのすべての列の幅を設定する
ワークシートのすべての列に同じ列幅を設定するには、Cellsコレクションの標準幅財産。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// 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]; | |
// Setting the width of all columns in the worksheet to 20.5 | |
worksheet.Cells.StandardWidth = 20.5; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |