html is saved as word (rich text editor exports content as word)

In the past few days, there is a requirement in the project, use a rich text editor, and then export word.

Rich text editor, there are many online, it is very simple to use, we use kindEditor. Baidu's ueditor is very good, and the documentation is very complete. Ali's kissy feels more complicated. The one we write on the blog seems to be wangEditor. It is generally very simple to use.

Here we focus on exporting. How to save the content of the editor to a word document.

The general idea is this, (1) get the content of the editor, if it has html tags, (2) get the css used by the editor. (3) Write these contents into word in the form of standard html to generate temporary files. (4) Export

When we actually use it, we first generate a temporary file, and then read the temporary file and export it. There are also many export functions online.

The demo mainly shows how to generate an exported temporary file, and the exported code can be found online. It should be noted that temporary files are to be deleted, otherwise they will take up space. When deleting, the method file.delete() must be called after the streams are closed, otherwise it cannot be deleted. Because the file is shared by the stream.

Change the css in the demo, and replace the text content with the one passed by your editor.

The demo code is as follows:

import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Created by weiyuan on 2018/2/10/010.
 */
public class Editor {
    public static void main(String[] args) {
    Editor editor=new Editor();
        try {
            editor.docFile();
        } catch (Exception e) {
            e.printStackTrace ();
        }

    }
    public  void docFile() throws Exception {
        InputStream bodyIs = new FileInputStream("f:\\2.html");
        InputStream cssIs2 = new FileInputStream("f:\\ueditor.css");
        InputStream cssIs1 = new FileInputStream("f:\\codemirror.css");
        String body = this.getContent(bodyIs);
        String css1 = this.getContent(cssIs1);
        String css2 = this.getContent(cssIs2);
        //拼一个标准的HTML格式文档
String content = "<html><head><style>" + css1+"\\n" +css2 + "</style></head><body>" + body + "</body></html>";
        InputStream is = new ByteArrayInputStream(content.getBytes("GBK"));
        OutputStream os = new FileOutputStream("f:\\2.doc");
        this.inputStreamToWord(is, os);
    }

    /**
      * Write is to the corresponding word output stream os
 * Ignore exception capture, throw directly
 * @param is
 * @param os
 * @throws IOException
      */
 private void inputStreamToWord(InputStream is, OutputStream os) throws IOException {

        POIFSFileSystem fs = new POIFSFileSystem();
        //对应于org.apache.poi.hdf.extractor.WordDocument
fs.createDocument(is, "WordDocument");
        fs.writeFilesystem(os);
        os.close();
        is.close();
    }

    /**
      * Extract the content of the input stream as text in UTF-8 encoding.
* Ignore exceptions, throw directly
 * @param ises
 * @return
 * @throws IOException
      */
 private String getContent(InputStream... ises) throws IOException {
         if (ises != null ) {
            StringBuilder result = new StringBuilder();
            BufferedReader br;
            String line;
            for (InputStream is : ises) {
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line=br.readLine()) != null) {
                    result.append(line);
                }
            }
            return result.toString();
        }
        return null;
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325406247&siteId=291194637