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
<PackageReference Include="MicroSolution.ExcelData" Version="1.2.5.2" />
paket add MicroSolution.ExcelData --version 1.2.5.2
#r "nuget: MicroSolution.ExcelData, 1.2.5.2"
// 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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net48 is compatible. net481 was computed. |
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 |