ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.
Second fix for https://github.com/ClosedXML/ClosedXML/releases/tag/0.100.0.
Added conversion for nullable numbers/DateTime/TimeSpan to XLCellValue
. Null values will be converted to Blank.Value
in the XLCellValue
. There is no value in having user code littered by cell.Value = nullableNumber ?? Blank.Value
.
Null strings assigned to XLCellValue will also be converted to Blank.Value
(unlike exception from 0.100).
Full Changelog: https://github.com/ClosedXML/ClosedXML/compare/0.100.1...0.100.2
This is a quick improvement for https://github.com/ClosedXML/ClosedXML/releases/tag/0.100.0.
It adds an implicit conversion for decimal numbers to XLCellValue
, so users who generate workbooks with decimal number don't have to add explicit casting from decimal to double everywhere.
Full Changelog: https://github.com/ClosedXML/ClosedXML/compare/0.100.0...0.100.1
These are release notes for a version 0.100. We skipped a few version since the last release (0.97), because 0.100 should denote a major change at the very heart of ClosedXML. Not as clean break as I hoped, but close enough.
The list of all things that were changed from 0.97 to 0.100 is at the migration guide at the https://closedxml.readthedocs.io/en/latest/migrations/migrate-to-0.100.html
This is more like list of you should upgrade despite breaking changes :)
Memory consumption during saving of large data workbooks was significantly improved. Originally, ClosedXML workbook representation was converted to DocumentFomrat.OpenXML DOM representation and the DOM was then saved. Instead of creating whole DOM, sheet data (=cell values) are now directly streamed to the output file and aren't included in the DOM.
To demonstrate difference, see the before and after memory consumption of a report that generated 30 000 rows, 45 columns. Memory consumption has decreased from 2.08 GiB 🡆 0.8 GiB.
Save cells and strings through DOM: 2.08 GiB
Save cell and strings through streaming: 0.8 GiB
The purple area are bytes of uncompressed package zip stream.
IXLCell.Value
and IXLCellValue.CachedValue
have now type XLCellValue
. At the core, xlsx consists of addressable cells with a functions that transform a set of values in source cells to different values in target cells. Is is really important to represent potential values of cells by a sane type. All other things, pivot tables, auto filter, graphs rely on this premise.
Cell value has been represented as string text and a value. The string depended on the value, e.g. 0/1 for boolean. That has been the case since the beginning of the ClosedXML project (see the original XLCell). The value was also returned as an Object
.
This approach has several drawbacks
Object
is not suitable representation of cell value. User had no idea what kind of values could be returned as a cell value. Everything could also break down, if a new type would be returned (e.g. XLError
).IXLColumn
.Value of a cell is not represented by a XLCellValue
structure. It is basically a union of one of possible types that can be value of a cell:
Since datetime and duration are basically masqaraded number, you can use XLCellValue.GetUnifiedNumber()
to get a backing number, no matter if the type is number, datetime and duration.
The structure contains implicit operators, as well as other methods to make transaction as seamless as possible
// Will use an implicit cast operator to convert string to XLCellValue and pass it to the Value setter
ws.Cell("A1").Value = "Text";
There is also a new singleton Blank.Value
that represent a blank value of a cell. Null is not blank. Empty string is not a blank value of a cell. Null instead of blank was considered and everything is just so much easier to work with, if blank is represented as a custom singleton type and not as a null.
XLCellValue
will be able to represent all values of a cell and won't be boxed/unboxed all the time.
ClosedXML used to guess a data type from a value. It caused all sort of unexpected behaviors (e.g. text value Z12.31 has been converted to date time 12/30/2022 19:00). Date caused most problems, but other sometimes too (e.g. text "Infinity" was detected as a number).
This behavior was likely intended to emulate how user interacts with an Excel. Excel guesses type, but only if the cell Number Format is set to "General" (e.g. if NumberFormat is set to Text, there is no conversion even in Excel). Application is not human and doesn't have to interact with xlsx in the same way.
This behavior was removed. Type that is set is the type that will be returned. Note that although XLCellValue
can represent date and time as a different types, in reality that is only presentation logic for user. They are both just serial date time numbers.
Cell value now can accurately represent error or a blank value.
ClosedXML used to throw on error value and cell couldn't contain an error. That was a significant problem, especially for formula calculation where formula referenced a cell that should contain an error value.
ClosedXML used to represent blank cell as an empty string, but no longer. It uses Blank.Value
singleton, wrapped in XLCellValue
. Also brings significant improvement in accuracy for CalcEngine evaluation.
Excel has a pretty complicated undocumented coercion process from text to number. It can convert fraction text (="1 1/2"*2
is 3), dates (e.g. ="1900-01-05"*2
is 10, though date format is culture specific), percent (e.g. ="100%"*2
), braces imply negative value (="(100%)"*2
= -2) and many more. That causes a significant problems for formula evaluation, especially if the source cell contains a date as a text, not as a date.
ClosedXml used to only convert test that looked like double
, it now coerces nearly everything Excel does. Coercion from dates should mostly work, but Excel has it's own database of acceptable formats and it's own format, while we rely on .NET Core infrastructure.
Thanks to incorporation of XLError
to core of CalcEngine, the exceptions are no longer necessary and have been removed. Error is a normal value type that is used during formula evaluation (e.g. ISNA
accepts it and VLOOKUP
returns it).
Technically speaking CalcEngine can still throw MissingContextException
, but only if evaluation is not called from a cell, but from method like XLWorkbook.Evaluate
. Functions like ROW
just can't work without the context of the cell.
If you ever tried to use CalcEngine, you have encountered a dreaded The function *SomeFunctionwas not recognised.
exception.
ClosedXML will no longer throw an exception on unimplemented function, but will return #NAME?
error instead. It has several reasons
=SOME.UNKNOWN.FUN(4)
, why should it throw on =LARGE(A1:A5,1)
?Basically, the exception doesn't bring any benefit and only imposes costs. User can report missing function on #NAME?
error just like on exception.
CalcEngine now can evaluate array literal expressions, so formulas like VLOOKUP(4, {1,2; 3,2; 5,3; 7,4}, 2)
now actually work.
Array processing is limited to argument parsing across formulas and CalcEngine still needs some love to process it work correctly. Array formulas are still not implemented.
Information and lookup functions were reimplemented to take advantage of other improvements. They should now be compliant with Excel (with exception of wildcard search for VLOOKUP).
Documentation is being moved from wiki to the ReadTheDocs. It has been there for since 2019, but we didn't actually had any documentation. Documentation is super important and ClosedXML lacks in that area. It is of course WIP, but it should improve over the time (see https://closedxml.readthedocs.io/en/latest/features/protect.html, https://closedxml.readthedocs.io/en/latest/features/cell-format.html#number-format or infamous https://closedxml.readthedocs.io/en/latest/tips/missing-font.html).
The move to ReadTheDocs has significant advantages:
We are not breaking the compatibility just because. Break imposes heavy penalty on users of the library. That makes it less likely to use it and that is definitely not the goal. Even the ClosedXML.Report must be fixed after every release.
That is not desirable situation. Version 1.0 and semantic versioning is certainly the goal. But it must be with an clear API that can endure some development between minor version. That is just not the case at the moment.
API will be reviewed along with the documentation and will be adjusted as necessary. ClosedXML will practice release early, release often. If breaking changes are not acceptable, stay on version that works and wait for 1.0 (though that will likely take at least a year, likely more... we are on a second decade).
Technically we do semver since forever, since Major version zero (0.y.z) is for initial development. Anything MAY change at any time. The public API SHOULD NOT be considered stable. ). Initial development for a decade /sigh.
Similar to current release, the general plan is to work on neglected foundational things and bug fixes.
It is likely there will be 0.100.x to fix whatever bugs XLCellValue caused that weren't convered by tests.
Pivot tables won't get any love in 0.101, but hopefully in the next one. It is one of distinguishing features of ClosedXML and it has a lot of reported issues.
Full Changelog: https://github.com/ClosedXML/ClosedXML/compare/0.97.0...0.100.0
Project has been on hiatus for a while, but things are now moving forward. See full list of changes at https://github.com/ClosedXML/ClosedXML/milestone/17?closed=1
System.Common.Drawing
were removed (use another overload):IXLPictures.AddPicture(Bitmap bitmap)
IXLPictures.AddPicture(Bitmap bitmap, String name)
IXLWorksheet.AddPicture(Bitmap bitmap)
IXLWorksheet.AddPicture(Bitmap bitmap, string name)
XLDataType.DateTime
or XLDataType.TimeSpan
. The DataType has to be set explicitely.We have removed a System.Drawing.Common dependency, it was deprecated and throws runtime exception when called on non-windows environments. All complexity has been hidden behind an interface IXLGraphicEngine
and a default implementation DefaultGraphicEngine
in the `ClosedXML.Graphics namespace. The default engine uses SixLabors.Fonts library for font measurements. You can read more on the Graphic Engine wiki page.
On non-windows environment, it will be necessary to specify a default font. Use this code
// All workbooks created later will use the engine with a fallback font DejaVu Sans
LoadOptions.DefaultGraphicsEngine = new DefaultGraphicEngine("DejaVu Sans"); // or Tahoma or any other font that is installed
ClosedXML has used a handcrafted parser for a while. The parse could parse a simple formulas, but a lot of features were out of its grasp (e.g. arrays, references to other worksheets, operations on references and so much more). We have replaced the original the original parser with the XLParser to facilitate a more powerful formulas.
You can try the parsing yourself on an online demo page: https://xlparser.perfectxl.nl/demo/
Through slower than the original parser, we are working with upstream to improve performance (https://github.com/spreadsheetlab/XLParser/issues/163, https://github.com/spreadsheetlab/XLParser/issues/161). Not-so-close future of CalcEngine is also multi threaded.
CalcEngine has been half-rewritten. It can now correctly represent all Excel types (e.g. Error is now a value, not an an exception) and perform operations on them (e.g. reference unions, comparisons work as they should).
As an example, SUM of two areas that overlap should count overlapping cells twice, thus the result should be 12, not 9.
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet();
ws.Range(1, 1, 3, 3).SetValue(1);
var sum = ws.Evaluate("SUM((A1:B3,B1:C3))");
Console.WriteLine($"Result of a SUM function: {sum}");
Result of a SUM function: 12
A major change has been implicit intersections in the semantic of 2019 excel (Excel 2021 and 365 already support dynamic array formulas):
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet();
ws.Cell("A1").SetValue(0);
ws.Cell("A2").SetValue(Math.PI / 4);
ws.Cell("A3").SetValue(Math.PI / 2);
var c1 = ws.Cell("C1");
c1.FormulaA1 = "SIN(A1:A3)";
var c2 = ws.Cell("C2");
c2.FormulaA1 = "SIN(A1:A3)";
var c3 = ws.Cell("C3");
c3.FormulaA1 = "SIN(A1:A3)";
Console.WriteLine($"C1: {c1.Value} C2: {c2.Value} C3: {c3.Value}");
C1: 0 C2: 0.7071067811865472 C3: 1
XLWorksheet.Evaluate
functions now have an optional parameters to specify a context where is formula evaluated. If formula requires a context and it is missing, it will throw a MissingContextException
.
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet();
var row = ws.Evaluate("ROW()", "A3"); // Needs A3 for the context, otherwise MissingContextException
Console.WriteLine($"Result of a ROW function: {row}");
We have added a support for ROW and COLUMN functions. They even return arrays (e.g. formula ROW(A2:D4)
return {2;3;4}
) , though it is difficult to see due to half revamped of CalcEngine.
using var wb = new XLWorkbook();
var ws = wb.AddWorksheet();
var sum = ws.Evaluate("SUM(COLUMN(B1:D2))");
Console.WriteLine($"Result: {sum}");
Result: 9
We hope to make a release every few months. For the next release, the general plan is to work on neglected things before new features.
See full list of changes at: https://github.com/ClosedXML/ClosedXML/pulls?q=is%3Apr+milestone%3Av0.96+is%3Aclosed
IXLCell.Hyperlink
removed; use GetHyperlink()
to get an existing hyperlink or to create a new one if it does not exist; instead of property setter, use SetHyperlink()
; to replace the existing hyperlink with a fresh one, use CreateHyperlink()
;IXLCell.DataValidation
removed; use GetDataValidation()
;IXLCell.NewDataValidation
removed; use CreateDataValidation()
;IXLCell.Comment
removed; use GetComment()
to access the existing comment or to create a new one if it does not exist; use CreateComment()
to replace the existing comment with the fresh one;IXLCell.RichText
removed; use GetRichText()
to access the existing rich text or to create a new one if it does not exist; use CreateRichText()
to replace the existing rich text with the fresh one.A1:B2
are now always treated as absolute, i.e. relative to A1
instead of relative to the top left cell of a given range.IXLSheetProtection
has been totally refactored and now uses a flag based enum to control the elements that are allowed or locked.IXLSheetProtection.IsProtected
setterXLClearOptions
and XLCellUsedOptions
's flags are now alignedIXLDataValidation.Ranges
made readonlyAddRange
, AddRanges
, ClearRanges
, RemoveRange
)#1063 Copy comment style correctly when shifting ranges
#1108 Correctly save empty cell with quote prefix (do not treat as empty)
#1125 Change cached value type when data type changes
#1132
#1015 Implement TEXTJOIN function (#1010) #1038 IXLSheetProtection refactoring #1054 Implement workbook FileSharing and a few of the properties #1058 Don't recalculate formula cached values after load #1077 Refactor digit and letter parsing to allow Unicode characters #1109 Don't automatically adjust columns width to contents when inserting datatable. Some users want to opt out because of PlatformNotSupportedException #1122 Add helper methods to add worksheet with default sheet name #1129 Alternative implementation of pivot table formats #1130 Enhance TRUNC function for additional parameter #1136 Implement NUMBERVALUE function #1151 Performance improvement to delete entire columns #1158 Check that print titles are valid row ranges or column ranges #1166 Ignore default Excel function namespace and implement CONCAT function #1169 Avoid creating new XLCell instances during range shifting #1170 Remove IXLSheetProtection.IsProtected public setter #1174 Correctly dispose temporary memorystream #1175 Sparklines implementation #1183 Improve Merge operation performance #1194 Disallow table name that is also a valid address (#1192) #1205 Improve performance of worksheet copying (#1188) #1208 optional range consolidation via save options #1219 Implement MATCH and INDEX functions #1224 Improve performance of loading workbooks having many merged ranges (#1220) #1227 Refactor comments #1232 Set versioning number to allow enhanced pivot table features #1237 Improve HLOOKUP and VLOOKUP performance #1240 Prevent copying of deleted worksheets. #1253 Replace licenseUrl (deprecated) with license tag #1278 Clear CellsCollection on WorksheetInternals disposal (#1276) #1284 Improvements for workbook saving (#1280) #1293 Remove the unnecessary check for VML elements not existing (#1285) #1326 PopulateAutoFilter range check (#1325) #1353 Implement % operator and correctly parse expressions with multiple unary operators (--, ++) #1357 Replace String.Compare with TryGetValue #1364 Fix issues occurred when running tests on .Net Core 3.0 #1368 Clear inlined rich text data on saving (#1361) #1376 CEILING and FLOOR fixes #1379 Explicitly set document type on saving (#1375) #1381 Exclude dependent SUBTOTAL formulas in SUBTOTAL evaluation #1388 Remove the dependency on FastMember. Improve performance of InsertData (#797) #1391 Replace ContainsKey with TryGetValue
A1:B2
are now always treated as absolute, i.e. relative to A1
instead of relative to the top left cell of a given range.IXLSheetProtection
has been totally refactored and now uses a flag based enum to control the elements that are allowed or locked.IXLSheetProtection.IsProtected
setterXLClearOptions
and XLCellUsedOptions
's flags are now alignedIXLSheetProtection
refactoringPlatformNotSupportedException
TRUNC
function for additional parameterNUMBERVALUE
functionIXLSheetProtection.IsProtected
public setter<c>
elements with missing r
attribute: XLAddress
constructor had parameters swappedDivisionByZeroException
when denominator is zero._X
(capital X) as XML escaping charInternals.RowsCollection
when adding commentXLCells
, XLRows
, XLColumns
and XLRanges
Thanks to @Pankraty for many PRs, as usual.
Binaries are available at:
ClosedXML
: https://ci.appveyor.com/project/ClosedXML/closedxml/builds/23591647/job/dyitdjnq6ta3nnpb/artifacts
ClosedXML.Signed
: https://ci.appveyor.com/project/ClosedXML/closedxml/builds/23591647/job/hk6hbr6kt807lvxf/artifacts
Also see the release notes for v0.94.0
ClosedXML
package now depends on FastMember
instead of FastMember.Signed
. The signed ClosedXML.Signed
still depends on FastMember.Signed
.IDisposable
interface removed from IXLWorksheet
. See #1049IXLPictures
interface has changed to accept parameters of type IXLCell
instead of IXLAddress
. See #930IXLBaseAutoFilter
removed and replaced with the existing IXLAutoFilter
. See #909IXLRange.ToString()
and IXLRangeAddress.ToString()
now return context aware range strings. See #1021VisibleRows
for autofilters.DataRange
when savingGuid
values to be inserted when dumping dataIXLTables.TryGetTable()
XLCell.TryGetValue
and implement additional testsTryGetValue_decimal_Good2test
environment-independentXLPivotField.ShowBlankItems
by default to false and fix treatment of defaults from loading fileXLConditionalFormat
equality comparer fails for ColorScale
RelId
default to null
CellsUsed
refactoring and fix