A .NET library for reading, parsing, and validating Excel files with type-safe operations and comprehensive error handling.
- Excel File Reading: Read Excel files (.xlsx, .xls) using ExcelDataReader
- Type-Safe Parsing: Parse Excel data into strongly-typed C# objects
- Data Validation: Built-in validation using data annotations
- Batch Processing: Support for batch imports with error tracking
- Sheet Mapping: Map Excel sheets to different types with flexible column mapping
- Query Interface: Query imported Excel data with filtering and pagination
- Entity Framework Integration: Store and query Excel data using Entity Framework
This library consists of two NuGet packages:
- SimplyWorks.ExcelImport: Core library with Excel reading and parsing functionality
- SimplyWorks.ExcelImport.Extensions: ASP.NET Core dependency injection extensions
Install the core package:
dotnet add package SimplyWorks.ExcelImportFor ASP.NET Core applications, also install the extensions package:
dotnet add package SimplyWorks.ExcelImport.Extensionspublic class Order
{
[Required]
public string OrderId { get; set; }
[Required]
public string CustomerName { get; set; }
[Range(0, double.MaxValue)]
public decimal Amount { get; set; }
public DateTime OrderDate { get; set; }
}public void ConfigureServices(IServiceCollection services)
{
services.AddExcelImport();
// Configure your DbContext
services.AddDbContext<YourDbContext>(options =>
options.UseSqlServer(connectionString));
}public class OrderImportService
{
private readonly ExcelService _excelService;
public OrderImportService(ExcelService excelService)
{
_excelService = excelService;
}
public async Task ImportOrders(string excelFileUrl)
{
var options = new TypedParseToJsonOptions
{
TypeAssemblyQualifiedName = typeof(Order).AssemblyQualifiedName,
NamingStrategy = JsonNamingStrategy.SnakeCase
};
// Load and validate Excel file
var container = await _excelService.LoadExcelFileInfo(excelFileUrl, options);
// Check for validation errors
if (container.Sheets.Any(sheet => sheet.HasErrors()))
{
// Handle validation errors
return;
}
// Import valid data
await _excelService.Import(excelFileUrl, options);
}
}public class OrderQueryService
{
private readonly IExcelQueryable _excelQueryable;
public OrderQueryService(IExcelQueryable excelQueryable)
{
_excelQueryable = excelQueryable;
}
public async Task<IEnumerable<Order>> GetValidOrders(string reference, int pageIndex = 0, int pageSize = 100)
{
var options = new ExcelQueryValidatedOptions
{
Reference = reference,
PageIndex = pageIndex,
PageSize = pageSize,
RowStatus = QueryRowStatus.Valid
};
return await _excelQueryable.Get<Order>(options);
}
}Main service for importing Excel files with validation and error handling.
Interface for reading Excel files and sheets with support for:
- Multiple sheet processing
- Column mapping
- Row-by-row reading
Repository pattern implementation for storing Excel import metadata and results.
Generic sheet reader for type-safe Excel data parsing.
Built-in support for:
- Data annotation validation
- Type conversion validation
- Custom validation rules
TypeAssemblyQualifiedName: Target type for parsingNamingStrategy: JSON naming strategy (SnakeCase, CamelCase, etc.)SheetsOptions: Configuration for multiple sheets
- Column mapping configuration
- Sheet selection options
- Validation rules
The library includes Entity Framework entities for tracking:
ExcelFileRecord: Excel file metadataSheetRecord: Individual sheet informationRowRecord: Row-level data and validation resultsCellRecord: Cell-level dataBatchandBatchItem: Batch processing tracking
- ExcelDataReader: For reading Excel files
- Newtonsoft.Json: JSON serialization
- Entity Framework Core: Data persistence
- SimplyWorks Libraries: Additional utilities and extensions
- .NET Standard 2.1 (Core library)
- .NET Core 3.1 (Extensions package)
This project is licensed under the MIT License - see the LICENSE file for details.
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
For issues and questions:
- Create an issue on GitHub
- Check existing documentation
This library is part of the SimplyWorks ecosystem: