The opencsv project at http://opencsv.sourceforge.net/ is a nice overall java based csv parser library that can automatically support qualified strings that contain comma's. The below code works in ColdFusion 2021 as is with the opencsv.jar file but needs an additional jar with Lucee 5.3.9.
ColdFusion and Lucee:
Grab the source from opencsv.sourceforge.net and run mvn to produce the jar. I used version 5.6.
Lucee:
Download the apache commons lang3 source from https://commons.apache.org/proper/commons-lang/download_lang.cgi and compile with maven.
opencsv-5.6.1-SNAPSHOT.jar |
commons-lang3-3.12.0.jar |
Put the jar(s) in the lib folder of the server or use a jar loader. start up the server and run the following test (adjust the path to your sample csv data file). which should produce the data below (excluding the other dumps)
<cfscript> csvFile ="#expandPath('.')#/csv_data/addresses.csv"; csvData = []; fileReader = createobject("java","java.io.FileReader").init(csvFile); writeDump(fileReader); csvReader = createObject("java","com.opencsv.CSVReader").init(fileReader); writeDump(csvReader);
csvData = csvReader.readAll(); writeDump(csvData); csvReader.close(); </cfscript>
No comments:
Post a Comment