NanoXLSX

repository·master·Indexed 41 days ago

https://github.com/rabanti-github/nanoxlsx

A lightweight, dependency-free .NET library for creating and reading Microsoft Excel (.xlsx) files. It features a modular architecture consisting of NanoXLSX.Core for base logic, NanoXLSX.Reader for parsing workbooks, NanoXLSX.Writer for creating files, and optional modules for formatting and compatibility. Compatible with .NET Standard 2.0 and .NET 4.5 or newer.

Tokens
5.3K
Snippets
13
Records
26
Agent score
50%

What's inside NanoXLSX

  1. Overview of NanoXLSX.Writer

    master
    NanoXLSX.Writer is a component of the NanoXLSX library designed specifically for creating Microsoft Excel files in the XLSX format (Excel 2007 or newer). It provides the necessary tools to write workbooks and their constituent parts (such as worksheets and cells) directly to files or into memory streams.
  2. Understand NanoXLSX modules

    master

    NanoXLSX v3 uses a modular architecture. While the NanoXLSX meta-package is recommended for most users as it includes everything, you can pick specific modules:

    • NanoXLSX.Core: Mandatory. Contains workbooks, worksheets, cells, and styles. Has no external dependencies.
    • NanoXLSX.Reader: Optional. Provides extension methods to read/load XLSX files.
    • NanoXLSX.Writer: Optional. Provides extension methods to write/save XLSX files.
    • NanoXLSX.Formatting: Optional. Provides in-line cell formatting (rich text).
    • NanoXLSX.Compatibility: Optional. Handles compatibility features like external links.
  3. Understand NanoXLSX package dependencies and scopes

    master

    NanoXLSX is modular. While the meta package installs everything, you can choose specific modules based on your needs:

    • NanoXLSX.Core (Mandatory): Provides base functionalities including workbooks, worksheets, cells, and standard operations.
    • NanoXLSX.Reader (Optional): Adds functions to read workbooks, worksheets, styles, and shared strings.
    • NanoXLSX.Writer (Optional): Adds functions to write workbooks, worksheets, styles, and shared strings.
    • NanoXLSX.Formatting (Optional): Enables in-line cell formatting (rich text).
  4. Understand the purpose of NanoXLSX.Core

    master

    NanoXLSX.Core is the foundational component of the NanoXLSX library. It provides the core logic required to manipulate workbooks, worksheets, cells, and styles in the XLSX format.

    Important: This package is a logic-only library; it does not handle the actual reading or writing of files to disk. To perform file I/O, you must use additional plugin packages that depend on NanoXLSX.Core.

  5. Migrate ImportOptions to ReaderOptions in v3.0.0

    master

    The ImportOptions class has been renamed to ReaderOptions. When migrating to v3.0.0, update your code to use the new class name and note the following changes to constants and methods:

    Renamed Constants

    Old ImportOptions ConstantNew ReaderOptions Constant
    DEFAULT_TIMESPAN_FORMATDefaultTimeSpanFormat
    DEFAULT_DATETIME_FORMATDefaultDateTimeFormat

    Renamed Methods

    Old method nameNew method name
    EnforceValidColumnDimensionsEnforceStrictValidation
    EnforceValidRowDimensionsEnforceStrictValidation

    Removed Enum Values

    • ImportOptions.GlobalType.AllSingleToDecimal has been removed.
    • Replacement: Use ReaderOptions.GlobalType.AllNumbersToDecimal if the same behavior is required.
  6. Migrate internal reader namespaces in v3.0.0

    master

    If your code directly uses internal reader classes, you must update the namespace. All internal reader classes have moved from NanoXLSX.LowLevel to NanoXLSX.Internal.Readers.

    Warning: The architecture of these internal classes was redesigned from scratch in v3.0.0. If you have custom modifications to these classes, they will likely need to be redone.

    Recommended approach: Instead of modifying internal classes, use the introduced plugin architecture to implement custom readers that can replace or append to existing readers during the read process.

  7. Migrate from NanoXLSX v2.x to v3.0.0

    master

    NanoXLSX v3.0.0 introduced breaking changes to improve usability, consistency, and modularization. The most significant change is the modularization of the library into three distinct packages:

    1. NanoXLSX.Core: Contains core classes such as Workbook, Worksheet, Cell, and Style.
    2. NanoXLSX.Reader: Contains all reading functionalities.
    3. NanoXLSX.Writer: Contains all writing functionalities.

    When migrating, you must update your project references to include the specific package required for your task (reading or writing) instead of relying on a single monolithic package. Additionally, you may need to update namespaces (e.g., adding using NanoXLSX.Extensions; for reader extension methods) and adapt to renamed constants that now follow standard C# naming conventions.

    // Example of updated namespace usage for reader extensions
    using NanoXLSX.Extensions;
  8. Install NanoXLSX via NuGet

    master

    The recommended way to install NanoXLSX is using NuGet. You can install the meta-package NanoXLSX, which bundles all modules (Core, Reader, Writer, Formatting, and Compatibility), or install specific modules individually for advanced scenarios (e.g., NanoXLSX.Core + NanoXLSX.Writer for write-only applications).

    Use the Package Manager or .NET CLI to install the full bundle.

    # By Package Manager (PM)
    Install-Package NanoXLSX
    
    # By .NET CLI
    dotnet add package NanoXLSX
  9. Migrate Fill usage from v2.x to v3.0.0

    master

    The Fill class was overhauled to support flexible color definitions (ARGB, indexed, theme, or system colors).

    • Color Properties: Fill.BackgroundColor and Fill.ForegroundColor are now the Color class instead of string.
    • Indexed Colors: The IndexedColor property was removed. Use the Color class with IndexedColor.Value enum or integer indices.
    • Renamed Members: Constants and enums were renamed to PascalCase (e.g., FillType.patternColor $\rightarrow$ FillType.PatternColor).
    • Validation: Fill.ValidateColr(string, bool, bool) moved to NanoXLSX.Utils.Validators.ValidateColr(string, bool, bool).
    Fill fill = new Fill();
    fill.ForegroundColor = Color.CreateIndexed(IndexedColor.Value.Yellow);
    
    // ARGB
    Fill fill2 = new Fill();
    fill2.BackgroundColor = Color.CreateRgb("FF00FF00");
    
    // Theme color with tint
    Fill fill3 = new Fill();
    fill3.ForegroundColor = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.5f);
    
    // Implicit conversions
    Fill fill4 = "FF00FF00"; // Implicit SrgbColor
    Fill fill5 = IndexedColor.Value.Red2; // Implicit IndexedColor
    Fill fill6 = 64; // Implicit IndexedColor via index number
  10. Install the NanoXLSX meta package

    master

    The NanoXLSX meta package is the recommended way to install the library as it automatically includes the necessary dependencies for a full feature set. Installing this package provides access to core workbook operations, reading, writing, and cell formatting.

    # Use NuGet to install the meta package
    dotnet add package NanoXLSX
  11. Migrate Workbook usage from v2.x to v3.0.0

    master

    In version 3.0.0, the Workbook class underwent modularization.

    • Loading Workbooks: Workbook.Load(...) and Workbook.LoadAsync(...) have been removed. Use the Reader module instead.
    • Style Management: Workbook.AddStyle(Style) and Workbook.AddStyleComponent(Style, AbstractStyle) are removed. Apply styles directly to cells or ranges.
    • Removing Styles: Workbook.RemoveStyle(...) methods are removed. Use cell.RemoveStyle() instead.
    • Colors: Workbook.GetMruColors() now returns IReadOnlyList<Color> instead of string. The new Color class (namespace NanoXLSX.Colors) supports RGB, ARGB, system, theme, or indexed colors.
    // Old way (removed):
    // workbook.AddStyle(myStyle);
    
    // New way:
    workbook.CurrentWorksheet.Cells["A1"].CellStyle.CurrentFont.Bold = true;
    // OR
    workbook.CurrentWorksheet.Cells["A1"].CellStyle.Append(fontStyle);