@PostMapping("/export_doc")
@ApiOperationSupport(order = 15)
@ApiOperation(value = "导出word", notes = "")
public String exportDoc(Long contractId,HttpServletResponse httpResponse) {
try {
StringBuffer sbf = new StringBuffer();
// 这里拼接一下html标签,便于word文档能够识别
sbf.append("<html " +
"xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\"" + //将版式从web版式改成页面试图
">");
sbf.append("<head>" +
"<!--[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]-->" +
"</head>");
sbf.append("<body>");
// 富文本内容
sbf.append(printPdf(contractId).getData());
sbf.append("</body></html>");
byte[] b = sbf.toString().getBytes("GBK");
POIFSFileSystem fileSystem = new POIFSFileSystem();
DirectoryEntry directory = fileSystem.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", new ByteArrayInputStream(b));
// 导出word格式
httpResponse.setContentType("application/msword");
httpResponse.addHeader("Content-Disposition", "attachment;filename=" +
new String("ces".getBytes("GB2312"),"iso8859-1") + ".doc");
ServletOutputStream outputStream = httpResponse.getOutputStream();
fileSystem.writeFilesystem(outputStream);
} catch (Exception ex) {
return "下载失败";
}
return "下载成功";
}
11-07
203
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-11