Csv.CustomFieldsGenerator 1.0.1

dotnet add package Csv.CustomFieldsGenerator --version 1.0.1
                    
NuGet\Install-Package Csv.CustomFieldsGenerator -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Csv.CustomFieldsGenerator" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Csv.CustomFieldsGenerator" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Csv.CustomFieldsGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Csv.CustomFieldsGenerator --version 1.0.1
                    
#r "nuget: Csv.CustomFieldsGenerator, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Csv.CustomFieldsGenerator@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Csv.CustomFieldsGenerator&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Csv.CustomFieldsGenerator&version=1.0.1
                    
Install as a Cake Tool

Csv generator with dynamic fields

Introduction

Project was designed for quick creation of Csv streams with records contain some dynamic fields

Nuget package

https://www.nuget.org/packages/Csv.CustomFieldsGenerator

Change log

Version Description
1.0.1 Updated TargetFramework from .CORE3.1 to .NET6
1.0.0 Initial nuget package release

How to use

  1. Create a model implements ICustomFields interface (Will require to add CustomFields to model)

    public class CountryTestCsvModel : ICustomFields
    {
        public int CountryId { get; set; }
        public string Name { get; set; }
        public string CapitalName { get; set; }
        public int Population { get; set; }
        public Dictionary<string, string> CustomFields { get; set; }
    }

  1. Instantiate model with Dynamic fields. Dynamic fields are Dictionary where key is header name and value is actual value
    var csvRows = new List<CountryTestCsvModel>
    {
        new  CountryTestCsvModel
        {
            CountryId = 1, 
            Name = "Ukraine",
            CapitalName = "Kyiv",
            Population = 41167336,
            CustomFields = new Dictionary<string, string>
            {
                { "Language: Ukrainian", "Primary" },
                { "Language: English", "Secondary" }
            }
        }
    };
  1. Create a list of dynamic fields you want to include.

    var headers = new List<string>
    {
        "Language: Ukrainian", "Language: English"
    };

Important: Only headers specified in this and met to keys from previous step will be generated

  1. Instantiate helper and call one of the csv generation methods

    var csvGenerator = new CsvStreamGenerator();
    var stream = csvGenerator.GenerateCsvStream(headers, csvRows, false);

  1. Next you can save your stream to some csv file. As an example, here is how to save it locally
    var filePath = Directory.GetCurrentDirectory();
    const string fileName = "Test.csv";
    SaveStreamAsFile(filePath, stream, fileName);

    private static void SaveStreamAsFile(string filePath, Stream inputStream, string fileName)
    {
        DirectoryInfo info = new DirectoryInfo(filePath);
        if (!info.Exists)
        {
            info.Create();
        }
        string path = Path.Combine(filePath, fileName);
        using (FileStream outputFileStream = new FileStream(path, FileMode.Create))
        {
            inputStream.CopyTo(outputFileStream);
        }
    }

  1. See results

| CountryId | Name    | CapitalName | Population | Language: Ukrainian | Language: English |
| 1	        | Ukraine |	Kyiv        | 41167336   | Primary             | Secondary         |


Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.0.1 6,461 3/11/2023
1.0.0 478 8/28/2022
0.3.4 472 8/23/2022
0.3.3 461 8/22/2022
0.3.2 472 8/22/2022
0.3.1 463 8/22/2022
0.3.0 450 8/22/2022
0.2.0 481 8/21/2022
0.1.0 484 8/21/2022

Adding package icon.