首先引入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
@Resource
private OSSFactoryConfig ossFactory;
private void exportCSV() {
Date date = new Date();
CsvWriter csvWriter =null;
List<User> us = new ArrayList<>();
User user = new User();
user.setName("张三");
user.setAge(23);
user.setScore(88.32);
user.setPass(true);
user.setTestDate(DateUtil.date());
us.add(user);
User user2 = new User();
user2.setName("李四");
user2.setAge(33);
user2.setScore(88.32);
user2.setPass(false);
user2.setTestDate(DateUtil.date());
us.add(user2);
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String csvName = metadataExportTemplate.getName() + sdf.format(date);
String csvFileName = csvName + ".csv";
// 创建临时文件
File uploadFile = File.createTempFile(csvFileName, null);
csvWriter = CsvUtil.getWriter(uploadFile, CharsetUtil.CHARSET_UTF_8);
csvWriter.writeBeans(us);
FileInputStream fileInputStream = new FileInputStream(uploadFile);
String url = ossFactory.build().upload(fileInputStream, csvFileName);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
csvWriter.flush();
csvWriter.close();
}
}