生成word前台

exportHandle() {
this.loading = true
// 准备下载
axios({
method: 'post',
url: process.env.VUE_APP_BASE_API + '/dashboard/export',
data: this.queryParams,
responseType: 'blob',
headers: { 'Authorization': 'Bearer ' + getToken() }
}).then(res => {
const blob = res.data
const reader = new FileReader()
reader.readAsDataURL(blob)
reader.onload = (e) => {
const a = document.createElement('a')
// 获取选择的基金名称
const fileName = '【Dashboard报表】'
a.download = fileName + `.xlsx`
a.href = e.target.result
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
this.loading = false
}
})
}

 

 

 

 

 

@Transactional(readOnly = false)
public void getWork(String id,HttpServletResponse response) throws IOException, TemplateException {

FundDisclosure fundDisclosure = this.getById(id);
String templateName;
Map<String,Object> map;
if(Arrays.asList("1","3").contains(fundDisclosure.getReportPeriodSeason())){
map = getQuarterReportData(fundDisclosure);
Template template = wordConfiguration.getTemplate("/xinPiQuarterReport.ftl");
ServletOutputStream servletOutputStream = response.getOutputStream();
Writer out = new BufferedWriter(new OutputStreamWriter(servletOutputStream, "utf-8"));
template.process(map,out);
}else{
map = getYearReportData(fundDisclosure);
MieFileUtilsFund.downloadFreemarkerGenerateWordFile(map, response, wordConfiguration, "/xinPiReport", "基金信披报告.doc");
}

/*
//获取类加载的根路径
//获取项目地址
File dizhi = new File(this.getClass().getResource("/").getPath());
String dizhiStr = dizhi.toString() + "/templates";
WordUtil.createWord(map, "xinPiReport.ftl", dizhiStr, "xinPiReport.doc", this);
OutputStream outputStream = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
InputStream inStream = null;
try {
// 读到流中
File file = new File(dizhiStr + "/xinPiReport.doc");
inStream = new FileInputStream(file);
String fileName = "信披报告";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".doc").getBytes(), "iso-8859-1"));
outputStream = response.getOutputStream();

bis = new BufferedInputStream(inStream);
bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();

} finally {

if (null != bis) {
bis.close();
}
if (null != bos) {
bos.close();
}
if (null != outputStream) {
outputStream.flush();
outputStream.close();
}

}*/
}

posted @ 2025-04-21 17:22  翘中之楚  阅读(4)  评论(0)    收藏  举报