<el-button type="text" size="mini" icon="el-icon-bottom" @click="downloadFile(scope.row.id)">下载</el-button>
async downloadFile(id) {
this.loading = true
const res = await cfmmcReportApi.downloadFile({
cfmmcReportId: id
})
if (res.code === 200) {
if (res.data.zbdcExcel !== null) {
const el = document.createElement('a')
el.setAttribute('href', 'data:xlsx;base64,' + res.data.zbdcExcel)
el.setAttribute('download', 'a.xls')
document.body.append(el)
el.click()
}
if (res.data.zrdsExcel !== null) {
const el = document.createElement('a')
el.setAttribute('href', 'data:xlsx;base64,' + res.data.zrdsExcel)
el.setAttribute('download', 'b.xls')
document.body.append(el)
el.click()
}
this.$message.success(res.msg)
this.loading = false
} else {
this.$message.error(res.msg)
this.loading = false
}
}
@PostMapping({"/downloadFile"})
public AjaxResult<DownloadReportVo> downloadFile(@RequestBody DownloadDto dto){
Assert.notNull(dto.getCfmmcReportId(),"结算单id不能为空!");
return trsCfmmcReportService.downloadFile(dto.getCfmmcReportId());
}
@Override
public AjaxResult<DownloadReportVo> downloadFile(Long cfmmcReportId) {
LambdaQueryWrapper<CfmmcReportModel> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(BaseModel::getId,cfmmcReportId);
CfmmcReportModel cfmmcReportModel = cfmmcReportMapper.selectOne(queryWrapper);
String base64String = null;
Path path = Paths.get(cfmmcReportModel.getReportUrl());
String absolutePath = path.toAbsolutePath().toString();
try (FileInputStream fileInputStream = new FileInputStream(absolutePath);
Workbook workbook = new HSSFWorkbook(fileInputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
workbook.write(outputStream);
byte[] excelBytes = outputStream.toByteArray();
base64String = Base64.getEncoder().encodeToString(excelBytes);
return AjaxResult.success("Excel下载完成", base64String);
} catch (IOException e) {
e.printStackTrace();
return AjaxResult.error("Excel下载失败", null);
}
DownloadReportVo vo = new DownloadReportVo();
if ("a".equals(cfmmcReportModel.getReportType())) {
vo.setZbdcExcel(base64String);
}else if ("b".equals(cfmmcReportModel.getReportType())) {
vo.setZrdsExcel(base64String);
}
return AjaxResult.success("下载成功!",vo);
}