Introduction

Aspose.Cells FOSS for .NET is a pure managed-code, MIT-licensed library for reading, writing, and manipulating Excel .xlsx workbooks without any Microsoft Office dependency. Installed as the Aspose.Cells_FOSS NuGet package, it integrates into any .NET project — console apps, web APIs, background workers, and serverless functions.

This post provides a feature-by-feature walkthrough of the public API, showing what is possible and how to invoke each capability from C# code.


Feature Overview

Workbook and Worksheet Lifecycle

The entry point for every operation is the Workbook class. Construct one from scratch with new Workbook(), or load an existing .xlsx with new Workbook(filePath) or new Workbook(stream). Navigate sheets via Workbook.Worksheets, set Worksheet.Name and Worksheet.VisibilityType, and persist with Workbook.Save(). For damaged files, pass a LoadOptions instance with TryRepairPackage = true and TryRepairXml = true.

using Aspose.Cells_FOSS;

var options = new LoadOptions
{
    TryRepairPackage = true,
    TryRepairXml = true,
};

try
{
    _ = new Workbook("sample.xlsx", options);
}
catch (WorkbookLoadException exception)
{
    Console.WriteLine(exception.Message);
}

Cell Data and Formulas

Write values to cells with Cell.PutValue() — overloads accept string, int, double, decimal, bool, and DateTime. Read values back via Cell.Value, Cell.StringValue, and Cell.Formula. Set Excel formulas with the Cell.Formula property; the formula string is preserved on save and survives a reload cycle.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Cells["A1"].PutValue("Hello");
sheet.Cells["B1"].PutValue(123);
sheet.Cells["C1"].Formula = "=B1*2";

workbook.Save("hello.xlsx");

var loaded = new Workbook("hello.xlsx");
Console.WriteLine(loaded.Worksheets[0].Cells["C1"].Formula);
Console.WriteLine(loaded.Worksheets[0].Cells["C1"].StringValue);

Cell Styling

Apply Style objects to control font, fill, borders, number format, and alignment. Retrieve the current style with Cell.GetStyle(), mutate it, then commit with Cell.SetStyle(). For background fills, set Style.Pattern = FillPattern.Solid and assign a Color to Style.ForegroundColor. Use Cells.Merge() to span cell ranges.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var cell = workbook.Worksheets[0].Cells["A1"];
cell.PutValue("Styled");

var style = cell.GetStyle();
style.Font.Bold = true;
style.Pattern = FillPattern.Solid;
style.ForegroundColor = Color.FromArgb(255, 241, 196, 15);
cell.SetStyle(style);

Console.WriteLine($"{cell.StringValue} / Bold={cell.GetStyle().Font.Bold}");

Conditional Formatting

Attach conditional formatting rules to cell ranges via ConditionalFormattingCollection. Rules support FormatConditionType.CellValue, Expression, ColorScale, DataBar, and IconSet. Use OperatorType to specify comparison operators (Between, Equal, GreaterThan, etc.), assign a Style to matched cells, and configure priority and stop-if-true behaviour per rule.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];

var cfCollection = sheet.ConditionalFormattings[sheet.ConditionalFormattings.Add()];
cfCollection.AddArea(CellArea.CreateCellArea("A1", "A10"));
var rule = cfCollection[cfCollection.AddCondition(
    FormatConditionType.CellValue, OperatorType.Between, "3", "7")];
var style = rule.Style;
style.Pattern = FillPattern.Solid;
style.ForegroundColor = Color.FromArgb(255, 255, 199, 206);
style.Font.Bold = true;
rule.Style = style;

workbook.Save("conditional-formatting.xlsx");

Data Validation

Add input constraints to cells using Worksheet.Validations. Supported validation types include ValidationType.List, Decimal, Custom, and more. Configure input prompts (InputTitle, InputMessage) and error messages (ErrorTitle, ErrorMessage, ValidationAlertType). Use CellArea.CreateCellArea() to define the validated range and Validation.AddArea() to apply a rule to additional ranges.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Name = "Validation Sheet";

var listIdx = sheet.Validations.Add(CellArea.CreateCellArea("A1", "A3"));
var listVal = sheet.Validations[listIdx];
listVal.Type = ValidationType.List;
listVal.Formula1 = "\"Open,Closed\"";
listVal.IgnoreBlank = true;
listVal.InCellDropDown = true;
listVal.ShowInput = true;
listVal.InputTitle = "Status";
listVal.InputMessage = "Pick a status";
listVal.ShowError = true;
listVal.ErrorTitle = "Invalid";
listVal.ErrorMessage = "Choose from the list";

workbook.Save("validations-sample.xlsx");

Page Setup and Print Options

Configure print layout via Worksheet.PageSetup. Set margins (LeftMarginInch, RightMarginInch, TopMarginInch, BottomMarginInch), Orientation, PaperSize, scale, print area, title rows and columns, headers and footers, and page breaks. All settings survive a save-load round trip.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Name = "Print Sheet";
sheet.Cells["A1"].PutValue("Title");

var pageSetup = sheet.PageSetup;
pageSetup.Orientation = PageOrientationType.Landscape;
pageSetup.PaperSize = PaperSizeType.PaperA4;
pageSetup.PrintArea = "$A$1:$C$10";
pageSetup.PrintTitleRows = "$1:$2";
pageSetup.LeftHeader = "Left Header";
pageSetup.CenterFooter = "Center Footer";
pageSetup.PrintGridlines = true;
pageSetup.CenterHorizontally = true;

workbook.Save("page-setup-sample.xlsx");

Worksheet Settings and Protection

Control per-sheet display with Worksheet.ShowGridlines, ShowRowColumnHeaders, ShowZeros, RightToLeft, Zoom, and TabColor. Protect sheets with Worksheet.Protect() and configure fine-grained Protection flags (Objects, FormatCells, InsertRows, AutoFilter, SelectLockedCells). Manage row height, column width, hidden rows/columns, and merged regions via Cells.Rows, Cells.Columns, and Cells.Merge().

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var layout = workbook.Worksheets[0];
layout.Name = "Layout";
layout.VisibilityType = VisibilityType.Hidden;
layout.TabColor = Color.FromArgb(255, 34, 68, 102);
layout.ShowGridlines = false;
layout.Zoom = 85;
layout.Protect();
layout.Protection.FormatCells = true;
layout.Protection.InsertRows = true;

layout.Cells["A1"].PutValue("Merged");
layout.Cells.Rows[1].Height = 22.5d;
layout.Cells.Columns[0].Width = 18.25d;
layout.Cells.Merge(0, 0, 2, 2);

workbook.Save("worksheet-settings-sample.xlsx");

Add external, internal, and mailto: hyperlinks via HyperlinkCollection.Add(). Set Hyperlink.TextToDisplay and Hyperlink.ScreenTip for user-visible labels. Define named ranges at workbook or sheet scope using DefinedNameCollection.Add() — named ranges serve as stable formula anchors and chart data sources that survive file reload.

using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];

sheet.Cells["A1"].PutValue("Docs");
var link = sheet.Hyperlinks[sheet.Hyperlinks.Add("A1", 1, 1, "https://example.com/docs")];
link.TextToDisplay = "Docs";
link.ScreenTip = "External documentation";

var name = workbook.DefinedNames[workbook.DefinedNames.Add("PrimaryRange", "='Sheet1'!$A$1:$D$5")];
name.Comment = "Primary data range";

workbook.Save("hyperlinks-names.xlsx");
Console.WriteLine("Hyperlinks: " + sheet.Hyperlinks.Count);
Console.WriteLine("Defined names: " + workbook.DefinedNames.Count);

Quick Start

dotnet add package Aspose.Cells_FOSS
using Aspose.Cells_FOSS;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];
sheet.Cells["A1"].PutValue("Hello");
sheet.Cells["B1"].PutValue(123);
sheet.Cells["C1"].Formula = "=B1*2";
workbook.Save("output.xlsx");

Supported Formats

FormatExtensionReadWrite
Xlsx.xlsx
Auto(various)

Open Source & Licensing

Aspose.Cells FOSS for .NET is published under the MIT license and distributed via NuGet as Aspose.Cells_FOSS. It is suitable for open-source and commercial projects alike.


Getting Started