Skip to main content

Overview

Description

Surya.Ab.Export is a library designed to empower developers with seamless data export capabilities from C# objects. Whether you need to export data to Excel, CSV, Text or Pdf formats, this library provides a reliable and efficient solution by simplifying the export process and offering a range of customization options.

Installation

dotnet add package Surya.Ab.Export --version 6.2.61

Getting Started

public class Program
{
public static void Main()
{
IServiceCollection services = new ServiceCollection();
services.AddAbExport();
var serviceProvider = services.BuildServiceProvider();
var exportManager = serviceProvider.GetRequiredService<IExporter>();

// Get data
var data = GetData();
// Get metadata - it's optional
var metadata = GetMetadata();

/* ------------ Configure ExportOption ----------------
* NOTE : All properties of ExportOption are optional.
* One can pass an empty instance of Options.
*
* EXP: var option = SingleDataSetOptions();
*/
var exportOption = new SingleDataSetOptions() { Metadata = metadata, };

// set Output format - Excel/Pdf/Csv/Tsv
var exportType = ExportType.Excel;

/* Call Export method by passing ExportType, Data and ExportOption */
Stream stream = exportManager.Export(exportType, data, exportOption);

// Path where file to be saved
string filePath = @"..\Accounts.xlsx";

// Save stream as file
using (FileStream outputFileStream = new(filePath, FileMode.Create))
{
stream.CopyTo(outputFileStream);
}
}

// SAMPLE DATA MODEL
internal class Account
{
public string? Name { get; set; }
public double AccountBalance { get; set; }
}

// Get SAMPLE DATA MODEL Instances
internal static List<Account> GetData()
{
List<Account> data =
new()
{
new Account { Name = "Deposit", AccountBalance = 1540.4574761 },
new Account { Name = "Current", AccountBalance = 10012 },
new Account { Name = "Borrowing", AccountBalance = 100 },
new Account { Name = "Other", AccountBalance = 100 },
};
return data;
}

// Get Metadata list
internal static List<PropertyMetadata> GetMetadata()
{
List<PropertyMetadata> metadata =
new()
{
new PropertyMetadata()
{
Code = "Name",
DisplayName = "Name",
DisplayOrder = 1,
DataType = DataType.String,
DisplayType = DisplayType.XXXL
},
new PropertyMetadata()
{
Code = "AccountBalance",
DisplayName = "Balance",
DisplayOrder = 2,
DataType = DataType.Number,
DisplayType = DisplayType.Amount
},
};
return metadata;
}
}

Edit on GitHub