Excelファイルの行と列の挿入と削除

紹介

ワークシートをゼロから作成するか、既存のワークシートで作業する場合、さらなるデータを収容するために追加の行や列を必要とする場合があります。逆に、ワークシート内の特定の位置から行や列を削除する必要がある場合もあります。 これらの要件を満たすために、Aspose.Cells for Python via .NETでは、以下で説明する非常にシンプルなクラスとメソッドのセットを提供しています。

行と列の管理

Aspose.Cells for Python via .NETは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートへのアクセスを可能にするworksheetsコレクションが含まれています。ワークシートはWorksheetクラスによって表されます。Worksheetクラスは、ワークシート内のすべてのセルを表すcellsコレクションを提供します。

cells コレクションには、ワークシート内の行と列を管理するためのいくつかのメソッドが提供されています。そのうちのいくつかについて以下で説明します。

行と列の挿入

行の挿入方法

insert_row コレクションのcells メソッドを呼び出すことで、ワークシートの任意の位置に行を挿入できます。insert_row メソッドは新しい行が挿入される行のインデックスを取ります。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_row(2)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

複数の行を挿入する方法

ワークシートに複数の行を挿入するには、cells コレクションのinsert_rows メソッドを呼び出します。insert_rows メソッドは2つのパラメータを取ります:

  • 行インデックス、新しい行が挿入される行のインデックス。
  • 行数、挿入する必要がある行の合計数。
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Inserting 10 rows into the worksheet starting from 3rd row
worksheet.cells.insert_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

書式付きで行を挿入する方法

書式オプションを指定して行を挿入するには、パラメータとしてInsertOptionsを取るinsert_rows オーバーロードを使用します。InsertOptionsクラスのcopy_format_typeプロパティをCopyFormatType 列挙型で設定します。CopyFormatType 列挙型には次の3つのメンバーがあります。

  • SAME_AS_ABOVE:前の行と同じ形式にします。
  • SAME_AS_BELOW:次の行と同じ形式にします。
  • CLEAR:書式をクリアします。
from aspose.cells import CopyFormatType, InsertOptions, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Setting Formatting options
insertOptions = InsertOptions()
insertOptions.copy_format_type = CopyFormatType.SAME_AS_ABOVE
# Inserting a row into the worksheet at 3rd position
worksheet.cells.insert_rows(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 コレクションのinsert_column メソッドを呼び出すことで、ワークシートの任意の位置に列を挿入することもできます。insert_column メソッドは、新しい列が挿入される列のインデックスを取ります。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "book1.xls", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Inserting a column into the worksheet at 2nd position
worksheet.cells.insert_column(1)
# Saving the modified Excel file
workbook.save(dataDir + "output.out.xls")
# Closing the file stream to free all resources
fstream.close()

行と列の削除

複数の行を削除する方法

ワークシートから複数の行を削除するには、cells コレクションのdelete_rows メソッドを呼び出します。delete_rows メソッドは2つのパラメータを取ります:

  • 行インデックス、削除される行のインデックス。
  • 行数、削除する必要がある行の合計数。
from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "Book1.xlsx", "wb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Deleting 10 rows from the worksheet starting from 3rd row
worksheet.cells.delete_rows(2, 10)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()

列を削除する方法

ワークシートから任意の位置に列を削除するには、Cells コレクションのdelete_column メソッドを呼び出します。delete_column メソッドは削除する列のインデックスを取ります。

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "Book1.xlsx", "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Deleting a column from the worksheet at 5th position
worksheet.cells.delete_column(4)
# Saving the modified Excel file
workbook.save(dataDir + "output.xlsx")
# Closing the file stream to free all resources
fstream.close()