XWPFDocument doc = null; ByteArrayOutputStream baseOutput = null; InputStream in = null; InputStream inputStream = null; Document document=null; doc = WordExportUtil.exportWord07(templatePath, params); if (StringUtils.equals("1",flag)){ //流的形式给前端 response.setContentType("application/msword;charset=utf-8"); // 设置文件名 response.addHeader("Content-Disposition", "attachment;fileame=" + new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1)); doc.write(response.getOutputStream()); }else { //PDF的形式给前端 baseOutput = new ByteArrayOutputStream(); doc.write(baseOutput); in = new ByteArrayInputStream(baseOutput.toByteArray()); baseOutput.reset(); ShiftPdfUtil.doc2Pdf(in,baseOutput);(把doc转换成pfd,可用别的方法) response.setContentType("application/pdf"); // 设置文件名 response.addHeader("Content-Disposition", "inline;filename=" + new String(fileName.replace("docx","pdf").getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1)); baseOutput.writeTo(response.getOutputStream()); }
word生成,返回给前端流或者doc或者pdf格式
最新推荐文章于 2023-12-20 21:06:14 发布