public Map<String, List> readCsv(MultipartFile customerCsvMultipartFile) throws IOException, CsvValidationException {
List<CustomerCsvDto> customerCsvData;
Map<String, List> migrationCsvMap = new HashMap<>();
validateCsvFile(customerCsvMultipartFile);
try (InputStreamReader customerReader = new InputStreamReader(customerCsvMultipartFile.getInputStream());) {
CsvToBean<CustomerCsvDto> customerCsvToBean = new CsvToBeanBuilder<CustomerCsvDto>(customerReader)
.withType(CustomerCsvDto.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
customerCsvData = customerCsvToBean.parse();
migrationCsvMap.put("customerCsvData", customerCsvData);
return migrationCsvMap;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
我正在使用 OpenCsv,它提供了一个读取整个 CSV 的功能,以便使用注释直接将列映射到 DTO 属性。我想批量读取 CSV,其中我将传递我将首先读取的记录数量,然后从我已读取的数量开始。如何使用Java中的OpenCsv实现批量读取?
@CsvBindByName(column = "csv_column_name")
获取迭代器并读取您想要的记录数。