请教java html导出word如何实现
1个回答
展开全部
java将html导出word不用忘记<html></html>这对标签
//换页
<span style='font-size:16px;line-height:150%;font-family:"Times New Roman";
mso-fareast-font-family:宋体;mso-font-kerning:1px;mso-ansi-language:EN-US;
mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='mso-special-character:page-break;page-break-before:always'>
</span>
//换行
<p style='line-height:150%'><span style='font-size:16px;line-height:150%'><o:p> </o:p></span></p>
查看的话 打开word 视图——页面 就能看出看出效果
[java] view plain copy print?
ArrayList records = form.getRecords();//获取数据库数据
if(null!=records&&0!=records.size()){
//html拼接出word内容
String content="<html>";
for (int i = 0; i < records.size(); i++) {
Record record =(Record) records.get(i);
//从数据库中获得数据,将oracle中的clob数据类型转换成string类型
Method method = record.get("CONTENT").getClass().getMethod("getVendorObj",new Class[]{});
CLOB clob = (CLOB)method.invoke(record.get("CONTENT"));
String cx = clob.getSubString((long) 1, (int) clob.length());
String title= (String) record.get("TITLE");
//html拼接出word内容
content+="<div style=\"text-align: center\"><span style=\"font-size: 24px\"><span style=\"font-family: 黑体\">"+title+"<br /> <br /> </span></span></div>";
content+="<div style=\"text-align: left\"><span >"+cx+"<br /> <br /> </span></span></div>";
//插入分页符
content+="<span lang=EN-US style='font-size:16px;line-height:150%;mso-fareast-font-family:宋体;mso-font-kerning:1px;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'></span>";
content+="<p class=MsoNormal style='line-height:150%'><span lang=EN-US style='font-size:16px;line-height:150%'><o:p> </o:p></span></p>";
}
content += "</html>";
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//输出文件
String name="导出知识";
response.reset();
response.setHeader("Content-Disposition",
"attachment;filename=" +
new String( (name + ".doc").getBytes(),
"iso-8859-1"));
response.setContentType("application/msword");
OutputStream ostream = response.getOutputStream();
//输出文件的话,new一个文件流
//FileOutputStream ostream = new FileOutputStream(path+ fileName);
poifs.writeFilesystem(ostream);
ostream.flush();
ostream.close();
bais.close();
//换页
<span style='font-size:16px;line-height:150%;font-family:"Times New Roman";
mso-fareast-font-family:宋体;mso-font-kerning:1px;mso-ansi-language:EN-US;
mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='mso-special-character:page-break;page-break-before:always'>
</span>
//换行
<p style='line-height:150%'><span style='font-size:16px;line-height:150%'><o:p> </o:p></span></p>
查看的话 打开word 视图——页面 就能看出看出效果
[java] view plain copy print?
ArrayList records = form.getRecords();//获取数据库数据
if(null!=records&&0!=records.size()){
//html拼接出word内容
String content="<html>";
for (int i = 0; i < records.size(); i++) {
Record record =(Record) records.get(i);
//从数据库中获得数据,将oracle中的clob数据类型转换成string类型
Method method = record.get("CONTENT").getClass().getMethod("getVendorObj",new Class[]{});
CLOB clob = (CLOB)method.invoke(record.get("CONTENT"));
String cx = clob.getSubString((long) 1, (int) clob.length());
String title= (String) record.get("TITLE");
//html拼接出word内容
content+="<div style=\"text-align: center\"><span style=\"font-size: 24px\"><span style=\"font-family: 黑体\">"+title+"<br /> <br /> </span></span></div>";
content+="<div style=\"text-align: left\"><span >"+cx+"<br /> <br /> </span></span></div>";
//插入分页符
content+="<span lang=EN-US style='font-size:16px;line-height:150%;mso-fareast-font-family:宋体;mso-font-kerning:1px;mso-ansi-language:EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'><br clear=all style='page-break-before:always'></span>";
content+="<p class=MsoNormal style='line-height:150%'><span lang=EN-US style='font-size:16px;line-height:150%'><o:p> </o:p></span></p>";
}
content += "</html>";
byte b[] = content.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(b);
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//输出文件
String name="导出知识";
response.reset();
response.setHeader("Content-Disposition",
"attachment;filename=" +
new String( (name + ".doc").getBytes(),
"iso-8859-1"));
response.setContentType("application/msword");
OutputStream ostream = response.getOutputStream();
//输出文件的话,new一个文件流
//FileOutputStream ostream = new FileOutputStream(path+ fileName);
poifs.writeFilesystem(ostream);
ostream.flush();
ostream.close();
bais.close();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |