{"id":28122446,"url":"https://github.com/jjosh102/xtractxcel","last_synced_at":"2025-09-04T12:42:53.174Z","repository":{"id":279031131,"uuid":"937505892","full_name":"jjosh102/XtractXcel","owner":"jjosh102","description":"XtractXcel is a simple .NET library for extracting data from Excel files using ClosedXML, transforming it as needed, and loading it into your object.","archived":false,"fork":false,"pushed_at":"2025-04-23T10:39:22.000Z","size":755,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-23T11:33:36.142Z","etag":null,"topics":["csharp","excel","show"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jjosh102.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-02-23T08:10:22.000Z","updated_at":"2025-04-23T10:39:25.000Z","dependencies_parsed_at":"2025-02-23T10:25:23.877Z","dependency_job_id":"07cf6574-f92c-474d-b53e-daae90cc9bdd","html_url":"https://github.com/jjosh102/XtractXcel","commit_stats":null,"previous_names":["jjosh102/excel-transform-load","jjosh102/xtractxcel"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjosh102%2FXtractXcel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjosh102%2FXtractXcel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjosh102%2FXtractXcel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jjosh102%2FXtractXcel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jjosh102","download_url":"https://codeload.github.com/jjosh102/XtractXcel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101547,"owners_count":22014909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","excel","show"],"created_at":"2025-05-14T08:13:39.645Z","updated_at":"2025-05-14T08:13:55.778Z","avatar_url":"https://github.com/jjosh102.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XtractXcel\n\n[![NuGet](https://img.shields.io/nuget/v/XtractXcel.svg)](https://www.nuget.org/packages/XtractXcel)\n[![NuGet Downloads](https://img.shields.io/nuget/dt/XtractXcel?logo=nuget)](https://www.nuget.org/packages/XtractXcel)\n\n## Overview\n\nXtractXcel is a simple library for extracting data from Excel files using [ClosedXML](https://github.com/ClosedXML/ClosedXML), transforming it as needed, and loading it into your objects with minimal effort. It supports a variety of data types, flexible column mapping, and both attribute-based and manual mapping approaches.\n\n## Getting Started\n\n### Installing\n\nTo install the package add the following line inside your csproj file with the latest version.\n\n```xml\n\u003cPackageReference Include=\"XtractXcel\" Version=\"x.x.x\" /\u003e\n```\n\nAn alternative is to install via the .NET CLI with the following command:\n\n```xml\ndotnet add package XtractXcel\n```\n\n### Basic Usage\n\n#### 1. Define your model with ExcelColumn attributes\n\n```csharp\npublic class Person {\n    [ExcelColumn(\"Full Name\", \"Name\", \"Employee Name\")]\n    public string? Name { get; set; }\n    \n    [ExcelColumn(\"Age\", \"Employee Age\")]\n    public int? Age { get; set; }\n    \n    [ExcelColumn(\"Salary\")]\n    public decimal? Salary { get; set; }\n    \n    [ExcelColumn(\"Join Date\")]\n    public DateTime JoinDate { get; set; }\n    \n    [ExcelColumn(\"Last Active\", \"Last Activity\")]\n    public DateTime? LastActive { get; set; }\n}\n```\n\nThe `ExcelColumn` attribute maps Excel column headers to C# properties. You can provide multiple possible column names to handle variations in your Excel files gracefully.\n\n#### 2. Extract data from Excel\n\n##### From a Stream\n\n```csharp\n// Get a stream from a file, memory, or any other source\nusing var stream = File.OpenRead(\"employees.xlsx\");\n\n// Extract the data using a fluent API\nvar people = new ExcelExtractor()\n    .WithHeader(true)               // Excel file contains headers\n    .WithWorksheetIndex(1)          // Use the first worksheet (1-based index)\n    .FromStream(stream)             // Set the source stream\n    .Extract\u003cPerson\u003e();             // Perform the extraction\n\n// Use the extracted data\nforeach (var person in people) {\n    Console.WriteLine($\"Name: {person.Name}, Age: {person.Age}, Joined: {person.JoinDate:d}\");\n}\n```\n\n##### From a File\n\n```csharp\nvar people = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromFile(\"employees.xlsx\")\n    .Extract\u003cPerson\u003e();\n```\n\n## Advanced Features\n\n### Working with Files Without Headers\n\nFor Excel files without headers, you can use column position for extraction:\n\n```csharp\npublic class PersonNoHeader {\n    // No attributes needed - properties are mapped by column position (1-based)\n    // First column (A) maps to first property, second column (B) to second property, etc.\n    public string? Name { get; set; }\n    public int? Age { get; set; }\n    public decimal? Salary { get; set; }\n    public DateTime JoinDate { get; set; }\n    public DateTime? LastActive { get; set; }\n}\n\n// Extract the data\nvar people = new ExcelExtractor()\n    .WithHeader(false)              // Specify that there's no header row\n    .WithWorksheetIndex(1)\n    .FromFile(\"employees-no-header.xlsx\")\n    .Extract\u003cPersonNoHeader\u003e();\n```\n\n### Manual Mapping\n\nFor more control over the extraction process, you can use manual mapping:\n\n```csharp\n// Extract data with manual mapping\nvar people = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream)\n    .ExtractWithManualMapping(row =\u003e new Person {\n        Name = row.Cell(1).GetString(),\n        Age = !row.Cell(2).IsEmpty() ? (int)row.Cell(2).GetDouble() : null,\n        Salary = !row.Cell(3).IsEmpty() ? (decimal)row.Cell(3).GetDouble() : null,\n        JoinDate = row.Cell(4).GetDateTime(),\n        LastActive = !row.Cell(5).IsEmpty() ? row.Cell(5).GetDateTime() : null\n    });\n```\n\n### Supported Data Types\n\n`XtractXcel` supports a wide range of data types:\n\n- Basic types: `string`, `int`, `decimal`, `double`, `DateTime`\n- Nullable variants: `int?`, `decimal?`, `DateTime?`, etc.\n- `TimeSpan` for time values\n- `Guid` for unique identifiers\n- Enums for categorized data\n\n### Selecting Specific Columns\n\nIf you only need certain columns from an Excel file:\n\n```csharp\npublic class PersonWithSpecificColumns {\n    [ExcelColumn(\"Name\")]\n    public string? NameOnly { get; set; }\n    \n    [ExcelColumn(\"Salary\")]\n    public decimal SalaryOnly { get; set; }\n}\n\nvar partialData = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromFile(\"employees.xlsx\")\n    .Extract\u003cPersonWithSpecificColumns\u003e();\n```\n\n### Handling Enums\n\nEnums are supported out of the box:\n\n```csharp\npublic enum UserStatus {\n    None,\n    Active,\n    Inactive,\n    Suspended\n}\n\npublic class PersonWithEnumStatus {\n    [ExcelColumn(\"Name\")]\n    public string? Name { get; set; }\n    \n    [ExcelColumn(\"Status\")]\n    public UserStatus Status { get; set; }\n}\n\nvar people = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromFile(\"employees.xlsx\")\n    .Extract\u003cPersonWithEnumStatus\u003e();\n```\n\n### Data Transformation During Extraction\n\nTransform data as it's being extracted:\n\n```csharp\nvar transformedData = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream)\n    .ExtractWithManualMapping(row =\u003e new Person {\n        // Convert names to uppercase\n        Name = row.Cell(1).GetString().ToUpper(),\n        // Double the age values\n        Age = !row.Cell(2).IsEmpty() ? (int)(row.Cell(2).GetDouble() * 2) : null,\n        // Halve the salary values\n        Salary = !row.Cell(3).IsEmpty() ? (decimal)(row.Cell(3).GetDouble() / 2) : null,\n        // Add a year to join dates\n        JoinDate = row.Cell(4).GetDateTime().AddYears(1),\n        // Use current date for missing activity dates\n        LastActive = !row.Cell(5).IsEmpty() ? row.Cell(5).GetDateTime() : DateTime.Now\n    });\n```\n\n### Converting to Different Target Types\n\nYou can map Excel data to any object type:\n\n```csharp\npublic class CustomPerson {\n    public string? FullName { get; set; }\n    public int YearsOld { get; set; }\n    public decimal AnnualSalary { get; set; }\n    public DateTime StartDate { get; set; }\n    public bool IsActive { get; set; }\n}\n\nvar customData = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream)\n    .ExtractWithManualMapping(row =\u003e new CustomPerson {\n        FullName = row.Cell(1).GetString(),\n        YearsOld = !row.Cell(2).IsEmpty() ? (int)row.Cell(2).GetDouble() : 0,\n        AnnualSalary = !row.Cell(3).IsEmpty() ? (decimal)row.Cell(3).GetDouble() : 0,\n        StartDate = row.Cell(4).GetDateTime(),\n        IsActive = !row.Cell(5).IsEmpty()\n    });\n```\n\n### Saving Extracted Data\n\nThe `ExcelExtractor` class now supports saving extracted data into various formats using extension methods. Here are some examples:\n\n#### Save as JSON\n\n```csharp\nusing var stream = File.OpenRead(\"employees.xlsx\");\nvar extractor = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream);\n\nstring json = extractor.Extract\u003cPerson\u003e().SaveAsJson();\nConsole.WriteLine(json);\n```\n\n#### Save as XML\n\n```csharp\nusing var stream = File.OpenRead(\"employees.xlsx\");\nvar extractor = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream);\n\nstring xml = extractor.Extract\u003cPerson\u003e().SaveAsXml();\nConsole.WriteLine(xml);\n```\n\n#### Save as XLSX\n\n```csharp\nusing var stream = File.OpenRead(\"employees.xlsx\");\nvar extractor = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream);\n\nextractor.Extract\u003cPerson\u003e().SaveAsXlsx(\"output.xlsx\");\nConsole.WriteLine(\"Data saved to output.xlsx\");\n```\n\n#### Save as XLSX Without Headers\n\nIf your data does not have headers, you can save it directly into an XLSX file without adding headers:\n\n```csharp\nusing var stream = File.OpenRead(\"employees-no-header.xlsx\");\nvar extractor = new ExcelExtractor()\n    .WithHeader(false)\n    .WithWorksheetIndex(1)\n    .FromStream(stream);\n\nvar data = extractor.Extract\u003cPersonNoHeader\u003e();\ndata.SaveAsXlsxWithoutHeader(\"output-no-header.xlsx\");\nConsole.WriteLine(\"Data saved to output-no-header.xlsx without headers\");\n```\n\n#### Save Manually Mapped Data as XLSX\n\n```csharp\nusing var stream = File.OpenRead(\"employees.xlsx\");\nvar extractor = new ExcelExtractor()\n    .WithHeader(true)\n    .WithWorksheetIndex(1)\n    .FromStream(stream);\n\nvar manuallyMappedData = extractor.ExtractWithManualMapping(row =\u003e new Person\n{\n    Name = row.Cell(1).GetString()?.ToUpper(), // Convert names to uppercase\n    Age = !row.Cell(2).IsEmpty() ? (int)(row.Cell(2).GetDouble() * 2) : null, // Double the age\n    Salary = !row.Cell(3).IsEmpty() ? (decimal)(row.Cell(3).GetDouble() / 2) : null, // Halve the salary\n    JoinDate = row.Cell(4).GetDateTime().AddYears(1), // Add a year to join dates\n    LastActive = !row.Cell(5).IsEmpty() ? row.Cell(5).GetDateTime() : DateTime.Now // Use current date for missing activity dates\n});\n\nmanuallyMappedData.SaveAsXlsx(\"manually_mapped_output.xlsx\");\nConsole.WriteLine(\"Manually mapped data saved to manually_mapped_output.xlsx\");\n```\n\n## Performance Considerations\n\nBenchmark results show that both attribute-based and manual mapping perform well, but manual mapping has a slight edge in certain cases.\n\n| Method                               | Mean       | Error     | StdDev    | Gen0      | Gen1      | Gen2      | Allocated |\n|-------------------------------------|-----------:|----------:|----------:|----------:|----------:|----------:|----------:|\n| SmallFile_AttributeMapping           |   2.685 ms | 0.0504 ms | 0.0539 ms |  148.4375 |   46.8750 |         - |   1.88 MB |\n| SmallFile_ManualMapping              |   2.600 ms | 0.0423 ms | 0.0375 ms |  148.4375 |   46.8750 |         - |   1.86 MB |\n| SmallFile_ManualMapping_NoAttributes |   2.613 ms | 0.0183 ms | 0.0171 ms |  148.4375 |   46.8750 |         - |   1.86 MB |\n| MediumFile_AttributeMapping          |  16.793 ms | 0.3305 ms | 0.4740 ms | 1000.0000 |  545.4545 |   90.9091 |  13.79 MB |\n| MediumFile_ManualMapping             |  16.172 ms | 0.3170 ms | 0.4647 ms | 1000.0000 |  454.5455 |   90.9091 |  13.66 MB |\n| LargeFile_AttributeMapping           | 174.138 ms | 3.4191 ms | 3.9374 ms | 9000.0000 | 5000.0000 | 2000.0000 | 130.90 MB |\n| LargeFile_ManualMapping              | 169.002 ms | 3.3734 ms | 5.4474 ms | 9000.0000 | 5000.0000 | 2000.0000 | 129.48 MB |\n| ManyColumns_AttributeMapping         |  27.434 ms | 0.5463 ms | 0.9711 ms | 1500.0000 |  750.0000 |  250.0000 |  18.69 MB |\n| ManyColumns_ManualMapping            |  27.409 ms | 0.5114 ms | 1.0095 ms | 1375.0000 |  625.0000 |  250.0000 |  18.65 MB |\n\n## Why Use XtractXcel?\n\nIf you're already using [ClosedXML](https://github.com/ClosedXML/ClosedXML) or similar libraries extensively, this one might not add much extra value. But if you're looking for a simple way to read an Excel file and load it into your objects without any hassle, this might worth checking out .\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjjosh102%2Fxtractxcel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjjosh102%2Fxtractxcel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjjosh102%2Fxtractxcel/lists"}