准备HTML模板。
<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">
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<title/>
<style> v\: * {
behavior: url(#default#VML);
}
o\: * {
behavior: url(#default#VML);
}
w\: * {
behavior: url(#default#VML);
}
.shape {
behavior: url(#default#VML);
}
</style>
<style> @page {
mso-page-orientation: landscape;
size: 29.7cm 21cm;
margin: 1cm 1cm 1cm 1cm;
}
@page Section1 {
mso-header-margin: .5in;
mso-footer-margin: .5in;
mso-header: h1;
mso-footer: f1;
}
div.Section1 {
page: Section1;
}
table#hrdftrtbl {
margin: 0in 0in 0in 900in;
width: 1px;
height: 1px;
overflow: hidden;
}
p.MsoFooter, li.MsoFooter, div.MsoFooter {
margin: 0in;
margin-bottom: .0001pt;
mso-pagination: widow-orphan;
tab-stops: center 3.0in right 6.0in;
font-size: 12.0pt;
}
</style>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:Zoom>100</w:Zoom>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
</head>
<body>
<div class="Section1">
<!--此处插入需要显示的html文本内容 -->
<br/>
<table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'>
<tr>
<td>
<div style='mso-element:header' id=h1 >
<!-- HEADER-tags 页眉
注意如果页眉有多种内容,class必须是MsoHeader
-->
<p class=MsoHeader >附件
<!-- mso-tab-count:30 代表空格30个Tab-->
<span style=mso-tab-count:30'/>
第<span style='mso-field-code: PAGE '>
<span style='mso-no-proof:yes'/>
</span>页 共<span style='mso-field-code: NUMPAGES '/>页
</p>
<p class=MsoHeader >证书编号:2021300907000026</p>
<p class=MsoHeader >产品型号/规格如下:</p>
<!-- end HEADER-tags -->
</div>
</td>
<td>
<div style='mso-element:footer' id=f1>
<span style='position:relative;z-index:-1'>
<!-- FOOTER-tags 页脚 -->
注:此附件与证书同时使用时有效。
<span style='mso-no-proof:yes'/>
<!--<p class=MsoFooter>
<span style=mso-tab-count:2'></span>
第<span style='mso-field-code: PAGE '>
<span style='mso-no-proof:yes'></span>
</span>页 共<span style='mso-field-code: NUMPAGES '></span>页
</p> -->
<!-- end FOOTER-tags -->
</div>
<div style='mso-element:header' id=fh1>
<p class=MsoHeader>
<span lang=EN-US style='mso-ansi-language:EN-US'> <o:p/>
</span>
</p>
</div>
<div style='mso-element:footer' id=ff1>
<p class=MsoFooter>
<span lang=EN-US style='mso-ansi-language:EN-US'> <o:p/>
</span>
</p>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
- 根据实际业务插入文本内容(service层逻辑)。
StringBuffer str = new StringBuffer();
str.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\">");
str.append("<head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\"><title></title> \t\t\t<style> v\\:* {behavior:url(#default#VML);} o\\:* {behavior:url(#default#VML);} w\\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} \t\t\t</style> \t\t\t<style> @page { mso-page-orientation: landscape; size:29.7cm 21cm; margin:1cm 1cm 1cm 1cm; } @page Section1 { mso-header-margin:.5in; mso-footer-margin:.5in; mso-header: h1; mso-footer: f1; } div.Section1 { page:Section1; } table#hrdftrtbl { margin:0in 0in 0in 900in; width:1px; height:1px; overflow:hidden; } p.MsoFooter, li.MsoFooter, div.MsoFooter { margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; font-size:12.0pt; } \t\t\t</style> \t\t\t<xml> \t\t\t\t<w:WordDocument> \t\t\t\t\t<w:View>Print</w:View> \t\t\t\t\t<w:Zoom>100</w:Zoom> \t\t\t\t\t<w:DoNotOptimizeForBrowser/> \t\t\t\t</w:WordDocument> \t\t\t</xml> \t\t</head>");
str.append("<body><div class=\"Section1\">");
// 插入文本内容
str.append(StringEscapeUtils.unescapeHtml(authenticationApplication.getAttachmentContent()));
fstr=authenticationApplication.getApplicationNumber()+"证书附件";
str.append("<table id='hrdftrtbl' border='0' cellspacing='0' cellpadding='0'>");
str.append("<tr><td> <div style='mso-element:header' id=h1 >");
str.append("<p class=MsoHeader >附件 <span style=mso-tab-count:30'></span> 第<span style='mso-field-code: PAGE '> <span style='mso-no-proof:yes'></span> </span>页 共<span style='mso-field-code: NUMPAGES '></span>页 </p>");
str.append("<p class=MsoHeader >证书编号:");
if(StringUtils.isNotEmpty(certificateIssueModel.getNumber())){
str.append(certificateIssueModel.getNumber());
}
str.append("</p>");
str.append("<p class=MsoHeader >产品型号/规格如下:</p>");
str.append("</div> </td> <td> <div style='mso-element:footer' id=f1><span style='position:relative;z-index:-1'> 注:此附件与证书同时使用时有效。 <span style='mso-no-proof:yes'></span> </div> <div style='mso-element:header' id=fh1> <p class=MsoHeader><span lang=EN-US style='mso-ansi-language:EN-US'> <o:p></o:p></span></p> </div> <div style='mso-element:footer' id=ff1> <p class=MsoFooter><span lang=EN-US style='mso-ansi-language:EN-US'> <o:p></o:p></span></p> </div> </td></tr> </table> </div> </body> </html>");
FileUtils.writeWordFileAll(str.toString(),pacakageName, fstr);
2.html生成word帮助FileUtils类。
/**
* 1.6.6 生成word
* @param content
* @param mkdirName 文件夹名称
* @param newFileName 文件名称
* @return
*/
public static String writeWordFileAll(String content,String mkdirName,String newFileName) {
String newFilePath = "";
ByteArrayInputStream bais = null;
FileOutputStream fos = null;
try {
//String content = readFile(filepath);
byte b[] = content.getBytes("utf-8");
bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
String srcPath = RzPdfConfig.targetBaseDir + File.separator + mkdirName;
File file = new File(srcPath);
// 如果pdf保存路径不存在,则创建路径
if (!file.exists()) {
file.mkdirs();
}
newFilePath = srcPath + File.separator+newFileName+".doc";
fos = new FileOutputStream(newFilePath);
poifs.writeFilesystem(fos);
bais.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return newFilePath;
}
3.结果展示。