Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Magicodes.IE Library Adds Word, PDF, and HTML Export Support

Tech May 16 2

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

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.