常量:HTML头
/**
* html头
*/
public static final String WORD_HTML_HEAD = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
"xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"\n" +
"xmlns=\"http://www.w3.org/TR/REC-html40\"><head>\n" +
" <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val=\"Cambria Math\"/><m:brkBin m:val=\"before\"/><m:brkBinSub m:val=\"--\"/><m:smallFrac m:val=\"off\"/><m:dispDef/><m:lMargin m:val=\"0\"/> <m:rMargin m:val=\"0\"/><m:defJc m:val=\"centerGroup\"/><m:wrapIndent m:val=\"1440\"/><m:intLim m:val=\"subSup\"/><m:naryLim m:val=\"undOvr\"/></m:mathPr></w:WordDocument></xml><![endif]-->\n" +
"</head>";
/**
* 图片
*/
public static final String IMG= "<div style=\"margin-top: 30px; text-align: right;\"><span style=\"font-size: 18px;\">签字:</span> <img style=\"height: 40px;\" src='{img}' /> </div>";
/**
* 更换图片
*/
public static final String REPLACE_IMG = "{img}";
Controller
@PostMapping("/doc")
public void downloadDoc(HttpServletResponse response){
service.downloadDoc(response,param);
}
Service
void downloadDoc(HttpServletResponse response);
ServiceImpl
@Override
public void downloadDoc(HttpServletResponse response) {
//替换常量中的图片 url
String imageUrl= "";
String img= CommonConstants.IMG.replace(REPLACE_IMG, imageUrl);
OutputStream out = null;
try {
out = response.getOutputStream();
//数据源
String contents = "富文本内容";
String content = CommonConstants.WORD_HTML_HEAD + "<body>" + contents + "" + img+ "</body></html>";
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
response.setHeader("Content-disposition", "attachment;filename=" + new String((文件名 + ".docx").getBytes(StandardCharsets.UTF_8), "ISO8859-1"));
out.write(content.getBytes(StandardCharsets.UTF_8));
out.flush();
} catch (Exception e) {
log.error("导出word异常", e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
前端下载代码 res就是后台返回的文件流
const url = window.URL.createObjectURL(new Blob([res], { type: 'application/msword;charset=utf-8 ' }));
const link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', '文件名.doc');
document.body.appendChild(link);
link.click();