All Projects β†’ ipvalverde β†’ EPPlus.DataExtractor

ipvalverde / EPPlus.DataExtractor

Licence: MIT license
EPPlus extension that make easier to extract POCO from excel tables

Programming Languages

C#
18002 projects
powershell
5483 projects

Projects that are alternatives of or similar to EPPlus.DataExtractor

EPPlus.Core.Extensions
An extensions library for EPPlus package to generate and manipulate Excel files easily.
Stars: ✭ 66 (+1.54%)
Mutual labels:  excel, epplus
Php Ext Xlswriter
πŸš€ PHP Extension for creating and reader XLSX files.
Stars: ✭ 1,734 (+2567.69%)
Mutual labels:  extension, excel
Dexiom.EPPlusExporter
A very simple, yet incredibly powerfull library to generate Excel documents out of objects, arrays, lists, collections, etc.
Stars: ✭ 19 (-70.77%)
Mutual labels:  excel, epplus
Vscode Data Preview
Data Preview 🈸 extension for importing πŸ“€ viewing πŸ”Ž slicing πŸ”ͺ dicing 🎲 charting πŸ“Š & exporting πŸ“₯ large JSON array/config, YAML, Apache Arrow, Avro, Parquet & Excel data files
Stars: ✭ 245 (+276.92%)
Mutual labels:  extension, excel
spreadsheet
Yii2 extension for export to Excel
Stars: ✭ 79 (+21.54%)
Mutual labels:  extension, excel
VBA-Import-Export
Export & Import VBA code for use with Git (or any VCS)
Stars: ✭ 14 (-78.46%)
Mutual labels:  excel
siddhi-io-kafka
Extension that can be used to receive events from a Kafka cluster and to publish events to a Kafka cluster
Stars: ✭ 16 (-75.38%)
Mutual labels:  extension
sqlops-widgets
SQL Operations Studio Dashboard Widgets - including Always ON
Stars: ✭ 22 (-66.15%)
Mutual labels:  extension
Samples-ASP.NET-MVC-CSharp
ASP.NET MVC C# samples for Stimulsoft Reports.Web reporting tool.
Stars: ✭ 31 (-52.31%)
Mutual labels:  excel
Power-Refresh-VBScript
Visual Basic Script Refresher for Excel files with Power Query and PowerPivot model
Stars: ✭ 18 (-72.31%)
Mutual labels:  excel
pharext
Distribute your PHP extension as self-installing phar executable
Stars: ✭ 57 (-12.31%)
Mutual labels:  extension
9anime-Companion
πŸš€ A simple companion extension for 9anime
Stars: ✭ 83 (+27.69%)
Mutual labels:  extension
robotframework-excel
Robot-framework auto-test excel
Stars: ✭ 15 (-76.92%)
Mutual labels:  excel
prependers
Easily and cleanly extend third-party code.
Stars: ✭ 24 (-63.08%)
Mutual labels:  extension
vue-willtable
An editable table component for Vue.js 2.0
Stars: ✭ 119 (+83.08%)
Mutual labels:  excel
github-wc-polyfill
Ensure that all GitHub and GitLab scripts required for UXP and SeaMonkey are loaded correctly
Stars: ✭ 87 (+33.85%)
Mutual labels:  extension
solidus static content
πŸ“„ Content management for your Solidus store.
Stars: ✭ 18 (-72.31%)
Mutual labels:  extension
1click-webpage-screenshot
Entire page Screenshot extension for Google Chrome. I'm developing open source extension for Google Chrome. All extension are free for use. Let's make Chrome great again!
Stars: ✭ 432 (+564.62%)
Mutual labels:  extension
EPPlus.Sample.NetCore
EPPlus samples for .NET Core. More details in Readme.md.
Stars: ✭ 68 (+4.62%)
Mutual labels:  epplus
npoi
a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Stars: ✭ 4,493 (+6812.31%)
Mutual labels:  excel

Build status

Summary

This is a simple EPPlus extension that make easier to extract POCO from spreadsheets.

Installation

You can download the library from nuget with the following command:

Install-Package EPPlus.DataExtractor

If you donΒ΄t have the EPPlus as a dependency, nuget will install it for you.

Usage

Using the package is really simple. Let's work with a simple example first.

Consider you want to extract the following data from a spreadsheet:

First table

Just a define a class to hold the data.

public class SimpleRowData
{
    public string CarName { get; set; }

    public double Value { get; set; }

    public DateTime CreationDate { get; set; }
}

Considering that the data is on a worksheet named worksheet1, this is the code that can be used to retrieve an IEnumerable of SimpleRowData:

using (var package = new ExcelPackage("spreadsheet/file/location/file.xlsx"))
{
    var cars = package.Workbook.Worksheets["worksheet1"]
        .Extract<SimpleRowData>()
        // Here we can chain multiple definition for the columns
        .WithProperty(p => p.CarName, "B")
        .WithProperty(p => p.Value, "C")
        .WithProperty(p => p.CreationDate, "D")
        .GetData(4, 6) // To obtain the data we indicate the start and end of the rows.
        // In this case, the rows 4, 5 and 6 will be extracted.
        .ToList();
}

Yeah, it is that simple!

Callbacks

You can also use callbacks for filling properties values, where you can put custom code, validations and even abort the rest of the execution. You can specify a callback that is executed over the object ( setPropertyValueCallback ) value or over the casted TValue ( setPropertyCastedValueCallback ) type.

The first parameter is of type PropertyExtractionContext, that contains data about the cell address used to populate the property ( PropertyExtractionContext.CellAddress ) and a Abort() method that can be used to cancel the rest of the processing for the entire extraction. The rows extracted before the execution of the Abort will be returned by the GetData method, and an entity for the current row will also be returned with all the previous properties populated.

The following code is based on the previous example and uses the setPropertyCastedValueCallback to print a message based on the value of the cell:

using (var package = new ExcelPackage("spreadsheet/file/location/file.xlsx"))
{
    var cars = package.Workbook.Worksheets["worksheet1"]
        .Extract<SimpleRowData>()
        .WithProperty(p => p.CarName, "B")
        .WithProperty(p => p.Value, "C")
        .WithProperty(p => p.CreationDate, "D",
            setPropertyCastedValueCallback: (propContext, creationDate) =>
            {
                if(creationDate < new DateTime(2000, 1, 1)) {
                    Console.WriteLine("The car in row {0} is here for too long, no one will buy it", propContext.CellAddress.Row);
                }

                // We could also abort if the date time value is not set (i.e. is the default one):
                /*
                if(creationDate == default(DateTime))
                {
                    Console.WriteLine("Invalid value in cell {0}!", propContext.CellAddress.Address);
                    propContext.Abort();
                }
                */
            })
        .GetData(4, 6)
        .ToList();
}

Columns that should be rows

Sometimes the tables defined in spreadsheets does not have a friendly structure for a developer. Instead of creating multiple tables and foreign key relationships in excel it is simpler to put data that should go into different tables as columns in the existing table. It'll be clear with the following example:

Table 2

Imagine that you want to extract this data into a class structure where the columns that indicates months/year should be a collection inside the entity that will keep the row data. So we can have two classes defined like that:

public class RowDataWithColumnBeingRow
{
    public string Name { get; set; }

    public int Age { get; set; }

    public List<ColumnData> MoneyData { get; set; }
}

public class ColumnData
{
    public double ReceivedMoney { get; set; }

    public DateTime Date { get; set; }
}

You can use the following code to extract the spreadsheet data to these classes:

using (var package = new ExcelPackage("spreadsheet/file/location/file.xlsx"))
{
    var data = package.Workbook.Worksheets["worksheet1"]
        .Extract<RowDataWithColumnBeingRow>()
        .WithProperty(p => p.Name, "F")
        .WithProperty(p => p.Age, "G")

        // Here, the collection property is defined using the "WithCollectionProperty" method.
        // The following parameter is the expression indicating the property of "ColumnData"
        // that will be used to receive the header data followed by an integer indicating the row
        // that contains the header.
        // The last expression indicates the other "ColumnData" property, this one will receive
        // the row data. The two last strings are the start and end column from where
        // this data will be extracted.
        .WithCollectionProperty(p => p.MoneyData,
            item => item.Date, 1,
            item => item.ReceivedMoney, "H", "S")
        .GetData(2, 4)
        .ToList();
}

In this previous example the objective was to extract columns and their headers into a collection of custom types inside property inside the main row class type. If you have various columns and the just want the value of them (i.e. the header is meaningless) there is an easier way of doing it as you can see in the next example

Table 2

Consider the model for the rows as:

public class MultiLingualUserData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<string> LanguagesSpoken { get; set; }
}

You could extract the columns of the language spoken to the property LanguagesSpoken without hassle with the following code:

using (var package = new ExcelPackage("spreadsheet/file/location/file.xlsx"))
{
    var data = package.Workbook.Worksheets["worksheet3"]
        .Extract<MultiLingualUserData>()
        .WithProperty(p => p.FirstName, "A")
        .WithProperty(p => p.LastName, "B")

        // Here, the collection property is defined an overload of the "WithCollectionProperty" method.
        // This overload simply expects a property of List, Collection or HashSet followed by
        // the letters that indicate the interval of the columns from where the data will be
        // extracted
        .WithCollectionProperty(p => p.LanguagesSpoken, "C", "E")
        .GetData(2, 4)
        .ToList();
}

The GetData method returns an IEnumerable, and this IEnumerable is not evaluated until you interate through it or execute something like ToList over it. So make sure you'll do one of those things before disposing the EPPlus ExcelPackage.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].