Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
This section describes how to:
Create a new Excel file from scratch and add worksheet to it.
A worksheet is represented by the Worksheet class. The Worksheet class provides a wide range of properties and methods for managing worksheets.
To create a new Excel file programmatically:
//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("Adding Worksheet.xls");
The process of adding worksheets to a designer spreadsheet is the same as that of adding a new worksheet, except that the Excel file already exists so should be opened before worksheets are added. A designer spreadsheet can be opened by the Workbook class.
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("book1.xls", FileMode.Open);
//Instantiating a Workbook object
//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("Designer Spreadsheet.xls");
//Closing the file stream to free all resources
fstream.Close();
Access or get any worksheet by specifying its name or index.
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("WorksHeet Operations.xls", 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"];
To remove worksheets from a file, call the Worksheets collection' RemoveAt method. Pass the sheet name to the RemoveAt method to remove a specific worksheet.
//Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("WorksHeet Operations.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("Sheet3");
workbook.Save("WorksHeet Operations.xls");
Removing worksheets by name works well when the name of the worksheet is known. If you don’t know the worksheet’s name, use an overloaded version of the RemoveAt method that takes the sheet index of the worksheet instead of its sheet name.
//creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream("WorksHeet Operations.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(1);
workbook.Save("WorksHeet Operations.xls");
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.