MicroSolution.ExcelData 1.2.5.2

dotnet add package MicroSolution.ExcelData --version 1.2.5.2                
NuGet\Install-Package MicroSolution.ExcelData -Version 1.2.5.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MicroSolution.ExcelData" Version="1.2.5.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MicroSolution.ExcelData --version 1.2.5.2                
#r "nuget: MicroSolution.ExcelData, 1.2.5.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MicroSolution.ExcelData as a Cake Addin
#addin nuget:?package=MicroSolution.ExcelData&version=1.2.5.2

// Install MicroSolution.ExcelData as a Cake Tool
#tool nuget:?package=MicroSolution.ExcelData&version=1.2.5.2                

MS.ExcelData

A library for converting data in Excel tables (ListObjects) into structured class objects, as well as for convenient extraction and recording of information in a table

How to use

Let's say you have an Excel table (ListObject) that contains several columns:

  • Id
  • Name of ...
  • Actual date
  • Value

And a class definition that looks like this.

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
    public double Value { get; set; }
}

To describe the class, we need to add attributes

using MS.ExcelData.Attributes;

[TableName("Your ListObject table name")]
public class Foo
{
    [Name("Id"), IsIndex]
    public int Id { get; set; }
    
    [Name("Name of ...")]
    public string Name { get; set; }
    
    [Name("Actual date")]
    public DateTime Date { get; set; }
    
    [Name("Value")]
    public double Value { get; set; }
}

Instead of a column name, we can use its ordinal number

    [Index(2)]
    public string Name { get; set; }
    
    [Index(3)]
    public DateTime Date { get; set; }

    ...

If the column is read-only, this can be specified using attribute IsReadOnly:

    [Index(2), IsreadOnly]
    public string Name { get; set; }

Data context

Create a class to work with excel spreadsheets, like this

using MS.ExcelData;
using Excel = Microsoft.Office.Interop.Excel;

class DataContext
{
	public IBaseRepository<Model> FooTable { get; set; }
	private Excel.Workbook Workbook { get; set; }
	
    public DataContext(Excel.Workbook workbook)
	{
		Workbook = workbook;
		FooTable = new BaseRepository<Foo>(new ExcelTable<Foo>(Workbook));
	}
}

Use the data context

You can add data to table


Foo foo = new Foo() 
{
	Name = "Name 1",
	Value = 123,
	Date = DateTime.Today
};

DataContext.FooTable.Save(foo);

If the FooTable has an index field and the table has an entry foo, then that entry will be edited

You can find and delete records like this


Foo foo = DataContext.FooTable.GetById(1);
DataContext.FooTable.Delete(foo);

You can get all the records


IEnumerable<Foo> fs = DataContext.FooTable.GetAll();

Product Compatible and additional computed target framework versions.
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.5.2 471 3/31/2022