Introduction
In this topic, we will discuss the techniques to add or insert worksheets in an Excel file using Aspose.Grid.Desktop. The difference between adding and inserting worksheets is that in addition, a worksheet is simply added at the end of the worksheets collection of the Excel file however insertion means adding a worksheet to a specific position in the worksheets collection.
Adding a Worksheet
To add a worksheet using Aspose.Grid.Desktop, please follow the steps below:
- Add Aspose.Grid.Desktop control to your Form
- Call Add method of the Worksheets collection of GridDesktop control
Example:
[C#]
//Adding a worksheet to the Grid
Worksheet sheet = gridDesktop1.Worksheets.Add();
[VB.NET]
'Adding a worksheet to the Grid
Dim sheet As Worksheet = gridDesktop1.Worksheets.Add()
NOTE: Many overloaded versions of Add method are also available. Using the above overloaded version, a worksheet is simply added to the Excel file with a default sheet name. Using other overloaded versions of Add method, it is also possible to define the name, total number of rows and columns of the worksheet as shown below in the example.
Example:
[C#]
//Adding a worksheet to the Grid with a specific name
Worksheet sheet1 = gridDesktop1.Worksheets.Add("Sheet1");
//Adding a worksheet to the Grid with specific name, number of rows & columns
Worksheet sheet2 = gridDesktop1.Worksheets.Add("Sheet2",65536,256);
[VB.NET]
'Adding a worksheet to the Grid with a specific name
Dim sheet1 As Worksheet = gridDesktop1.Worksheets.Add("Sheet1")
'Adding a worksheet to the Grid with specific name, number of rows & columns
Dim sheet2 As Worksheet = gridDesktop1.Worksheets.Add("Sheet2",65536,256)
Inserting a Worksheet
To insert a worksheet using Aspose.Grid.Desktop, please follow the steps below:
- Add Aspose.Grid.Desktop control to your Form
- Call Insert method of the Worksheets collection of GridDesktop control
Example:
[C#]
//Inserting a worksheet to Grid at first position of the worksheets collection
Worksheet sheet = gridDesktop1.Worksheets.Insert(0);
[VB.NET]
'Inserting a worksheet to Grid at first position of the worksheets collection
Dim sheet As Worksheet = gridDesktop1.Worksheets.Insert(0)
Like Add method, Insert method also has many overloaded versions whose usage is also demonstrated in the example given below.
Example:
[C#]
//Inserting a worksheet to Grid at first position with a specific sheet name
Worksheet sheet1 = gridDesktop1.Worksheets.Insert(0,"Sheet1");
//Inserting a worksheet to Grid at first position with specific sheet name,
//number of rows and columns
Worksheet sheet2 = gridDesktop1.Worksheets.Insert(0,"Sheet2",65536,256);
[VB.NET]
'Inserting a worksheet to Grid at first position with a specific sheet name
Dim sheet1 As Worksheet = gridDesktop1.Worksheets.Insert(0,"Sheet1")
'Inserting a worksheet to Grid at first position with specific sheet name,
'number of rows and columns
Dim sheet2 As Worksheet = gridDesktop1.Worksheets.Insert(0,"Sheet2",65536,256)
IMPORTANT: As we know that Microsoft Excel supports Excel sheets containing 65536 rows and 256 columns at maximum. So, Aspose.Grid.Desktop also follows the same standards. In Aspose.Grid.Desktop control, developers can add or insert worksheets having more number of rows and columns than the standard limit of rows and columns in a worksheet but when they would try to save their Grid data to an Excel file, an exception will be thrown at runtime by the control. It means that only data contained in the 65536 rows and 256 columns can be saved to an Excel file using Aspose.Grid.Desktop.