Coder Social home page Coder Social 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 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

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)”?

NuGet package

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

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

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

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:

DateTime

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

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

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...

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,,


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

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.