Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, learn to create Excel files in C# using IronXL, a library that supports the latest .NET framework versions. Start by installing IronXL via NuGet Package Manager. Import IronXL into your program file, allowing access to its features. Create a workbook using Workbook.Create
, with XLSX as the default format. Add sheets and set cell values or formulas programmatically. Customize cell styling, including borders and background colors. Assign values and properties to cell ranges. Enhance text with different font styles and sizes. Implement formulas with ease and protect sheets from editing. Finally, save the Excel file using the Save As
function. IronXL simplifies Excel operations without requiring Microsoft Excel or Excel Interop on your server, making it a versatile tool for developers.
// Import the IronXL namespace
using IronXL;
class ExcelExample
{
static void Main()
{
// Create a new workbook with a default sheet named "Sheet1"
var workbook = Workbook.Create();
// Add a new sheet to the workbook
var sheet = workbook.CreateWorkSheet("MySheet");
// Set value in a cell
sheet["A1"].Value = "Hello, IronXL!";
// Assign a formula to a cell
sheet["B1"].Formula = "=SUM(1, 2, 3)";
// Customize cell styling
var cell = sheet["A1"];
cell.Style.Font.Bold = true; // Make font bold
cell.Style.Fill.SetPattern(FillPatternStyle.Solid, Color.Cyan, Color.Empty);
// Define a range of cells and set a value
var range = sheet.GetRange("C1:D2");
range.Value = "Range Test";
// Set protection on the sheet
sheet.Protect();
// Save the workbook to the file system
workbook.SaveAs("example.xlsx");
// Inform user of completion
Console.WriteLine("Excel file 'example.xlsx' created successfully.");
}
}
// Import the IronXL namespace
using IronXL;
class ExcelExample
{
static void Main()
{
// Create a new workbook with a default sheet named "Sheet1"
var workbook = Workbook.Create();
// Add a new sheet to the workbook
var sheet = workbook.CreateWorkSheet("MySheet");
// Set value in a cell
sheet["A1"].Value = "Hello, IronXL!";
// Assign a formula to a cell
sheet["B1"].Formula = "=SUM(1, 2, 3)";
// Customize cell styling
var cell = sheet["A1"];
cell.Style.Font.Bold = true; // Make font bold
cell.Style.Fill.SetPattern(FillPatternStyle.Solid, Color.Cyan, Color.Empty);
// Define a range of cells and set a value
var range = sheet.GetRange("C1:D2");
range.Value = "Range Test";
// Set protection on the sheet
sheet.Protect();
// Save the workbook to the file system
workbook.SaveAs("example.xlsx");
// Inform user of completion
Console.WriteLine("Excel file 'example.xlsx' created successfully.");
}
}
Key Points:
Further Reading: IronXL Documentation