Initial commit of the ClosedXML library

Establishes the foundational structure, core Excel functionality, and tooling.

- Implements base components for cells, ranges, columns, and rows.
- Integrates a custom calculation engine with a wide range of Excel functions.
- Adds comprehensive support for styling, conditional formatting, data validation, comments, pictures, and charts.
- Configures build setup, project metadata (NuGet), and developer guidelines.
- Includes editor and Git attributes for consistent code style and line endings.
This commit is contained in:
Thom Lamb
2026-06-23 11:02:53 -05:00
parent 28bc05cf54
commit 2cd6df6481
1092 changed files with 97270 additions and 408 deletions
@@ -0,0 +1,29 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class DefaultStyles : IXLExample
{
public void Create(String filePath)
{
// Create our workbook
var workbook = new XLWorkbook();
// This worksheet will have the default style, row height, column width, and page setup
var ws1 = workbook.Worksheets.Add("Default Style");
// Change the default row height for all new worksheets in this workbook
workbook.RowHeight = 30;
var ws2 = workbook.Worksheets.Add("Tall Rows");
// Create a worksheet and change the default row height
var ws3 = workbook.Worksheets.Add("Short Rows");
ws3.RowHeight = 7.5;
workbook.SaveAs(filePath);
}
}
}
@@ -0,0 +1,19 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class PurpleWorksheet : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Purple Worksheet");
ws.Style.Fill.BackgroundColor = XLColor.Purple;
workbook.SaveAs(filePath);
}
}
}
+105
View File
@@ -0,0 +1,105 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class StyleAlignment : IXLExample
{
#region Variables
// Public
// Private
#endregion
#region Properties
// Public
// Private
// Override
#endregion
#region Constructors
// Public
public StyleAlignment()
{
}
// Private
#endregion
#region Events
// Public
// Private
// Override
#endregion
#region Methods
// Public
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Alignment");
var co = 2;
var ro = 1;
ws.Cell(++ro, co).Value = "Horizontal = Right";
ws.Cell(ro, co).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
ws.Cell(++ro, co).Value = "Indent = 2";
ws.Cell(ro, co).Style.Alignment.Indent = 2;
ws.Cell(++ro, co).Value = "JustifyLastLine = true";
ws.Cell(ro, co).Style.Alignment.JustifyLastLine = true;
ws.Cell(++ro, co).Value = "ReadingOrder = ContextDependent";
ws.Cell(ro, co).Style.Alignment.ReadingOrder = XLAlignmentReadingOrderValues.ContextDependent;
ws.Cell(++ro, co).Value = "RelativeIndent = 2";
ws.Cell(ro, co).Style.Alignment.RelativeIndent = 2;
ws.Cell(++ro, co).Value = "ShrinkToFit = true";
ws.Cell(ro, co).Style.Alignment.ShrinkToFit = true;
ws.Cell(++ro, co).Value = "TextRotation = 45";
ws.Cell(ro, co).Style.Alignment.TextRotation = 45;
ws.Cell(++ro, co).Value = "TopToBottom = true";
ws.Cell(ro, co).Style.Alignment.TopToBottom = true;
ws.Cell(++ro, co).Value = "Vertical = Center";
ws.Cell(ro, co).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
ws.Cell(++ro, co).Value = "WrapText = true";
ws.Cell(ro, co).Style.Alignment.WrapText = true;
workbook.SaveAs(filePath);
}
// Private
// Override
#endregion
}
}
+52
View File
@@ -0,0 +1,52 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class StyleBorder : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Border");
var co = 2;
var ro = 1;
ws.Cell(++ro, co).Value = "BottomBorder = Thick; BottomBorderColor = Red";
ws.Cell(ro, co).Style.Border.BottomBorder = XLBorderStyleValues.Thick;
ws.Cell(ro, co).Style.Border.BottomBorderColor = XLColor.Red;
ws.Cell(++ro, co).Value = "LeftBorder = Thick; LeftBorderColor = Blue";
ws.Cell(ro, co).Style.Border.LeftBorder = XLBorderStyleValues.Thick;
ws.Cell(ro, co).Style.Border.LeftBorderColor = XLColor.Blue;
ws.Cell(++ro, co).Value = "TopBorder = Thick; TopBorderColor = Yellow";
ws.Cell(ro, co).Style.Border.TopBorder = XLBorderStyleValues.Thick;
ws.Cell(ro, co).Style.Border.TopBorderColor = XLColor.Yellow;
ws.Cell(++ro, co).Value = "RightBorder = Thick; RightBorderColor = Black";
ws.Cell(ro, co).Style.Border.RightBorder = XLBorderStyleValues.Thick;
ws.Cell(ro, co).Style.Border.RightBorderColor = XLColor.Black;
ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalUp = true";
ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
ws.Cell(ro, co).Style.Border.DiagonalBorderColor = XLColor.Red;
ws.Cell(ro, co).Style.Border.DiagonalUp = true;
ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalDown = true";
ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
ws.Cell(ro, co).Style.Border.DiagonalBorderColor = XLColor.Red;
ws.Cell(ro, co).Style.Border.DiagonalDown = true;
ws.Cell(++ro, co).Value = "DiagonalBorder = Thin; DiagonalBorderColor = Red; DiagonalUp = true; DiagonalDown = true";
ws.Cell(ro, co).Style.Border.DiagonalBorder = XLBorderStyleValues.Thin;
ws.Cell(ro, co).Style.Border.DiagonalBorderColor = XLColor.Red;
ws.Cell(ro, co).Style.Border.DiagonalUp = true;
ws.Cell(ro, co).Style.Border.DiagonalDown = true;
workbook.SaveAs(filePath);
}
}
}
+27
View File
@@ -0,0 +1,27 @@
using ClosedXML.Excel;
using System;
namespace ClosedXML_Examples.Styles
{
public class StyleFill : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Fill");
var co = 2;
var ro = 1;
ws.Cell(++ro, co + 1).Value = "BackgroundColor = Red";
ws.Cell(ro, co).Style.Fill.BackgroundColor = XLColor.Red;
ws.Cell(++ro, co + 1).Value = "PatternType = DarkTrellis; PatternColor = Orange; BackgroundColor = Blue";
ws.Cell(ro, co).Style.Fill.PatternType = XLFillPatternValues.DarkTrellis;
ws.Cell(ro, co).Style.Fill.PatternColor = XLColor.Orange;
ws.Cell(ro, co).Style.Fill.BackgroundColor = XLColor.Blue;
workbook.SaveAs(filePath);
}
}
}
+53
View File
@@ -0,0 +1,53 @@
using ClosedXML.Excel;
using System;
namespace ClosedXML_Examples.Styles
{
public class StyleFont : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Font");
var co = 2;
var ro = 1;
ws.Cell(++ro, co).Value = "Bold";
ws.Cell(ro, co).Style.Font.Bold = true;
ws.Cell(++ro, co).Value = "FontColor - Red";
ws.Cell(ro, co).Style.Font.FontColor = XLColor.Red;
ws.Cell(++ro, co).Value = "FontFamilyNumbering - Script";
ws.Cell(ro, co).Style.Font.FontFamilyNumbering = XLFontFamilyNumberingValues.Script;
ws.Cell(++ro, co).Value = "FontCharSet - العربية التنضيد";
ws.Cell(ro, co).Style
.Font.SetFontName("Arabic Typesetting")
.Font.SetFontCharSet(XLFontCharSet.Arabic);
ws.Cell(++ro, co).Value = "FontName - Stencil";
ws.Cell(ro, co).Style.Font.FontName = "Stencil";
ws.Cell(++ro, co).Value = "FontSize - 15";
ws.Cell(ro, co).Style.Font.FontSize = 15;
ws.Cell(++ro, co).Value = "Italic - true";
ws.Cell(ro, co).Style.Font.Italic = true;
ws.Cell(++ro, co).Value = "Strikethrough - true";
ws.Cell(ro, co).Style.Font.Strikethrough = true;
ws.Cell(++ro, co).Value = "Underline - Double";
ws.Cell(ro, co).Style.Font.Underline = XLFontUnderlineValues.Double;
ws.Cell(++ro, co).Value = "VerticalAlignment - Superscript";
ws.Cell(ro, co).Style.Font.VerticalAlignment = XLFontVerticalTextAlignmentValues.Superscript;
ws.Column(co).AdjustToContents();
workbook.SaveAs(filePath);
}
}
}
@@ -0,0 +1,39 @@
using ClosedXML.Excel;
using System;
using System.Linq;
namespace ClosedXML_Examples.Styles
{
public class StyleIncludeQuotePrefix : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style IncludeQuotePrefix");
var data = Enumerable.Range(1, 20)
.Select(i =>
new
{
IntegerIndex = i,
StringIndex = i.ToString(),
PaddedString1000 = (i * 1000).ToString().PadLeft(8, '0'),
PrependedString1000 = "Str" + (i * 1000).ToString().PadLeft(8, '0')
});
ws.FirstCell().InsertData(data);
// Columns B to D will be of type text
// but column B will not have the leading quotation mark
ws.Column("B").Style.IncludeQuotePrefix = false;
// Columns C and D will have the leading quotation mark
ws.Column("C").Style.IncludeQuotePrefix = true;
ws.Column("D").Style.SetIncludeQuotePrefix();
ws.Columns().AdjustToContents();
workbook.SaveAs(filePath);
}
}
}
@@ -0,0 +1,85 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class StyleNumberFormat : IXLExample
{
#region Variables
// Public
// Private
#endregion
#region Properties
// Public
// Private
// Override
#endregion
#region Constructors
// Public
public StyleNumberFormat()
{
}
// Private
#endregion
#region Events
// Public
// Private
// Override
#endregion
#region Methods
// Public
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style NumberFormat");
var co = 2;
var ro = 1;
ws.Cell(++ro, co).Value = "123456.789";
ws.Cell(ro, co).Style.NumberFormat.Format = "$ #,##0.00";
ws.Cell(++ro, co).Value = "12.345";
ws.Cell(ro, co).Style.NumberFormat.Format = "0000";
ws.Cell(++ro, co).Value = "12.345";
ws.Cell(ro, co).Style.NumberFormat.NumberFormatId = 3;
ws.Column(co).AdjustToContents();
workbook.SaveAs(filePath);
}
// Private
// Override
#endregion
}
}
@@ -0,0 +1,34 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class StyleRowsColumns : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Rows and Columns");
//Set the entire worksheet's cells to be bold and with a light cyan background
ws.Style.Font.Bold = true;
ws.Style.Fill.BackgroundColor = XLColor.LightCyan;
// Set the width of all columns in the worksheet
ws.Columns().Width = 5;
// Set the height of all rows in the worksheet
ws.Rows().Height = 20;
// Let's play with the rows and columns
ws.Rows(2, 3).Style.Fill.BackgroundColor = XLColor.Blue;
ws.Columns(3, 4).Style.Fill.BackgroundColor = XLColor.Orange;
ws.Rows(5, 5).Style.Fill.BackgroundColor = XLColor.Pink;
ws.Row(6).Style.Fill.BackgroundColor = XLColor.Brown;
ws.Column("E").Style.Fill.BackgroundColor = XLColor.Gray;
workbook.SaveAs(filePath);
}
}
}
@@ -0,0 +1,42 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class StyleWorksheet : IXLExample
{
public void Create(String filePath)
{
var workbook = new XLWorkbook();
var ws = workbook.Worksheets.Add("Style Worksheet");
ws.Style.Font.Bold = true;
ws.Style.Font.FontColor = XLColor.Red;
ws.Style.Fill.BackgroundColor = XLColor.Cyan;
// The following cells will be bold and red
// because we've specified those attributes to the entire worksheet
ws.Cell(1, 1).Value = "Test";
ws.Cell(1, 2).Value = "Case";
// Here we'll change the style of a single cell
ws.Cell(2, 1).Value = "Default";
ws.Cell(2, 1).Style = XLWorkbook.DefaultStyle;
// Let's play with some rows
ws.Row(4).Style = XLWorkbook.DefaultStyle;
ws.Row(4).Height = 20;
ws.Rows(5, 6).Style = XLWorkbook.DefaultStyle;
ws.Rows(5, 6).Height = 20;
// Let's play with some columns
ws.Column(4).Style = XLWorkbook.DefaultStyle;
ws.Column(4).Width = 5;
ws.Columns(5, 6).Style = XLWorkbook.DefaultStyle;
ws.Columns(5, 6).Width = 5;
workbook.SaveAs(filePath);
}
}
}
+84
View File
@@ -0,0 +1,84 @@
using System;
using System.Drawing;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class UsingColors : IXLExample
{
public void Create(String filePath)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Using Colors");
Int32 ro = 0;
// From Known color
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.Red;
ws.Cell(ro, 2).Value = "XLColor.Red";
// From Color not so known
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.Byzantine;
ws.Cell(ro, 2).Value = "XLColor.Byzantine";
ro++;
// FromArgb(Int32 argb) using Hex notation
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(0xFF00FF);
ws.Cell(ro, 2).Value = "XLColor.FromArgb(0xFF00FF)";
// FromArgb(Int32 argb) using an integer (you need to convert the hex value to an int)
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(16711935);
ws.Cell(ro, 2).Value = "XLColor.FromArgb(16711935)";
// FromArgb(Int32 r, Int32 g, Int32 b)
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(255, 0, 255);
ws.Cell(ro, 2).Value = "XLColor.FromArgb(255, 0, 255)";
// FromArgb(Int32 a, Int32 r, Int32 g, Int32 b)
// Note: Excel ignores the alpha value
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromArgb(0, 255, 0, 255);
ws.Cell(ro, 2).Value = "XLColor.FromArgb(0, 255, 0, 255)";
ro++;
// FromColor(Color color)
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromColor(Color.Red);
ws.Cell(ro, 2).Value = "XLColor.FromColor(Color.Red)";
ro++;
// FromHtml(String htmlColor)
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromHtml("#FF996515");
ws.Cell(ro, 2).Value = "XLColor.FromHtml(\"#FF996515\")";
ro++;
// FromIndex(Int32 indexedColor)
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromIndex(25);
ws.Cell(ro, 2).Value = "XLColor.FromIndex(25)";
ro++;
// FromName(String colorName)
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromName("PowderBlue");
ws.Cell(ro, 2).Value = "XLColor.FromName(\"PowderBlue\")";
ro++;
// From Theme color
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromTheme(XLThemeColor.Accent1);
ws.Cell(ro, 2).Value = "XLColor.FromTheme(XLThemeColor.Accent1)";
// From Theme color with tint
ws.Cell(++ro, 1).Style.Fill.BackgroundColor = XLColor.FromTheme(XLThemeColor.Accent1, 0.5);
ws.Cell(ro, 2).Value = "XLColor.FromTheme(XLThemeColor.Accent1, 0.5)";
ws.Columns().AdjustToContents();
wb.SaveAs(filePath);
}
}
}
@@ -0,0 +1,41 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class UsingPhonetics : IXLExample
{
#region Methods
// Public
public void Create(String filePath)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Using Phonetics");
var cell = ws.Cell(1, 1);
// Phonetics are implemented as part of the Rich Text functionality. For more information see [Using Rich Text]
// First we add the text.
cell.RichText.AddText("みんなさんはお元気ですか。").SetFontSize(16);
// And then we add the phonetics
cell.RichText.Phonetics.SetFontSize(8);
cell.RichText.Phonetics.Add("げん", 7, 8);
cell.RichText.Phonetics.Add("き", 8, 9);
//TODO: I'm looking for someone who understands Japanese to confirm the validity of the above code.
wb.SaveAs(filePath);
}
// Private
// Override
#endregion
}
}
+121
View File
@@ -0,0 +1,121 @@
using System;
using ClosedXML.Excel;
namespace ClosedXML_Examples.Styles
{
public class UsingRichText : IXLExample
{
#region Variables
// Public
// Private
#endregion
#region Properties
// Public
// Private
// Override
#endregion
#region Constructors
// Public
// Private
#endregion
#region Events
// Public
// Private
// Override
#endregion
#region Methods
// Public
public void Create(String filePath)
{
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Rich Text");
// Let's start with a plain text and then decorate it...
var cell1 = ws.Cell(1, 1).SetValue("The show must go on...");
// We want everything in blue except the word show
// (which we want in red and with Courier Font)
cell1.Style.Font.FontColor = XLColor.Blue; // Set the color for the entire cell
cell1.RichText.Substring(4, 4)
.SetFontColor(XLColor.Red)
.SetFontName("Courier"); // Set the color and font for the word "show"
// On the next example we'll start with an empty cell and add the rich text
var cell = ws.Cell(3, 1);
// Add the text parts
cell.RichText.AddText("Hello").SetFontColor(XLColor.Red);
cell.RichText.AddText(" BIG ").SetFontColor(XLColor.Blue).SetBold();
cell.RichText.AddText("World").SetFontColor(XLColor.Red);
// Here we're showing that even though we added three pieces of text
// you can treat then like a single one.
cell.RichText.Substring(4, 7).SetUnderline();
// Right now cell.RichText has the following 5 strings:
//
// "Hell" -> Red
// "o" -> Red, Underlined
// " BIG " -> Blue, Underlined, Bold
// "W" -> Red, Underlined
// "orld" -> Red
// Of course you can loop through each piece of text and check its properties
foreach (var richText in cell.RichText)
{
if(richText.Bold)
ws.Cell(3, 2).Value = String.Format("\"{0}\" is Bold.", richText.Text);
}
// Now we'll build a cell with rich text, and some other styles
cell = ws.Cell(5, 1);
// Add the text parts
cell.RichText.AddText("Some").SetFontColor(XLColor.Green);
cell.RichText.AddText(" rich text ").SetFontColor(XLColor.Blue).SetBold();
cell.RichText.AddText("with a gray background").SetItalic();
cell.Style.Fill.SetBackgroundColor(XLColor.Gray);
ws.Cell(5, 2).Value = cell.RichText; // Should copy only rich text, but not background
ws.Columns().AdjustToContents();
wb.SaveAs(filePath);
}
// Private
// Override
#endregion
}
}