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

Aspose.Cells はクラスを提供し、ワークブックこれは Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。

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

新しい Excel ファイルへのワークシートの追加

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

  1. のオブジェクトを作成しますワークブッククラス。
  2. 電話する追加の方法ワークシート コレクションクラス。空のワークシートが Excel ファイルに自動的に追加されます。新しいワークシートのシート インデックスをワークシートコレクション。
  3. ワークシート参照を取得します。
  4. ワークシートで作業を行います。
  5. を呼び出して、新しいワークシートを含む新しい Excel ファイルを保存します。ワークブッククラス'セーブ方法。
// 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);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet 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");

Designer スプレッドシートへのワークシートの追加

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

// 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);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet 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");

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

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

// 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);
string InputPath = dataDir + "book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Instantiating a Workbook object
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing a worksheet using its sheet name
Worksheet worksheet = workbook.Worksheets["Sheet1"];
Cell cell = worksheet.Cells["A1"];
Console.WriteLine(cell.Value);

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

ファイルからワークシートを削除するには、削除場所方法ワークシート コレクションクラス。シート名を削除場所特定のワークシートを削除するメソッド。

// 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);
// Removing a worksheet using its sheet name
workbook.Worksheets.RemoveAt("Sheet1");
// Save workbook
workbook.Save(dataDir + "output.out.xls");

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

名前によるワークシートの削除は、ワークシートの名前がわかっている場合にうまく機能します。ワークシートの名前がわからない場合は、オーバーロードされたバージョンの削除場所シート名の代わりにワークシートのシート インデックスを取得するメソッド。

// 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);
// Removing a worksheet using its sheet index
workbook.Worksheets.RemoveAt(0);
// Save workbook
workbook.Save(dataDir + "output.out.xls");

シートをアクティブ化し、ワークシートで Cell をアクティブにする

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

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

アンアクティブセル選択されたセル (入力を開始したときにデータが入力されるセル) です。一度にアクティブになるセルは 1 つだけです。アクティブ セルは、太い境界線で強調表示されます。

シートのアクティブ化と Cell のアクティブ化

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

水平または垂直スクロールバーが、特定のデータを表示したい行と列のインデックス位置にあることを確認するには、Aspose.Cells.Worksheet.FirstVisibleRowAspose.Cells.Worksheet.FirstVisibleColumnプロパティ。

次の例は、ワークシートをアクティブにして、その中にアクティブ セルを作成する方法を示しています。生成された出力では、スクロールバーがスクロールされて、2 番目の行と 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);
// Instantiate a new Workbook.
Workbook workbook = new Workbook();
// Get the first worksheet in the workbook.
Worksheet worksheet1 = workbook.Worksheets[0];
// Get the cells in the worksheet.
Cells cells = worksheet1.Cells;
// Input data into B2 cell.
cells[1, 1].PutValue("Hello World!");
// Set the first sheet as an active sheet.
workbook.Worksheets.ActiveSheetIndex = 0;
// Set B2 cell as an active cell in the worksheet.
worksheet1.ActiveCell = "B2";
// Set the B column as the first visible column in the worksheet.
worksheet1.FirstVisibleColumn = 1;
// Set the 2nd row as the first visible row in the worksheet.
worksheet1.FirstVisibleRow = 1;
// Save the excel file.
workbook.Save(dataDir + "output.xls");

先行トピック