Created
November 19, 2014 03:23
-
-
Save magnet88jp/9f117610cbcea32e9ab4 to your computer and use it in GitHub Desktop.
csv import sample controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class ImportSample1Ctrl { | |
| public DataImportHistory__c dih { get; set; } | |
| public ImportSample1Ctrl() { | |
| System.debug('IN ImportSample1Ctrl'); | |
| } | |
| public void init() { | |
| dih = new DataImportHistory__c(); | |
| System.debug('IN init'); | |
| } | |
| public PageReference createData2() { | |
| System.debug('IN createData2'); | |
| String txtImport = dih.ImportData__c; | |
| CSVParser parser = new CSVParser(); | |
| parser.textQualifier = '"'; | |
| parser.delimiter = '\t'; | |
| parser.escapeMode = CSVParser.ESCAPE_MODE_DOUBLED; | |
| // String contents = 'Mode,Price,Count\nFerrari 458 Italia,"$225,325,5"\nFerrari 599 GTB Fiorano,"$336,541,2"'; | |
| String contents = dih.ImportData__c; | |
| List<List<String>> result = parser.parse(contents); | |
| System.debug('result=' + result); | |
| Integer i = 0; | |
| Integer j = 0; | |
| List<ImportData__c> dataList = new List<ImportData__c>(); | |
| for(List<String> resultItems : result) { | |
| j = 0; | |
| ImportData__c data = new ImportData__c(); | |
| for(String resultItem : resultItems) { | |
| System.debug('i=' + i + '; j=' + j + '; result=' + resultItem); | |
| if( j == 0) { | |
| data.Data1__c = resultItem; | |
| } else if( j == 1 ) { | |
| data.Data2__c = resultItem; | |
| } else if( j == 2 ) { | |
| data.Data3__c = resultItem; | |
| } | |
| j++; | |
| } | |
| dataList.add(data); | |
| i++; | |
| } | |
| insert dataList; | |
| dih.ImportData__c = ''; | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment