Преобразование текстовой строки HTML в файл .doc

У меня есть строковая переменная, которая содержит отформатированный текст html, и мне нужно преобразовать ее в файл .doc с помощью apache-poi.

Я получил это решение, используя docx4j для файла .docx, но клиент хочет получить решение, используя apache-poi, который представляет собой преобразование строки html в .doc и .docx.

Итак, как преобразовать текстовую строку html в файлы .doc и .docx из форматированной текстовой строки html с помощью apache-poi при весенней загрузке?

Изменить: решения-

Для Дока:

private String getDocHtmlText(byte[] contents)
            throws FileNotFoundException, IOException, ParserConfigurationException, TransformerConfigurationException,
            TransformerFactoryConfigurationError, TransformerException {
        File file = new java.io.File("reportTemplate.doc");
        FileUtils.writeByteArrayToFile(file, contents);
        InputStream input = new FileInputStream(file);
        HWPFDocument wordDocument = new HWPFDocument(input);
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        WordToHtmlConverter converter = new WordToHtmlConverter(doc);
        converter.processDocument(wordDocument);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {
            DOMSource domSource = new DOMSource(converter.getDocument());
            StreamResult streamResult = new StreamResult(output);
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.METHOD, "html");
            serializer.transform(domSource, streamResult);
        } finally {
            input.close();
            output.close();
            file.delete();
        }
        return output.toString();
    }

Для документа:

private String getDocxHtmlText(byte[] contents) throws IOException, FileNotFoundException {
        File file = new java.io.File("reportTemplate.docx");
        FileUtils.writeByteArrayToFile(file, contents);
        InputStream in = new FileInputStream(file);
        XWPFDocument document = new XWPFDocument(in);
        XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(new File("word/media")));
        OutputStream out = new ByteArrayOutputStream();
        XHTMLConverter.getInstance().convert(document, out, options);
        in.close();
        out.close();
        file.delete();
        return out.toString();
    }

person stackUser    schedule 22.01.2019    source источник
comment
Возможный дубликат Convert HTML to docx — Apache POI Java   -  person g00glen00b    schedule 22.01.2019
comment
Я думал, что это возможно из-за этой ссылки - {stackoverflow.com/a/5403453/9024680} Итак, как преобразовать текстовая строка HTML в файл .doc как docx4j используется только для файлов .docx.   -  person stackUser    schedule 22.01.2019
comment
Также я использовал apache-poi для преобразования .doc и .docx в строку html.   -  person stackUser    schedule 22.01.2019
comment
Для преобразования текстовой строки HTML в *.docx с использованием apache poi и jsoup для обхода HTML см. мой пример в the-same-paragraph/54275245#54275245" title="как установить разные стили для одного и того же абзаца"> stackoverflow.com/questions/54268485/. Создание *.doc? Ну, у меня никогда не было никакого прогресса даже в самых простых вещах, используя apache poi HWPF материал. Я отказался от этого.   -  person Axel Richter    schedule 22.01.2019