Created
April 26, 2021 14:47
-
-
Save mjhagen/cfd620a4c79f46a650ec0dbe1b429009 to your computer and use it in GitHub Desktop.
CSV Parser using Univocity in CFML / ColdFusion
This file contains 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
component accessors=true { | |
public any function getCSVParser( | |
boolean delimiterDetectionEnabled = true, | |
boolean headerExtractionEnabled = true, | |
boolean lineSeparatorDetectionEnabled = true, | |
boolean quoteDetectionEnabled = true | |
) { | |
var csvParserSettings = createObject( 'java', 'com.univocity.parsers.csv.CsvParserSettings' ); | |
csvParserSettings.setDelimiterDetectionEnabled( delimiterDetectionEnabled ); | |
csvParserSettings.setHeaderExtractionEnabled( headerExtractionEnabled ); | |
csvParserSettings.setLineSeparatorDetectionEnabled( lineSeparatorDetectionEnabled ); | |
csvParserSettings.setQuoteDetectionEnabled( quoteDetectionEnabled ); | |
return createObject( 'java', 'com.univocity.parsers.csv.CsvParser' ).init( csvParserSettings ); | |
} | |
public any function getJavaFileReader( csvFile ) { | |
return createObject( 'java', 'java.io.FileReader' ).init( csvFile ); | |
} | |
public array function readCsvData( required string csvFile ) { | |
return getCSVParser().parseAll( getJavaFileReader( csvFile ) ); | |
} | |
public array function readCsvDataAsRecords( required string csvFile ) { | |
return getCSVParser().parseAllRecords( getJavaFileReader( csvFile ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implement like this: