指定磁盘路径:
HSSFWookbook wb = new HSSFWookbook();
//表中写数据 省略
String fileName= "表名.xls";
LocalDateTime date = LocalDateTime.now();
String path = String.format("D:\\a\\%d\\%d\\",date.getYear(),date.getMonth().getValue());
File pathFile = new File(path);
if(!pathFile.exists()){
if(!pathFile.mkdirs()){
throw new RuntimeException("创建文件路径错误")
}
}
try{
FileOutputStream fileOutputStream = new FileOutputStream(path + fileName);
wb.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}catch(Exception e){
e.printStackTrace();
}
浏览器端(可参考springmvc文件下载)
传送门:https://blog.csdn.net/weixin_49419695/article/details/123697650?spm=1001.2014.3001.5501
@RequestMapping("/sadasd")
public void a(HttpServletRequest req,HttpServletResponse res){
//数据添加到表格省略
String filename = "xxxx.xls";
res.setContentType("application/vnd.ms-excel");
res.setHeader("Contest-Disposition","filename=" + URLEncoder.encode(fileName,"UTF-8"));
OutputStream ops = res.getOutputStream();
wb.write(ops);
ops.flush();
ops.close();
}