基本思路就是:
- 使用poi读入word文档,
- 读入内容转为html内容,(直接读入text仅仅只是文本没有格式)
- 将html内容给富文本编辑器显示(这样的目的是带格式).
下边贴代码:
poi读入word并转为html.
// word文档路径(带名称):sourceFileName
public static boolean docToHtml(String sourceFileName, String htmlPath) throws Exception {
//poi读入word文件
HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream(sourceFileName));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
//这个根据domSource可以获取到里边的内容,但是不带格式,需要转换.
//转换html文件
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(outStream);
DOMSource domSource = new DOMSource(htmlDocument);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
//content就是需要的html文件
String content = new String(outStream.toByteArray());
System.out.println(content);
//导出.html文件到指定路径 不需要注释掉
/*FileUtils.writeStringToFile(new File(htmlPath, "interface.html"), content,
"utf-8");*/
return true;
}
转换出html样式如下:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">.b1{white-space-collapsing:preserve;}
.b2{margin: 1.0in 1.25in 1.0in 1.25in;}
.s1{color:black;}
.s2{font-family:LiberationSerif;color:black;}
.s3{font-family:宋体;color:black;}
.p1{text-align:start;hyphenate:auto;font-family:宋体;font-size:15pt;}
.p2{text-align:start;hyphenate:auto;font-family:LiberationSerif;font-size:15pt;}
.p3{text-align:justify;hyphenate:auto;font-family:Calibri;font-size:10pt;}
</style>
<meta content="hydata2018" name="author">
</head>
<body class="b1 b2">
<p class="p1">
<span class="s1">(</span><span class="s2">3</span><span class="s1">)</span><span class="s2">GenericApplicationContext</span><span class="s1">是</span><span class="s2">Spring Context</span><span class="s1">模块中最容易构建 </span>
</p>
<p class="p2">
<span class="s1">Spring</span><span class="s3">环境的实体类,涵盖了</span><span class="s1">Spring Context</span><span class="s3">的核心功能,在不需要特殊 </span>
</p>
<p class="p1">
<span class="s1">定制的场景下可以实现开箱即用。如图</span><span class="s2">2-1</span><span class="s1">所示, </span>
</p>
<p class="p2">
<span class="s1">AnnotationConfigApplicationContext</span><span class="s3">完美利用了 </span>
</p>
<p class="p2">
<span class="s1">GenericApplicationContext</span><span class="s3">的封装性和对外简便性,如果想扩展适合自己 </span>
</p>
<p class="p1">
<span class="s1">业务的轻量级 </span><span class="s2">Spring </span><span class="s1">容器,使用</span><span class="s2">GenericApplicationContext</span><span class="s1">这个基类则 </span>
</p>
<p class="p1">
<span class="s1">会非常容易上手。</span><span class="s2">AnnotationConfigApplicationContext</span><span class="s1">的构造方法先传入 </span>
</p>
<p class="p1">
<span class="s1">一个 </span><span class="s2">class </span><span class="s1">数组,再创建一个可执行的上下文实例来构造一个可运行的 </span>
</p>
<p class="p2">
<span class="s1">Spring</span><span class="s3">运行环境,使用起来非常简便。 </span>
</p>
<p class="p3"></p>
</body>
</html>
这里注意:现在的样式都在html中的header里,有些富文本编辑器不支持这个,把上述的内容放到富文本编辑器中丢失样式.
一般富文本支持样式写到标签里:比如<p style="样式">张三李四</p>.
然后我们用的富文本编辑器是:tinymce.
然后咱们在编辑新增保存富文本的时候,只需要将富文本生成的上述的文本存到数据库里就可以了.注意:富文本生成的html的样式都是写到标签里的,没有带头.
然后是下载:就是到处word.这里也有注意,这个下载仅仅是把上述生成的html内容放入流中,文件名设置为.doc就生成了.然后你会发现你下载下来的word文档体积非常小,跟你在windows上创建的差好多,这是因为这个word本质上只是标签,不信你可以用编辑器(notepad++)打开它,发现它内部都是标签,样式都是通过标签过来的.你把他扩展名改为.html也可以正常显示跟word一样.
然后你还会发现,你把下下来的这个word,再上传使用poi解析读取它的时候,会报错,理由是poi不认为它是个.word文件,你说神奇不?
所以上述方法用来在word跟富文本之间转换应该只是取巧的做法,方便省事,但是不是正道,在专门做这方面时可能会出问题.解决方法暂时未找(之所以说未找,是因为我找到这个能解决当前需求就没往下找),
下面看下载代码:看完你就明白我为啥那么说了
try {
//获取内容
String content = notificationContent.getContent();
// 拼一个标准的HTML格式文档
InputStream inStream1 = new ByteArrayInputStream(content.getBytes("UTF-8"));
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(("***.doc").getBytes(), "iso-8859-1"));
response.setCharacterEncoding("UTF-8");
byte[] b = new byte[1024];
int len;
while ((len = inStream1.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
// 关闭流
inStream1.close();
} catch (Exception e) {
}
这中间还有一个需求:在生成word的时候需要根据业务添加一段或者几段话(话术是定好的).
我首先需要定位到我添加内容的位置,然后加入定好的内容.
然后我发现word(html版)中的一段话其实都在一个<p>标签中包着呢,里边含有样式等信息,本质上这一个<p>就是一行,
然后我从富文本编辑器中生成我自己的<p>,追加在定位段落的<p>后边就完成了.
下边贴获取String str=html内容字符串中,每一行(其实就是word中的每个段落)的代码:
String s="里边是标签其实";
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(s.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
String line;
StringBuffer strbuf=new StringBuffer();
while ( (line = br.readLine()) != null ) {
if(!line.trim().equals("")){
if(line.contains("远程视频")){
line+=line;
}
System.out.println(line);
strbuf.append(line);
}
}
System.out.println(strbuf.toString());