富文本在库里是以标签的形式存储
这里需要富文本什么样导出还是什么样
controller :
@Controller
@RequestMapping("/fck")
public class FckController {
/**
* 测试富文本导出word
*/
@RequestMapping("/export")
public void exportWord( HttpServletRequest request, HttpServletResponse response, String id) throws Exception {
try {
Fck fck = fckService.getFckbyId(id);
//word内容
String content="<html><meta charset=\"utf-8\" /><body>";
content += fck.getText();
content += "</body></html>";
//获取绝对路径 这里要与文件存储的路径一样
String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/fck/";
path = path.substring(1);
//替换里面的所有图片路径
content = content.replaceAll("<img alt=\"\" src=\"", "<img alt=\"\" src=\"" + path);
byte b[] = content.getBytes("utf-8"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
//* 关键地方
//* 生成word格式
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//设置文件名
String fileName = fck.getId();
//处理文件名乱码
String userAgent = request.getHeader("User-Agent");
// 针对IE或者以IE为内核的浏览器:
if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} else {
// 非IE浏览器的处理:
fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
}
//输出文件
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".doc");
OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}catch(Exception e){
System.err.println(e);
}
}
}
页面js:
$(function() {
$.post(
'/fck/get_all_id',
function(data) {
console.log(data);
for (var i = 0; i < data.length; i++) {
$('#listid').append('<li><a href="JavaScript:void(0);" οnclick="setin(\'' + data[i] +'\')">' + data[i] + '</a></li>');
}
}
)
});
//获取内容
function onon() {
var val = CKEDITOR.instances.editor.getData();
if (val.length == 0) {
alert("空白");
}
else {
console.log(val);
$('#text').val(val);
$('#fck').submit();
}
}
//设置内容
function setin(id) {
$.post(
'/fck/get_one?id=' + id,
function (data) {
console.log(data);
$('#id').val(data.id);
CKEDITOR.instances.editor.setData(data.text);
}
)
}
//导出word
function exportWord() {
var id = $('#id').val();
location = '/fck/export?id=' + id;
}
<!DOCTYPE html>
<!--
Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
-->
<html>
<head>
<meta charset="utf-8"/>
<title>CKEditor Sample</title>
<script src="/ckeditor/ckeditor.js"></script>
<script src="/ckeditor/samples/js/sample.js"></script>
<link rel="stylesheet" href="/ckeditor/samples/css/samples.css"/>
<script src="/js/jquery.min.js"></script>
<!-- <link rel="stylesheet" href="toolbarconfigurator/lib/codemirror/neo.css"> -->
</head>
<body>
<div id="editor"></div>
<form action="/fck/add" id="fck">
<input id="id" name="id" type="hidden"/>
<input id="text" name="text" type="hidden"/>
</form>
<div>
<input type="button" value="保存" οnclick="onon()"/>
<input type="button" value="导出" οnclick="exportWord()"/>
</div>
<ul id="listid">
</ul>
<script>
initSample();
</script>
<script src="/js/fck.js"></script>
</body>
</html>
访问:http://localhost/fck/page
导出后:
源码:https://github.com/dream-broken/springbootEasyFrame/tree/master/src/main/java/com/dream/springbootframe/fck