Excelファイルの行と列の挿入と削除
序章
新しいワークシートをゼロから作成する場合でも、既存のワークシートで作業する場合でも、より多くのデータを収容するために余分な行または列を追加する必要がある場合があります。逆に、ワークシートの指定された位置から行または列を削除する必要がある場合もあります。 これらの要件を満たすために、Aspose.Cells は、以下で説明する非常に単純なクラスとメソッドのセットを提供します。
行と列を管理する
Aspose.Cells はクラスを提供しますワークブック、Microsoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。ワークシートは、ワークシートクラス。のワークシートクラスはCellsワークシート内のすべてのセルを表すコレクション。
のCellsコレクションには、ワークシートの行と列を管理するいくつかのメソッドが用意されています。これらのいくつかを以下で説明します。
行と列を挿入する
行を挿入する
を呼び出して、ワークシートの任意の場所に行を挿入します。行の挿入の方法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); | |
// 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]; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRow(2); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
複数の行を挿入
複数の行をワークシートに挿入するには、InsertRowsの方法Cellsコレクション。のInsertRowsメソッドは次の 2 つのパラメーターを取ります。
- 行インデックス。新しい行が挿入される行のインデックス。
- 行数、挿入する必要がある行の総数。
// 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]; | |
// Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.Cells.InsertRows(2, 10); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
書式を設定して行を挿入する
書式設定オプションを含む行を挿入するには、InsertRowsかかるオーバーロード挿入オプションパラメータとして。をセットするCopyFormatTypeのプロパティ挿入オプションクラスCopyFormatType列挙。のCopyFormatTypeEnumeration には、以下に示す 3 つのメンバーがあります。
- SameAsAbove: 上の行と同じように行をフォーマットします。
- SameAsBelow: 下の行と同じように行をフォーマットします。
- クリア: フォーマットをクリアします。
// 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 Formatting options | |
InsertOptions insertOptions = new InsertOptions(); | |
insertOptions.CopyFormatType = CopyFormatType.SameAsAbove; | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.Cells.InsertRows(2, 1, insertOptions); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "InsertingARowWithFormatting.out.xls"); | |
// 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); | |
// 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]; | |
// Inserting a column into the worksheet at 2nd position | |
worksheet.Cells.InsertColumn(1); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
行と列を削除する
複数の行を削除
ワークシートから複数の行を削除するには、行の削除の方法Cellsコレクション。の行の削除メソッドは次の 2 つのパラメーターを取ります。
- 行インデックス。行が削除される行のインデックス。
- 行数、削除する必要がある行の総数。
// 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.xlsx", FileMode.OpenOrCreate); | |
// 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]; | |
// Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.Cells.DeleteRows(2, 10); | |
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", 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]; | |
// Deleting a column from the worksheet at 5th position | |
worksheet.Cells.DeleteColumn(4); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xlsx"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |