fck富文本(二)之以Word形式导出 java

富文本在库里是以标签的形式存储


这里需要富文本什么样导出还是什么样

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值