Microsoft Excelファイルのワークシートを管理します。

Aspose.Cellsは、Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイルの各ワークシートにアクセスできるworksheetsコレクションが含まれています。

ワークシートはWorksheetクラスで表されます。Worksheetクラスには、ワークシートを管理するための多くのプロパティやメソッドが用意されています。

新しい Excel ファイルにワークシートを追加する方法

プログラムで新しいExcelファイルを作成するには:

  1. Workbookクラスのオブジェクトを作成します。
  2. WorksheetCollectionクラスのaddメソッドを呼び出します。空のワークシートがExcelファイルに自動的に追加されます。新しいワークシートのシートインデックスをworksheetsコレクションに渡すことで参照できます。
  3. ワークシートの参照を取得します。
  4. ワークシートで作業を実行します。
  5. Workbookクラスのsaveメソッドを呼び出して、新しいワークシートで新しいExcelファイルを保存します。
from aspose.cells import Workbook
from os import os, path
# 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(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.out.xls")

デザイナースプレッドシートにワークシートを追加する方法

デザイナースプレッドシートにワークシートを追加するプロセスは、新しいワークシートを追加するプロセスと同じですが、既存のExcelファイルがあるため、ワークシートを追加する前に開く必要があります。デザイナースプレッドシートは、Workbookクラスによって開くことができます。

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(".")
InputPath = dataDir + "book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Adding a new worksheet to the Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Setting the name of the newly added worksheet
worksheet.name = "My Worksheet"
# Saving the Excel file
workbook.save(dataDir + "output.xlsx")

シート名を使用してワークシートにアクセスする方法

名前またはインデックスを指定して任意のワークシートにアクセスできます。

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(".")
InputPath = dataDir + "book1.xlsx"
# Creating a file stream containing the Excel file to be opened
fstream = open(InputPath, "rb")
# Instantiating a Workbook object
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing a worksheet using its sheet name
worksheet = workbook.worksheets.get("Sheet1")
cell = worksheet.cells.get("A1")
print(cell.value)

シート名を使用してワークシートを削除する方法

ファイルからワークシートを削除するには、WorksheetCollectionクラスのremove_by_nameメソッドを呼び出します。特定のワークシートを削除するには、remove_by_nameメソッドにシート名を渡します。

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)
# Removing a worksheet using its sheet name
workbook.worksheets.remove_by_name("Sheet1")
# Save workbook
workbook.save(dataDir + "output.out.xls")

シートインデックスを使用してワークシートを削除する方法

ワークシートの名前がわかっている場合は、名前を使用してワークシートを削除できます。ワークシートの名前がわからない場合は、シート名の代わりにワークシートのシートインデックスを取る remove_by_index メソッドを使用します。

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)
# Removing a worksheet using its sheet index
workbook.worksheets.remove_by_index(0)
# Save workbook
workbook.save(dataDir + "output.out.xls")

シートをアクティブにし、ワークシート内のセルをアクティブにする方法

時々、ユーザーがMicrosoft ExcelファイルをExcelで開いたときに特定のワークシートをアクティブで表示する必要があります。同様に、特定のセルをアクティブにし、スクロールバーをアクティブなセルを表示するように設定することがあります。 Aspose.Cellsはこれらのすべてのタスクを実行できます。

アクティブなシートとは、作業中のシートのことです。タブ上のアクティブなシートの名前は、デフォルトで太字になります。

アクティブなセルは選択されたセルであり、タイプを始めるとデータが入力されるセルです。1度に1つのセルがアクティブです。アクティブなセルは太い枠で強調表示されます。

シートをアクティブにし、セルをアクティブにする方法

Aspose.Cellsには、シートとセルをアクティブにするための特定のAPI呼び出しが用意されています。たとえば、ブック内でアクティブなシートを設定するのにAspose.Cells.WorksheetCollection.active_sheet_indexプロパティが役立ちます。 同様に、Aspose.Cells.Worksheet.active_cellプロパティはワークシート内のアクティブなセルを設定および取得するために使用されます。

水平または垂直のスクロールバーが特定のデータを表示するために行および列の索引位置にあることを確認するには、Aspose.Cells.Worksheet.first_visible_rowおよびAspose.Cells.Worksheet.first_visible_columnプロパティを使用します。

次の例は、ワークシートをアクティブ化し、その中のアクティブなセルにします。生成された出力では、スクロールバーは、2行と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(".")
# Instantiate a new Workbook.
workbook = Workbook()
# Get the first worksheet in the workbook.
worksheet1 = workbook.worksheets[0]
# Get the cells in the worksheet.
cells = worksheet1.cells
# Input data into B2 cell.
cells.get(1, 1).put_value("Hello World!")
# Set the first sheet as an active sheet.
workbook.worksheets.active_sheet_index = 0
# Set B2 cell as an active cell in the worksheet.
worksheet1.active_cell = "B2"
# Set the B column as the first visible column in the worksheet.
worksheet1.first_visible_column = 1
# Set the 2nd row as the first visible row in the worksheet.
worksheet1.first_visible_row = 1
# Save the excel file.
workbook.save(dataDir + "output.xls")