行と列のフォーマット
行で操作する
行の高さの調整方法
Aspose.CellsはMicrosoft Excelファイルを表すWorkbookクラスを提供します。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-.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コレクションのStandardHeightプロパティを使用して行の高さを設定できます。
例:
// 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コレクションのSetColumnWidthメソッドを呼び出すことで列の幅を設定する。SetColumnWidthメソッドは次のパラメータを取ります:
- 列インデックス:幅を変更する列のインデックス。
- 列の幅:設定したい列の幅。
// 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メソッドは次のパラメータを取ります:
- 列インデックス:幅を変更する列のインデックス。
- 列の幅、ピクセル単位での所望の列の幅。
// 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コレクションのStandardWidthプロパティを使用します。
// 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(); |