Magicodes.IE Library Adds Word, PDF, and HTML Export Support
Overview
Magicodes.IE is a .NET library for importing and exporting data using Data Transfer Object (DTO) models. It supports Excel, Word, PDF, and HTML formats.
Key Features
- Import and export operations are controlled via DTO classes and attributes
- Custom column heeaders for multilingual support
- Text filtering and transformation during export
- Automatic skipping of empty rows during import
- Automatic template generation with required field indicators
- Dropdown selection support for enum types
- Whitespace trimming with per-column configuration
- Template validation, data verification, and unified error handling
- Support for dynamic column exports using DataTable
- Custom export templates for HTML, Word, and PDF
Code Examples
Basic Export
public class ItemData
{
public string Title { get; set; }
public string Description { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
var items = new List<ItemData>
{
new ItemData { Title = "Product A", Description = "Sample item", Category = "Electronics", Price = 99.99m },
new ItemData { Title = "Product B", Description = "Another item", Category = "Books", Price = 24.50m }
};
await Exporter.Export("output.xlsx", items);
Export with Attributes
[ExcelExporter(Name = "Inventory", TableStyle = "Light8")]
public class InventoryItem
{
[ExporterHeader(DisplayName = "Item Name", IsBold = true)]
public string ProductName { get; set; }
[ExporterHeader(DisplayName = "Stock Count", Format = "#,##0")]
public int Quantity { get; set; }
[ExporterHeader(DisplayName = "Status", IsAutoFit = true)]
public string Availability { get; set; }
}
var inventory = new List<InventoryItem>
{
new InventoryItem { ProductName = "Laptop", Quantity = 15, Availability = "In Stock" },
new InventoryItem { ProductName = "Monitor", Quantity = 8, Availability = "Low Stock" }
};
await Exporter.Export("inventory_report.xlsx", inventory);
Import with Validation
public class ProductImport
{
[ImporterHeader(Name = "Product Name")]
[Required(ErrorMessage = "Product name is required")]
public string Name { get; set; }
[ImporterHeader(Name = "Product Code")]
[MaxLength(10, ErrorMessage = "Code cannot exceed 10 characters")]
public string Code { get; set; }
[ImporterHeader(Name = "Weight (KG)")]
public double Weight { get; set; }
}
var importResult = await Importer.Import<ProductImport>("products.xlsx");
if (importResult.HasError)
{
foreach (var error in importResult.RowErrors)
{
Console.WriteLine($"Row {error.RowIndex}: {error.ErrorMessage}");
}
}
Docker Configuration for Export
# Install required libraries for Excel and PDF export
RUN apt-get update && apt-get install -y libgdiplus libc6-dev fontconfig
RUN ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
COPY /simsun.ttc /usr/share/fonts/simsun.ttc