Всем привет! Я пытаюсь прочитать файл .docx с помощью java API Apache POI. Я использую:
public static String view(String nameDoc){
String text = null;
try{
XWPFDocument docx = new XWPFDocument(
new FileInputStream(nameDoc));
XWPFWordExtractor we = new XWPFWordExtractor(docx);
text = we.getText();
we.close();
docx.close();
}catch (Exception e){
e.printStackTrace();
}
return text;
}
В этом случае я получаю только текст файла, но у меня все файлы разные. В некоторых из них встречается не только текст, но и таблицы, изображения и т.д. Как мне получить полностью содержание файла?
По совету Max использую wordtohtmlconverter
public static String getDocHtml(String nameDoc){
String html = null;
try {
Document newDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument);
HWPFDocument doc = new HWPFDocument(new FileInputStream(nameDoc));
wordToHtmlConverter.processDocument(doc);
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.METHOD, "html");
transformer.transform(
new DOMSource(wordToHtmlConverter.getDocument()),
new StreamResult(stringWriter));
html = stringWriter.toString();
}catch (Exception e){
e.printStackTrace();
}
return html;
}
Отправляю в jsp, но на странице ничего не получаю.
Ошибка: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)