java web实现在线编辑word,并将word导出(一)

可紊 提交于 2020-08-14 03:48:28

  前段时间领导交代了一个需求:客户需要一个能够web在线编辑文字,如同编辑word文档一样,同时能够将编辑完成的内容导出为word文档并下载到本地。

  我们选择了前台使用富文本插件的形式用于编辑内容,使用的是UEditor(官网:http://ueditor.baidu.com/website/),该插件类似于用于博客园的文章编写前台功能功能,使用了该插件的源码的jsp版本(下载地址:http://ueditor.baidu.com/build/build_down.php?n=ueditor&v=1_4_3_3-utf8-jsp)。

  实例化编辑器,并将后台传递的word内容数据(html形式)展现在编辑区域内。

var ue = UE.getEditor('editor',{
            toolbars: [
                ['undo', 'redo', 'bold','italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|' ,
                    'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'directionalityltr', 'directionalityrtl', 'indent', '|',
                    'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
                    'simpleupload', 'insertimage','link', 'unlink', '|', 'customstyle', 'paragraph', 'fontfamily', 'fontsize','|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
                    ,'horizontal', 'date', 'time', 'spechars', '|', 'preview', 'searchreplace','print']
            ],
        });


ue.ready(function () {
    //content是html标签的内容,附带有css样式
      ue.setContent(content,true)
});

  以上是前台的简单实现,这个插件甚至能够直接粘贴图片!

  后台的实现思路是:由于前台允许在线编辑,所有不能直接在后台生成一个报告文档,需要将前台显示的内容完全传至后台,有后台代码将前台的html界面转换成word文档。

  我在项目中有两种后台实现方式,本篇先介绍前一种实现方式,另一种方式将于下一篇介绍。

  这种方式生成的是doc后缀的文档,是Word2003以前规范的word文档。

@RequestMapping("/defectV2/defect/analysis/tranformHtmlToWord")
    @ResponseBody
    public MessageBean tranformHtmlToWordDocx(@RequestParam Map params,HttpServletRequest request, HttpServletResponse response) {
        try {
//params包含前台传回的html内容
// analysisService.tranformHtmlToWordDoc(params,response); String content = "<html><head></head><body>" + (String) params.get("editorValue") + "</body></html>"; InputStream is = new ByteArrayInputStream(content.getBytes());//utf-8 // OutputStream os = new FileOutputStream("F:/analysis/test.doc"); OutputStream os = response.getOutputStream(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("问题统计分析.doc","utf-8")); POIFSFileSystem fs = new POIFSFileSystem(); fs.createDocument(is, "WordDocument"); fs.writeFilesystem(os); os.close(); is.close(); fs.close(); return new MessageBean("success", "生成报告成功!", null); } catch (Exception e) { e.printStackTrace(); utils.WriteSystemLog(sls, "ERROR", "生成报告", "生成报告失败!" + e.getCause()); return new MessageBean("error", "生成报告失败!", null); } }

  这种方式直接使用的POI附带的功能,在pom.xml需要引入POI的相关依赖。

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!