Giter Site home page Giter Site logo

damienbod / aspnetcorecsvimportexport Goto Github PK

View Code? Open in Web Editor NEW
79.0 11.0 43.0 68 KB

ASP.NET Core CSV import export custom formatters

Home Page: https://damienbod.com/2016/06/17/import-export-csv-in-asp-net-core/

C# 94.62% HTML 5.38%
csv formatter aspnet-core

aspnetcorecsvimportexport's Introduction

Import and Export CSV in ASP.NET Core

Where to get it

You can download this package on NuGet.

About

This article shows how to import and export csv data in an ASP.NET Core application. The InputFormatter and the OutputFormatter classes are used to convert the csv data to the C# model classes.

2020-01-10: Updated to .NET Core 3.1

2019-09-14: Updated to .NET Core 3.0

2018-11-23: Encoding bug fix

2018-10-24: Updated to .NET Core 2.2, sync with Web API Contrib Core

2018-10-08: Updated to .NET Core 2.1.5, adding compression

2018-09-14: Updated to .NET Core 2.1.4

2018-07-09: Updated to .NET Core 2.1.1

2018-05-31: Support for ignore, Updated to .NET Core 2.1

2018-05-13: Updated to .NET Core 2.1 rc

2017-10-29: Support for CSV encoding

2017-08-23: Updated to ASP.NET Core 2.0

2017-02-12: Updated to VS2017 msbuild

2016-11-25: Updated ASP.NET Core 1.1

2016-06-29: Updated to ASP.NET Core RTM

The LocalizationRecord class is used as the model class to import and export to and from csv data.

using System;

namespace AspNetCoreCsvImportExport.Model
{
    public class LocalizationRecord
    {
        public long Id { get; set; }
        public string Key { get; set; }
        public string Text { get; set; }
        public string LocalizationCulture { get; set; }
        public string ResourceKey { get; set; }
    }
}

The MVC Controller CsvTestController makes it possible to import and export the data. The Get method exports the data using the Accept header in the HTTP Request. Per default, Json will be returned. If the Accept Header is set to 'text/csv', the data will be returned as csv. The GetDataAsCsv method always returns csv data because the Produces attribute is used to force this. This makes it easy to download the csv data in a browser.

The Import method uses the Content-Type HTTP Request header to decide how to handle the request body. If the 'text/csv' is defined, the custom csv input formatter will be used.

using System.Collections.Generic;
using AspNetCoreCsvImportExport.Model;
using Microsoft.AspNetCore.Mvc;

namespace AspNetCoreCsvImportExport.Controllers
{
    [Route("api/[controller]")]
    public class CsvTestController : Controller
    {
        // GET api/csvtest
        [HttpGet]
        public IActionResult Get()
        {
            return Ok(DummyData());
        }

        [HttpGet]
        [Route("data.csv")]
        [Produces("text/csv")]
        public IActionResult GetDataAsCsv()
        {
            return Ok( DummyData());
        }

        private static IEnumerable<LocalizationRecord> DummyData()
        {
            var model = new List<LocalizationRecord>
            {
                new LocalizationRecord
                {
                    Id = 1,
                    Key = "test",
                    Text = "test text",
                    LocalizationCulture = "en-US",
                    ResourceKey = "test"

                },
                new LocalizationRecord
                {
                    Id = 2,
                    Key = "test",
                    Text = "test2 text de-CH",
                    LocalizationCulture = "de-CH",
                    ResourceKey = "test"

                }
            };

            return model;
        }

        // POST api/csvtest/import
        [HttpPost]
        [Route("import")]
        public IActionResult Import([FromBody]List<LocalizationRecord> value)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            else
            {
                List<LocalizationRecord> data = value;
                return Ok();
            }
        }

    }
}

The csv input formatter implements the InputFormatter class. This checks if the context ModelType property is a type of IList and if so, converts the csv data to a List of Objects of type T using reflection. This is implemented in the read stream method. The implementation is very basic and will not work if you have more complex structures in your model class.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;
using System.Text;

namespace AspNetCoreCsvImportExport.Formatters
{
    /// <summary>
    /// ContentType: text/csv
    /// </summary>
    public class CsvInputFormatter : InputFormatter
    {
        private readonly CsvFormatterOptions _options;

        public CsvInputFormatter(CsvFormatterOptions csvFormatterOptions)
        {
            SupportedMediaTypes.Add(Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/csv"));

            if (csvFormatterOptions == null)
            {
                throw new ArgumentNullException(nameof(csvFormatterOptions));
            }

            _options = csvFormatterOptions;
        }

        public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
        {
            var type = context.ModelType;
            var request = context.HttpContext.Request;
            MediaTypeHeaderValue requestContentType = null;
            MediaTypeHeaderValue.TryParse(request.ContentType, out requestContentType);


            var result = ReadStream(type, request.Body);
            return InputFormatterResult.SuccessAsync(result);
        }

        public override bool CanRead(InputFormatterContext context)
        {
            var type = context.ModelType;
            if (type == null)
                throw new ArgumentNullException("type");

            return IsTypeOfIEnumerable(type);
        }

        private bool IsTypeOfIEnumerable(Type type)
        {

            foreach (Type interfaceType in type.GetInterfaces())
            {

                if (interfaceType == typeof(IList))
                    return true;
            }

            return false;
        }

        private object ReadStream(Type type, Stream stream)
        {
            Type itemType;
            var typeIsArray = false;
            IList list;
            if (type.GetGenericArguments().Length > 0)
            {
                itemType = type.GetGenericArguments()[0];
                list = (IList)Activator.CreateInstance(type);
            }
            else
            {
                typeIsArray = true;
                itemType = type.GetElementType();

                var listType = typeof(List<>);
                var constructedListType = listType.MakeGenericType(itemType);

                list = (IList)Activator.CreateInstance(constructedListType);
            }

            var reader = new StreamReader(stream, Encoding.GetEncoding(_options.Encoding));

            bool skipFirstLine = _options.UseSingleLineHeaderInCsv;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(_options.CsvDelimiter.ToCharArray());
                if(skipFirstLine)
                {
                    skipFirstLine = false;
                }
                else
                {
                    var itemTypeInGeneric = list.GetType().GetTypeInfo().GenericTypeArguments[0];
                    var item = Activator.CreateInstance(itemTypeInGeneric);
                    var properties = item.GetType().GetProperties();
                    for (int i = 0;i<values.Length; i++)
                    {
                        properties[i].SetValue(item, Convert.ChangeType(values[i], properties[i].PropertyType), null);
                    }

                    list.Add(item);
                }

            }

            if(typeIsArray)
            {
                Array array = Array.CreateInstance(itemType, list.Count);

                for(int t = 0; t < list.Count; t++)
                {
                    array.SetValue(list[t], t);
                }
                return array;
            }
            
            return list;
        }
    }
}

The csv output formatter is implemented using the code from Tugberk Ugurlu's blog with some small changes. Thanks for this. This formatter uses ';' to separate the properties and a new line for each object. The headers are added tot he first line.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.Formatters;
using System.ComponentModel.DataAnnotations;

namespace AspNetCoreCsvImportExport.Formatters
{
    /// <summary>
    /// Original code taken from
    /// http://www.tugberkugurlu.com/archive/creating-custom-csvmediatypeformatter-in-asp-net-web-api-for-comma-separated-values-csv-format
    /// Adapted for ASP.NET Core and uses ; instead of , for delimiters
    /// </summary>
    public class CsvOutputFormatter : OutputFormatter
    {
        private readonly CsvFormatterOptions _options;

        public string ContentType { get; private set; }

        public CsvOutputFormatter(CsvFormatterOptions csvFormatterOptions)
        {
            ContentType = "text/csv";
            SupportedMediaTypes.Add(Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/csv"));
            _options = csvFormatterOptions ?? throw new ArgumentNullException(nameof(csvFormatterOptions));
        }

        protected override bool CanWriteType(Type type)
        {

            if (type == null)
                throw new ArgumentNullException("type");

            return IsTypeOfIEnumerable(type);
        }

        private bool IsTypeOfIEnumerable(Type type)
        {

            foreach (Type interfaceType in type.GetInterfaces())
            {

                if (interfaceType == typeof(IList))
                    return true;
            }

            return false;
        }

        public async override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
        {
            var response = context.HttpContext.Response;

            Type type = context.Object.GetType();
            Type itemType;

            if (type.GetGenericArguments().Length > 0)
            {
                itemType = type.GetGenericArguments()[0];
            }
            else
            {
                itemType = type.GetElementType();
            }

            var streamWriter = new StreamWriter(response.Body, Encoding.GetEncoding(_options.Encoding));

            if (_options.UseSingleLineHeaderInCsv)
            {
                await streamWriter.WriteLineAsync(
                    string.Join(
                        _options.CsvDelimiter, itemType.GetProperties().Select(x => x.GetCustomAttribute<DisplayAttribute>(false)?.Name ?? x.Name)
                    )
                );
            }

            foreach (var obj in (IEnumerable<object>)context.Object)
            {

                var vals = obj.GetType().GetProperties().Select(
                    pi => new
                    {
                        Value = pi.GetValue(obj, null)
                    }
                );

                string valueLine = string.Empty;

                foreach (var val in vals)
                {
                    if (val.Value != null)
                    {

                        var _val = val.Value.ToString();

                        //Check if the value contans a comma and place it in quotes if so
                        if (_val.Contains(","))
                            _val = string.Concat("\"", _val, "\"");

                        //Replace any \r or \n special characters from a new line with a space
                        if (_val.Contains("\r"))
                            _val = _val.Replace("\r", " ");
                        if (_val.Contains("\n"))
                            _val = _val.Replace("\n", " ");

                        valueLine = string.Concat(valueLine, _val, _options.CsvDelimiter);

                    }
                    else
                    {
                        valueLine = string.Concat(valueLine, string.Empty, _options.CsvDelimiter);
                    }
                }

                await streamWriter.WriteLineAsync(valueLine.TrimEnd(_options.CsvDelimiter.ToCharArray()));
            }

            await streamWriter.FlushAsync();
        }
    }
}

The custom formatters need to be added to the MVC middleware, so that it knows how to handle media types 'text/csv'.

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options =>
    {
        options.InputFormatters.Add(new CsvInputFormatter());
        options.OutputFormatters.Add(new CsvOutputFormatter());
        options.FormatterMappings.SetMediaTypeMappingForFormat("csv", MediaTypeHeaderValue.Parse("text/csv"));
    });
}

When the data.csv link is requested, a csv type response is returned to the client, which can be saved. This data contains the header texts and the value of each property in each object. This can then be opened in excel.

http://localhost:10336/api/csvtest/data.csv

Id;Key;Text;LocalizationCulture;ResourceKey
1;test;test text;en-US;test
2;test;test2 text de-CH;de-CH;test

This data can then be used to upload the csv data to the server which is then converted back to a C# object. I use fiddler, postman or curl can also be used, or any HTTP Client where you can set the header Content-Type.

 http://localhost:10336/api/csvtest/import 

 User-Agent: Fiddler 
 Content-Type: text/csv 
 Host: localhost:10336 
 Content-Length: 110 


 Id;Key;Text;LocalizationCulture;ResourceKey 
 1;test;test text;en-US;test 
 2;test;test2 text de-CH;de-CH;test 

The following image shows that the data is imported correctly.

importExportCsv

Notes

The implementation of the InputFormatter and the OutputFormatter classes are specific for a list of simple classes with only properties. If you require or use more complex classes, these implementations need to be changed.

Links

http://www.tugberkugurlu.com/archive/creating-custom-csvmediatypeformatter-in-asp-net-web-api-for-comma-separated-values-csv-format

https://damienbod.com/2015/06/03/asp-net-5-mvc-6-custom-protobuf-formatters/

http://www.strathweb.com/2014/11/formatters-asp-net-mvc-6/

https://wildermuth.com/2016/03/16/Content_Negotiation_in_ASP_NET_Core

aspnetcorecsvimportexport's People

Contributors

damienbod avatar fastboxster avatar lvmajor avatar mattfrear avatar pogione avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aspnetcorecsvimportexport's Issues

How to Upload File

Hi Damien,

I tried your code but struggle with getting the upload done from the User Interface. I tried with Kendo UI to Upload to the Import URL but as List<LocalizationRecord> value I get count == 0.

Could you help me ?

Html Code snippet:

<div id="example">
    <div class="box">
        <h4>Information</h4>
        <p>
            The Upload can be used as a drop-in replacement
            for file input elements. This "synchronous" mode does not require
            special handling on the server.
        </p>
    </div>
    <form method="post" action="/api/csvtest/import">
        <div class="demo-section k-content">
            <input name="files" id="files" type="file" aria-label="files" />
            <p style="padding-top: 1em; text-align: right">
                <input type="submit" value="Submit" class="k-button k-primary" />
            </p>
        </div>
    </form>
    <script>
        $(document).ready(function() {
            $("#files").kendoUpload();
        });
    </script>
</div>

Thanks,

Erich

System.InvalidCastException: Unable to cast object of type 'report_engine.Models.CustomerPlanModel' to type 'System.Collections.IList'

System.InvalidCastException: Unable to cast object of type 'report_engine.Models.CustomerPlanModel' to type 'System.Collections.IList'

On line 68 of CsvInputFormatter.cs

list = (IList)Activator.CreateInstance(itemType);

Not sure why, my class is simple it's just two string properties...

public class CustomerPlanModel
    {
        public string CustomerNumber { get; set; }
        public string PlanId { get; set; }
    }

Note I did change the CsvDelimeter to "," instead of ";" and I am POSTing over:

CustomerNumber,PlanId
CUS-091090129301823,1420fosj
CUS-019209123,fdsojwi3
CUS-91302920139,dsds908

Edit: tried using ";" as the original code does and it still gave me the same error...

DateTime

Is there any, easy, way to serve the CSV with a custom datetime formatting?

Ignore Properties in CSV

Hi damienbod,

Thanks for a great library. I have a question:
I have a class with 10 properties. I would like to export to CSV only 5 properties and ignore 5. How could we achieve that?

Something like an attribute on the top of property such as
[CsvIgnore]
public bool NewProduct {get; set;}

Thanks

Demo code threw this exception

System.InvalidCastException was unhandled by user code
HResult=-2147467262
Message=Unable to cast object of type 'AspNetCoreCsvImportExport.Model.LocalizationRecord' to type 'System.Collections.IList'.
Source=AspNetCoreCsvImportExport
StackTrace:
at AspNetCoreCsvImportExport.Formatters.CsvInputFormatter.readStream(Type type, Stream stream) in C:\projects\AspNetCoreCsvImportExport\AspNetCoreCsvImportExport-master\src\AspNetCoreCsvImportExport\Formatters\CsvInputFormatter.cs:line 71
at AspNetCoreCsvImportExport.Formatters.CsvInputFormatter.ReadRequestBodyAsync(InputFormatterContext context) in C:\projects\AspNetCoreCsvImportExport\AspNetCoreCsvImportExport-master\src\AspNetCoreCsvImportExport\Formatters\CsvInputFormatter.cs:line 37
at Microsoft.AspNetCore.Mvc.Formatters.InputFormatter.ReadAsync(InputFormatterContext context)
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.d__3.MoveNext()
InnerException:

Generric import & export - for runtime file import

Hi I am importing files from an external FTP site, often the file headers are not known ahead of time. And, I need to save into a temp db stable in sql.

Here i ma using your project to help me. But I am running into an issue where the file headers are not know ahead of time, can we emit "genric" models.

I can adding my code here to build a generic db sql table to help others as well. I just need help on the import side

thanks

Bug, improvement

Line 123 of the CsvOutputFormatter will truncate everything in the line until the last null value property. It should be changed to
_valueLine = string.Concat(_valueLine, string.Empty, _options.CsvDelimiter);
Question: In the CsvOutputFormatter, why did you call “GetValue(obj, null)” on the PropertyInfo objects instead of just using “GetValue(obj)”?

Default file name for exported csv file

Hello,
I have been using you code for exporting data from an IQueryable into a CSV file and it works fine.
I just would like to know how can I parametrize the Save window? For example, by giving a default file name (for the time being it's taking the name of my controller without the file extension), Save as type?

Thank you

Sylvain

NuGet package

How about putting this into a NuGet package? i.e. the inputformatter and outputformatter.

Does this handle nested complex data?

JSON (employees)

[
    {
        "id": 1,
        "name": "Tom",
        "products": [
            {
                "id": 10,
                "name": "car"
            },
            {
                "id": 11,
                "name": "truck"
            },
        ]
    },
    {
        "id": 2,
        "name": "Dave",
        "products": [
            {
                "id": 10,
                "name": "car"
            }
        ]
    }
]

CSV

employee_id,employee_name,employee_products1_id,employee_products1_name,employee_products2_id,employee_products1_name
1,Tom,10,Car,11,Truck
2,Dave,10,Car,,


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.