How to Create New Spreadsheets
XLSX is a modern file format for storing Microsoft Excel spreadsheets. It uses the Open XML standard, introduced in Office 2007. XLSX supports advanced features like charts and conditional formatting and is widely used for data analysis and business tasks.
XLS is the older binary format for Excel files used in earlier versions. It lacks the features of XLSX and is now less common.
IronXL offers the capability to create both XLSX and XLS files with just a single line of code.
How to Create New Spreadsheets
- Download the C# library for creating spreadsheets
- Use the static
Create
method to create a spreadsheet - Choose whether to create an XLSX or XLS file
- Export the workbook and check the Excel file
- Use the CreatingOptions class to configure the workbook creation
Get started with IronXL
Start using IronXL in your project today with a free trial.
Create Spreadsheet Example
Use the static method Create
to create an Excel workbook that can be used to store a collection of sheets or worksheets. By default, this method will create an Excel workbook in XLSX format.
:path=/static-assets/excel/content-code-examples/how-to/create-spreadsheet-create-spreadsheet.cs
using IronXL;
// The following code demonstrates how to create a new spreadsheet using the IronXL library.
// Ensure that you have the IronXL library installed and properly referenced in your project
// to use its functionality for workbook creation and other spreadsheet operations.
class SpreadsheetExample
{
static void Main()
{
// Create a new workbook, which acts as a container for one or more worksheets.
WorkBook workBook = WorkBook.Create();
// Create a new worksheet within the workbook. Specify the name of the worksheet.
WorkSheet sheet = workBook.CreateWorkSheet("Sheet1");
// Example data to populate into the worksheet.
sheet["A1"].Value = "Hello, World!";
sheet["A2"].Value = "This is an example of using IronXL.";
// Save the workbook to a file. Specify the path where the file will be saved.
workBook.SaveAs("ExampleSpreadsheet.xlsx");
// Note: Additional operations to add more worksheets, populate additional data,
// and perform other manipulations can be added here as needed.
}
}
Imports IronXL
' The following code demonstrates how to create a new spreadsheet using the IronXL library.
' Ensure that you have the IronXL library installed and properly referenced in your project
' to use its functionality for workbook creation and other spreadsheet operations.
Friend Class SpreadsheetExample
Shared Sub Main()
' Create a new workbook, which acts as a container for one or more worksheets.
Dim workBook As WorkBook = WorkBook.Create()
' Create a new worksheet within the workbook. Specify the name of the worksheet.
Dim sheet As WorkSheet = workBook.CreateWorkSheet("Sheet1")
' Example data to populate into the worksheet.
sheet("A1").Value = "Hello, World!"
sheet("A2").Value = "This is an example of using IronXL."
' Save the workbook to a file. Specify the path where the file will be saved.
workBook.SaveAs("ExampleSpreadsheet.xlsx")
' Note: Additional operations to add more worksheets, populate additional data,
' and perform other manipulations can be added here as needed.
End Sub
End Class
Choose Spreadsheet Type
The Create
method also accepts an ExcelFileFormat
enum that you can use to specify whether you want to create an XLSX or XLS file. Both file formats are for storing Microsoft Excel spreadsheets, with XLSX being the modern, XML-based format introduced in Office 2007, while XLS is the older binary format used in earlier versions. XLS is now less common due to its limited features and lower efficiency compared to XLSX.
:path=/static-assets/excel/content-code-examples/how-to/create-spreadsheet-spreadsheet-type.cs
using IronXL;
try
{
// Create a new workbook in XLSX format.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a worksheet to the workbook.
WorkSheet sheet = workBook.CreateWorkSheet("Sheet1");
// Populate the worksheet with some data.
sheet["A1"].Value = "Hello";
sheet["B1"].Value = "World";
// Save the workbook to a specified path.
workBook.SaveAs("example.xlsx");
Console.WriteLine("Workbook created and saved successfully.");
}
catch (Exception ex)
{
// Handle potential exceptions, such as file I/O issues.
Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports IronXL
Try
' Create a new workbook in XLSX format.
Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
' Add a worksheet to the workbook.
Dim sheet As WorkSheet = workBook.CreateWorkSheet("Sheet1")
' Populate the worksheet with some data.
sheet("A1").Value = "Hello"
sheet("B1").Value = "World"
' Save the workbook to a specified path.
workBook.SaveAs("example.xlsx")
Console.WriteLine("Workbook created and saved successfully.")
Catch ex As Exception
' Handle potential exceptions, such as file I/O issues.
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
There is another overload for the Create
method, which takes CreatingOptions
as a parameter. However, the CreatingOptions
class currently has only one property, DefaultFileFormat
, which is used to configure whether to create an XLSX or XLS file. Check the code below for reference:
:path=/static-assets/excel/content-code-examples/how-to/create-spreadsheet-spreadsheet-type.cs
using IronXL;
try
{
// Create a new workbook in XLSX format.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Add a worksheet to the workbook.
WorkSheet sheet = workBook.CreateWorkSheet("Sheet1");
// Populate the worksheet with some data.
sheet["A1"].Value = "Hello";
sheet["B1"].Value = "World";
// Save the workbook to a specified path.
workBook.SaveAs("example.xlsx");
Console.WriteLine("Workbook created and saved successfully.");
}
catch (Exception ex)
{
// Handle potential exceptions, such as file I/O issues.
Console.WriteLine($"An error occurred: {ex.Message}");
}
Imports IronXL
Try
' Create a new workbook in XLSX format.
Dim workBook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
' Add a worksheet to the workbook.
Dim sheet As WorkSheet = workBook.CreateWorkSheet("Sheet1")
' Populate the worksheet with some data.
sheet("A1").Value = "Hello"
sheet("B1").Value = "World"
' Save the workbook to a specified path.
workBook.SaveAs("example.xlsx")
Console.WriteLine("Workbook created and saved successfully.")
Catch ex As Exception
' Handle potential exceptions, such as file I/O issues.
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
Frequently Asked Questions
What is the difference between XLSX and XLS file formats?
XLSX is a modern, XML-based file format introduced in Office 2007, supporting advanced features like charts and conditional formatting. XLS is the older binary format with fewer features and is less efficient.
How can I create a new Excel workbook using IronXL?
You can create a new Excel workbook using IronXL by calling the static method 'Create' from the WorkBook class. This method defaults to creating an XLSX format workbook.
How do I specify the file format when creating a new workbook with IronXL?
You can specify the file format when creating a new workbook by using the 'ExcelFileFormat' enum in the 'Create' method of the WorkBook class.
What is the purpose of the CreatingOptions class in IronXL?
The CreatingOptions class in IronXL is used to configure workbook creation, specifically to set the default file format to either XLSX or XLS.
How do you add a new worksheet to a workbook in IronXL?
To add a new worksheet to a workbook in IronXL, use the 'CreateWorkSheet' method on your workbook object and provide the desired sheet name.
How can I save a workbook created with IronXL?
You can save a workbook created with IronXL by using the 'SaveAs' method on the WorkBook object, specifying the desired file path and name.
Is it necessary to install IronXL before creating spreadsheets?
Yes, you need to install the IronXL library from NuGet before you can create spreadsheets using its features.
Can I create both XLS and XLSX files using IronXL?
Yes, IronXL allows you to create both XLS and XLSX files, providing flexibility depending on your requirements.
What is the default file format when using the 'Create' method in IronXL?
The default file format when using the 'Create' method in IronXL is XLSX.
Where can I download the IronXL library?
You can download the IronXL library from NuGet, which is the package manager for .NET.