目前,我使用的是关于如何读取CSV文件的教程:https://stackabuse.com/reading-and-writing-csvs-in-java-with-apache-commons-csv/它使用了Apache-commons-csv中的CSVParser和CSVFormat。
它可以读得很好,但不是我想要的方式。在我检查了为什么它不工作的原因后,发现我的CSV文件有分隔符"|“而不是",”,并且CSVParser无法读取它。有没有办法把CSVParser的分隔符改为"|“而不是","?我真的想使用这种方法,因为record.get("name of the map")对我正在做的这个项目真的很有帮助,而且我不想手动更改CSV文件。
这是我当前的代码:
@PostMapping("/uploadFile")
public static void uploadFile(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws IOException{
if(file.getContentType().equalsIgnoreCase("application/vnd.ms-excel")) {
InputStreamReader input = new InputStreamReader(file.getInputStream());
CSVParser csvParser = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(input);
for (CSVRecord record : csvParser) {
System.err.println(record.get("nameOftheColumn"));
}
response.sendRedirect("/rekonsiliasi");
}
else {
response.sendRedirect("/rekonsiliasi");
}
}任何形式的帮助都是值得感谢的。
发布于 2020-05-22 05:19:34
你有没有尝试使用
CSVParser csvParser = CSVFormat.EXCEL.withFirstRecordAsHeader().withDelimiter('|').parse(input);相关JavaDoc请参见http://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#withDelimiter-char-。
https://stackoverflow.com/questions/61906363
复制相似问题