static Document process(File docFile, HtmlDocumentFacade htmlDocumentFacade) throws Exception { final HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(docFile)); DocConverter converter = new DocConverter(htmlDocumentFacade); converter.processDocument(wordDocument); return converter.getDocument(); }
static Document process(File docFile) throws Exception { final HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(docFile)); DocConverter converter = new DocConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); converter.processDocument(wordDocument); return converter.getDocument(); }
/** * test * * @param args */ public static void main(String[] args) { String filePath = "c:\\poi\\test.doc"; // String filePath = "c:\\poi\\××××内部文档保密系统20120425 v4.1 [秘密]1.doc"; String output = "c:/poi/output/test.html"; DocConverter.convert(filePath, output); }
/** 转换文档 */ public static String convert(String filePath, String output) { HtmlDocumentFacade facade = null; // File docFile = new File(docPath); try { facade = new HtmlDocumentFacade( DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); String s1 = "background-color:gray"; facade.addStyleClass(facade.body, "body", s1); } catch (Exception e) { e.printStackTrace(); } if (AbstractWordUtils.isEmpty(filePath) || AbstractWordUtils.isEmpty(output)) { System.err.println("docPath OR output is empty. >>!quit"); return ""; } System.out.println("Converting " + filePath); System.out.println("Saving output to " + output); DocConverter.output = output .substring(0, output.lastIndexOf(".html")) .concat(File.separator) .concat("images") .concat(File.separator); // get fileName new File(DocConverter.output).mkdirs(); try { Document doc = null; if (facade == null) doc = DocConverter.process(new File(filePath)); else { Document document = facade.getDocument(); Element window = document.createElement("div"); Element center = document.createElement("center"); center.appendChild(window); facade.addStyleClass( window, "window", "border:2px solid green;width:800px!important; margin:0 auto; background-color:#fff; text-align:left;"); facade.body.appendChild(center); pageContainer = window; doc = DocConverter.process(new File(filePath), facade); } FileWriter out = new FileWriter(output); DOMSource domSource = new DOMSource(doc); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); // TODO set encoding from a command argument serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "html"); serializer.transform(domSource, streamResult); out.close(); } catch (Exception e) { e.printStackTrace(); } return ""; }