java 创建文件夹及文件写入数据到excel

@Test
public void write2Excel(){
String filePath = "C:/logpath/"+"202003"+"/";
String fileName="202003log.xlsx";
XSSFWorkbook workbook = new XSSFWorkbook();
// 创建一个工作表sheet
try {
// 创建一个文件
File file = new File(filePath+fileName);
if (!file.getParentFile().exists()) {
//判断父目录路径是否存在,即202003log.xlsx前的C:/logpath/202003/
                try {
file.getParentFile().mkdirs();
//不存在则创建父目录
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream outputStream = new FileOutputStream(file);
workbook.write(outputStream);
workbook.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
posted @ 2021-03-15 17:06  HalleyZ  阅读(448)  评论(0编辑  收藏  举报