У меня есть строковая переменная, которая содержит отформатированный текст 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();
}
*.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