Aspose如何用,第一步,Workbook

 

Worksheets

 

Managing worksheets using Aspose.Cells is as easy as ABC. In this section, we will describe that how can we:

  1. Create a new Excel file and add worksheet to it

 

Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains a Worksheets collection that allows to access each worksheet in the Excel file.

A worksheet is represented by the Worksheet class. Worksheet class provides a wide range of properties and methods to manage a worksheet. Let's see that how can we make use of these basic set of APIs.

  

1. Adding Worksheets to a New Excel File

 

To create a new Excel file programmatically, developers would need to create an object of Workbook class that represents an Excel file. Then developers can call Addmethod of the Worksheets collection. When we call Add method, an empty worksheet is added to the Excel file automatically, which can be referenced by passing the sheet index of the newly added worksheet to the Worksheets collection. After the worksheet reference is obtained, developers can work on their worksheets according to their requirements. After the work is done on the worksheets, developers can save their newly created Excel file with new worksheets by calling the Savemethod of the Workbook class.

Example:

[C#]
//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(saveFileDialog1.FileName);

 

你可能感兴趣的:(asp)