CSV importer

1
I'm looking for a CSV importer. I found the following question on the forum: https://community.mendix.com/questions/277/Import-csv-file-directly-into-the-database-is-this-possible Is the given answer still the case?
asked
2 answers
3

Using OpenCSV (which also handles quotes very well) you can do something like:

    // '\u003B' refers to ; as separator. Use the one appropriate for you.
    CSVReader reader = new CSVReader(new InputStreamReader(Core.getFileDocumentContent(this.getContext(), <YourCSVFileObject>.getMendixObject())), '\u003B');
    List<String[]> lines = reader.readAll();
    String[] headers = lines.get(0);

    // Creates batches of 2000 records
    ICreateBatch createBatch = Core.createBatch(this.getContext(), <YourObject>.getType(), 2000, true);
    // Loop through lines. Bypass first line which is the header
    for(int i = 1 ; i< lines.size();i++  )
    {
        String[] splittedline = lines.get(i);
        // Check that records have same number of columns as header. If not throw exception
        if(splittedline.length != headers.length) {
            throw new CoreException(new CoreException());
        }
        if(splittedline.length>1)
        {
            createBatch.create();
            // Loop through values
            for(int j = 0; j < splittedline.length;j++)
            {
                switch (j) {
                case 0:  createBatch.setMemberValue(<YourObject>.MemberNames.<Attr1>.toString(), splittedline[0]);       break;
                case 1:  createBatch.setMemberValue(<YourObject>.MemberNames.<Attr2>.toString(), splittedline[1]);       break;
                case 2:  createBatch.setMemberValue(<YourObject>.MemberNames.<Attr3>.toString(), splittedline[2]);       break;
                case 3:  createBatch.setMemberValue(<YourObject>.MemberNames.<Attr4>.toString(), splittedline[3]);       break;
                case 4:  createBatch.setMemberValue(<YourObject>.MemberNames.<Attr5>.toString(), splittedline[4]);       break;
                }
            }
            createBatch.commit();
        }
    }
answered
0

Convert it to Excel and use the Excel importer.

answered