我使用的是open csv 5.0
能够读取从csv文件导入的数据
我面临一个关于
dateoffsetdatetime格式的转换。
这是我的密码:
模型:
import com.opencsv.bean.CsvBindByName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.time.OffsetDateTime;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class IndProdCsv {
@CsvBindByName
private Long id;
@CsvBindByName
private String service;
@CsvBindByName
private OffsetDateTime time;
@CsvBindByName
private Long nbAppels;
@CsvBindByName
private Double tempsDeReponseMoyenMillisecondes;
@CsvBindByName
private Long volume;
@CsvBindByName
private Double tempsDeReponseMoyenSecondes;
}
控制器:
@PostMapping("/upload-csv-file")
public String uplaodCSVFile(@RequestParam("file") MultipartFile file, Model model){
if (file.isEmpty()){
model.addAttribute("message", "Veuillez selectionner un fichier csv à importer.");
model.addAttribute("status", false);
}else try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
// create csv bean reader
CsvToBean<IndProdCsv> csvToBean = new CsvToBeanBuilder(reader)
.withType(IndProdCsv.class)
.withSeparator(';')
.withIgnoreLeadingWhiteSpace(true)
.build();
// convert `CsvToBean` object
List<IndProdCsv> myIndProdCsv = csvToBean.parse();
model.addAttribute("myIndProdCsv", myIndProdCsv);
model.addAttribute("status", true);
} catch (Exception ex) {
model.addAttribute("message", "An error occurred while processing the CSV file.");
model.addAttribute("status", false);
}
return "mon-dasboard";
}
html和html格式:
tr th:each=“ind,i:${myindprodcsv}”
td th:text=“${ind.time}”></td
/tr公司
下面是错误:
Caused by: org.apache.commons.beanutils.ConversionException: Can't convert value '2020-09-18T00:50:00+02:00' to type class java.time.OffsetDateTime
at org.apache.commons.beanutils.converters.AbstractConverter.conversionException(AbstractConverter.java:474)
at org.apache.commons.beanutils.converters.StringConverter.convertToType(StringConverter.java:96)
at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:169)
at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:61)
at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:491)
at com.opencsv.bean.ConverterPrimitiveTypes.convertToRead(ConverterPrimitiveTypes.java:118)
... 9 more
谢谢你的帮助
暂无答案!
目前还没有任何答案,快来回答吧!