最近有个需求,需要将excel中的内容导入到数据库中。由于我们的excel内容不包含公式等,只是文字信息,采用csv格式的会更方便。在网上查找到目前比较好的Java CSV读写库是OpenCSV,于是就开始撸代码了。
InputStream inputStream = applicationContext.getAssets().open("metro_data2.csv", Context.MODE_PRIVATE);
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
CSVReader csvReader = new CSVReader(reader);
String [] header = reader.readNext();
String[] nextRecord;
while ((nextRecord = csvReader.readNext()) != null) {
}
代码执行到创建CSVReader对象new CSVReader(reader)的时候就报错:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/lang3/ObjectUtils;
报错很清楚,就是缺少了ObjectUtils这个类。这个类所在jar包的下载地址。
https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
在gradle依赖中加上下面这个依赖,重新编译就可以了。
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'