java讀取csv文件

1.添加maven依賴

 

 

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.4</version>
</dependency>

 

2. 相關代碼

public static List<CSVRecord> getcsvRecordListFromStr(String buff) throws IOException {
    ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(buff.getBytes());
    Reader csvReader = new InputStreamReader(tInputStringStream, "UTF-8");
    CSVParser csvParser = new CSVParser(csvReader, CSVFormat.EXCEL.withHeader());
    List<CSVRecord> csvRecordList = csvParser.getRecords();
    return csvRecordList;
}

 

List<CSVRecord> records = CommonUtils.getcsvRecordListFromStr(buff);
records.remove(0);
for(CSVRecord record:records) {
    String settlementDay = record.get("結算日期\t").trim();

}apache

相關文章
相關標籤/搜索