### Minimal End-to-End Example Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started A concise example demonstrating the basic workflow of creating a workbook, adding data, saving it, and then reading it back. ```csharp using NanoXLSX; using NanoXLSX.Extensions; using NanoXLSX.Styles; // Write Workbook wb = new Workbook("Demo.xlsx", "Data"); wb.CurrentWorksheet.AddNextCell("Name"); wb.CurrentWorksheet.AddNextCell("Score"); wb.CurrentWorksheet.GoToNextRow(); wb.CurrentWorksheet.AddNextCell("Alice"); wb.CurrentWorksheet.AddNextCell(95); wb.Save(); // Read back Workbook loaded = WorkbookReader.Load("Demo.xlsx"); foreach (var pair in loaded.CurrentWorksheet.Cells) Console.WriteLine($"{pair.Key}: {pair.Value.Value}"); ``` -------------------------------- ### Create a Workbook Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Example of creating a new workbook with a specified filename and worksheet name. ```csharp Workbook workbook = new Workbook("MyFile.xlsx", "Sheet1"); ``` -------------------------------- ### Add Formulas Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Examples of adding formula cells to the worksheet. ```csharp workbook.CurrentWorksheet.AddNextCellFormula("B1*22"); workbook.CurrentWorksheet.AddNextCellFormula("ROUNDDOWN(A2,1)"); workbook.CurrentWorksheet.AddNextCellFormula("PI()"); ``` -------------------------------- ### Styling Cells with BasicStyles Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Examples of applying predefined styles to individual cells and directly when inserting cells. ```csharp workbook.CurrentWorksheet.Cells["A1"].SetStyle(BasicStyles.Bold); workbook.CurrentWorksheet.Cells["B1"].SetStyle(BasicStyles.Italic); workbook.CurrentWorksheet.Cells["C1"].SetStyle(BasicStyles.BorderFrame); workbook.CurrentWorksheet.Cells["D1"].SetStyle(BasicStyles.DoubleUnderline); workbook.CurrentWorksheet.AddNextCell("Highlighted", BasicStyles.Bold); ``` -------------------------------- ### Add a Worksheet Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Example of adding a new worksheet to the workbook. ```csharp workbook.AddWorksheet("Sheet2"); ``` -------------------------------- ### Font Color Examples Source: https://github.com/rabanti-github/nanoxlsx/blob/master/MigrationGuide.md Examples demonstrating the usage of the new Color class for Font.ColorValue. ```csharp Font font = new Font(); font.ColorValue = Color.CreateIndexed(IndexedColor.Value.Yellow); // Different examples with other color types Font font2 = new Font(); font.ColorValue = Color.CreateRgb("FF00FF00"); // ARGB Font font3 = new Font(); font.ColorValue = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.5f); // Theme color with tint // Implicit creation of specific color from a string (ARGB): Font font4 = new Font(); font4.ColorValue = "FF00FF00"; // Implicit conversion to SrgbColor // Implicit creation of font from an int (indexed color): Font font5 = new Font(); Font font5.ColorValue = IndexedColor.Value.Black; // Implicit conversion to IndexedColor as background // Implicit creation of specific color from an int (color index): Font font6 = new Font(); font6.ColorValue = 63; // Implicit conversion to IndexedColor // Implicit creation of specific color from an enum value (color index): Font font7 = new Font(); font7.ColorValue = IndexedColor.Value.Magenta; // Implicit conversion to IndexedColor enum value ``` -------------------------------- ### Fill Class Migration Examples Source: https://github.com/rabanti-github/nanoxlsx/blob/master/MigrationGuide.md Examples demonstrating the usage of the Fill class after version 3.0.0, including various color types and implicit conversions. ```csharp Fill fill = new Fill(); fill.ForegroundColor = Color.CreateIndexed(IndexedColor.Value.Yellow); // Different examples with other color types: Fill fill2 = new Fill(); fill2.BackgroundColor = Color.CreateRgb("FF00FF00"); // ARGB Fill fill3 = new Fill(); fill3.ForegroundColor = Color.CreateTheme(Theme.ColorSchemeElement.Accent1, 0.5f); // Theme color with tint // Implicit creation of fill from a string (ARGB): Fill fill4 = "FF00FF00"; // Implicit conversion to SrgbColor as foreground // Implicit creation of fill from an int (indexed color): Fill fill5 = IndexedColor.Value.Red2; // Implicit conversion to IndexedColor as foreground // Implicit creation of fill from an int (indexed color): Fill fill6 = 64; // Implicit conversion to IndexedColor as foreground, using the index number // Implicit creation of specific color from a string (ARGB): Fill fill7 = new Fill(); fill7.ForegroundColor = "FF00FF00"; // Implicit conversion to SrgbColor // Implicit creation of fill from an int (indexed color): Fill fill8 = new Fill(); Fill fill8.BackgroundColor = IndexedColor.Value.Black; // Implicit conversion to IndexedColor as background // Implicit creation of specific color from an int (color index): Fill fill9 = new Fill(); fill9.BackgroundColor = 63; // Implicit conversion to IndexedColor ``` -------------------------------- ### Create a Workbook in Stream Mode Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Example of creating a workbook in stream mode for output without a filename. ```csharp Workbook workbook = new Workbook(true); // true = use stream mode ``` -------------------------------- ### Switch Active Worksheet Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Example of switching the active worksheet by its name. ```csharp workbook.SetCurrentWorksheet("Sheet1"); ``` -------------------------------- ### Quick Start (shortened syntax) Source: https://github.com/rabanti-github/nanoxlsx/blob/master/README.md A C# code example demonstrating the basic usage of NanoXLSX to create, modify, and save a workbook. ```csharp Workbook workbook = new Workbook("myWorkbook.xlsx", "Sheet1"); // Create new workbook with a worksheet called Sheet1 workbook.WS.Value("Some Data"); // Add cell A1 workbook.WS.Formula("=A1"); // Add formula to cell B1 workbook.WS.Down(); // Go to row 2 workbook.WS.Value(DateTime.Now, Style.BasicStyles.Bold); // Add formatted value to cell A2 workbook.Save(); // Save the workbook as myWorkbook.xlsx ``` -------------------------------- ### Address Casting Example Source: https://github.com/rabanti-github/nanoxlsx/blob/master/MigrationGuide.md Example demonstrating how to define an Address object using an explicit cast from a string. ```cs sting addressString = "A15"; Address address = (Address)addressString; ``` -------------------------------- ### Load from a stream Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Loads a workbook from an open FileStream. ```csharp using (FileStream fs = new FileStream("MyFile.xlsx", FileMode.Open)) { Workbook wb = WorkbookReader.Load(fs); // use wb … } ``` -------------------------------- ### Insert Cell Range Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Example of inserting a list of values into a range of cells. ```csharp var headers = new List { "Name", "Age", "City" }; workbook.CurrentWorksheet.AddCellRange(headers, new Address(0, 0), new Address(2, 0)); // A1:C1 ``` -------------------------------- ### Range Implicit Casting Example Source: https://github.com/rabanti-github/nanoxlsx/blob/master/MigrationGuide.md Example demonstrating how to define a Range object using an implicit cast from a string. ```cs Range range = "A2:D15"; ``` -------------------------------- ### Building a custom style Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Demonstrates how to create a new style, modify its fill, font, and alignment properties, and apply it to a cell. It also shows how to copy and partially modify an existing style, and how to start from a predefined style and adjust its properties. ```csharp Style s = new Style(); s.CurrentFill.SetColor("FF22FF11", Fill.FillType.FillColor); // green fill s.CurrentFont.Underline = Font.UnderlineValue.Double; s.CurrentCellXf.HorizontalAlign = CellXf.HorizontalAlignValue.Center; workbook.CurrentWorksheet.Cells["B2"].SetStyle(s); ``` ```csharp Style s2 = s.CopyStyle(); s2.CurrentFont.Italic = true; workbook.CurrentWorksheet.AddNextCell(false, s2); ``` ```csharp Style s3 = BasicStyles.Strike; s3.CurrentCellXf.TextRotation = 45; s3.CurrentCellXf.VerticalAlign = CellXf.VerticalAlignValue.Center; workbook.CurrentWorksheet.Cells["B4"].SetStyle(s3); ``` -------------------------------- ### Sequential Cell Insertion Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Demonstrates inserting various data types using AddNextCell and moving to the next row. ```csharp workbook.CurrentWorksheet.AddNextCell("Text"); // A1 → string workbook.CurrentWorksheet.AddNextCell(123); // B1 → integer workbook.CurrentWorksheet.AddNextCell(true); // C1 → bool workbook.CurrentWorksheet.AddNextCell(DateTime.Now); // D1 → DateTime workbook.CurrentWorksheet.AddNextCell(new TimeSpan(1, 30, 0)); // E1 → TimeSpan workbook.CurrentWorksheet.GoToNextRow(); workbook.CurrentWorksheet.AddNextCell(123.456d); // A2 → double ``` -------------------------------- ### Quick Start (regular syntax) Source: https://github.com/rabanti-github/nanoxlsx/blob/master/README.md Demonstrates how to create a new workbook, add cells with different data types, and save the workbook. ```csharp Workbook workbook = new Workbook("myWorkbook.xlsx", "Sheet1"); // Create new workbook with a worksheet called Sheet1 workbook.CurrentWorksheet.AddNextCell("Some Data"); // Add cell A1 workbook.CurrentWorksheet.AddNextCell(42); // Add cell B1 workbook.CurrentWorksheet.GoToNextRow(); // Go to row 2 workbook.CurrentWorksheet.AddNextCell(DateTime.Now); // Add cell A2 workbook.Save(); // Save the workbook as myWorkbook.xlsx ``` -------------------------------- ### Usage Example Source: https://github.com/rabanti-github/nanoxlsx/blob/master/llms.txt Demonstrates how to create a new workbook, add data to cells, and save the workbook. Also shows how to load an existing workbook. ```csharp Workbook workbook = new Workbook("myWorkbook.xlsx", "Sheet1"); workbook.CurrentWorksheet.AddNextCell("Some Data"); workbook.CurrentWorksheet.AddNextCell(42); workbook.CurrentWorksheet.GoToNextRow(); workbook.CurrentWorksheet.AddNextCell(DateTime.Now); workbook.Save(); \Reading a workbook Workbook workbook = WorkbookReader.Load("myWorkbook.xlsx"); ``` -------------------------------- ### Insert Cell at Specific Address Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Examples of inserting cells using string addresses and zero-based row/column indices. ```csharp workbook.CurrentWorksheet.AddCell("Hello", "B3"); // string address workbook.CurrentWorksheet.AddCell("World", 2, 4); // column 2, row 4 (zero-based → C5) ``` -------------------------------- ### Quick Start (read) Source: https://github.com/rabanti-github/nanoxlsx/blob/master/README.md Shows how to load an existing workbook, access its worksheet name, and iterate through its cells to print their content. ```csharp using NanoXLSX.Extensions; Workbook wb = WorkbookReader.Load("basic.xlsx"); // Read the workbook System.Console.WriteLine("contains worksheet name: " + wb.CurrentWorksheet.SheetName); foreach (KeyValuePair cell in wb.CurrentWorksheet.Cells) { System.Console.WriteLine("Cell address: " + cell.Key + ": content:'" + cell.Value.Value + "'"); } ``` -------------------------------- ### Set Cell Direction Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Demonstrates changing the cursor movement direction for a worksheet. ```csharp // Default: column-by-column (ColumnToColumn) workbook.CurrentWorksheet.CurrentCellDirection = Worksheet.CellDirection.ColumnToColumn; // Row-by-row (top to bottom within a column) workbook.CurrentWorksheet.CurrentCellDirection = Worksheet.CellDirection.RowToRow; // Disable automatic movement — every AddCell call requires an explicit address workbook.CurrentWorksheet.CurrentCellDirection = Worksheet.CellDirection.Disabled; ``` -------------------------------- ### Save asynchronously Source: https://github.com/rabanti-github/nanoxlsx/wiki/Getting-started Saves the workbook to its file asynchronously. This is useful for non-blocking operations in applications. ```csharp Workbook workbook = new Workbook("Report.xlsx", "Sheet1"); workbook.WS.Value("Hello"); workbook.WS.Value(42); await workbook.SaveAsync(); ``` -------------------------------- ### Quick Start (read) Source: https://github.com/rabanti-github/nanoxlsx/wiki/Home Read a workbook using WorkbookReader, print the worksheet name, and iterate through cells to print their addresses and content. ```csharp using NanoXLSX.Extensions; Workbook wb = WorkbookReader.Load("basic.xlsx"); // Read the workbook System.Console.WriteLine("contains worksheet name: " + wb.CurrentWorksheet.SheetName); foreach (KeyValuePair cell in wb.CurrentWorksheet.Cells) { System.Console.WriteLine("Cell address: " + cell.Key + ": content:'" + cell.Value.Value + "' "); } ```