public void downBatchFileExcel(@PathVariable(value = "batchId") String batchId) throws Exception
{
if(StringUtils.isEmpty(batchId))
{
throw new IllegalArgumentException("参数为空!");
}
ResourceBatchInfo batchInfo = resourceBatchUploadInfoService.selectByPrimaryKey(batchId);
if(null == batchInfo || StringUtils.isEmpty(batchInfo.getFileDir()))
{
throw new IllegalArgumentException("批次信息为空!");
}
File dirFile = new File(batchInfo.getFileDir());
if(null == dirFile || !dirFile.exists())
{
throw new IllegalArgumentException("目录不存在!");
}
List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
Path startingDir = Paths.get(batchInfo.getFileDir());
try (Stream<Path> paths = Files.walk(startingDir))
{
paths
.filter(Files::isRegularFile)
.forEach(path ->
{
System.out.println("name=" + path.getFileName().toString() + ", path = " + path.toString());
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(path.getFileName().toString());
fileInfo.setLocalPath(path.toString());
fileInfoList.add(fileInfo);
});
if(fileInfoList.size() == 0)
{
throw new IllegalArgumentException("文件信息为空!");
}
String templatePath = "/home/jhedu/template/file_list.xlsx"; // Excel模板路径
Workbook workbook = new XSSFWorkbook(templatePath);
Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
// 遍历数据并填充到Excel模板中对应的位置
if(fileInfoList.size() > 0)
{
for(int i=0; i<fileInfoList.size(); i++)
{
Row row = sheet.createRow(i + 2); // 从第3行开始填充数据,假设第一行是标题行
for (int j = 0; j < 6; j++)
{
Cell cell = row.createCell(j);
if(j == 0)
{
cell.setCellValue(fileInfoList.get(i).getFileName());
}
else if(j == 7)
{
cell.setCellValue(fileInfoList.get(i).getLocalPath());
}
}
}
}
String fileName = "filelist_" + batchId + ".xlsx";
// 设置响应内容类型
response.setContentType("application/vnd.ms-excel");
// 设置响应头
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
// 获取输出流
OutputStream out = response.getOutputStream();
// 将工作簿写入输出流
workbook.write(out);
out.flush();
out.close();
workbook.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}