1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| public static void main(String argv[]) { try { String oldFileName = "D:/Test/ml/test.doc"; String newFileName = "D:/Test/ml/html_to_word.doc"; String temporary = new File(oldFileName).getParent()+File.separator+"temporary.html";
Map<String,Object> map = convert2Html(oldFileName); List<String> listPics = (List<String>) map.get("listPics"); String htmlContent = (String) map.get("htmlContent"); for (String pic : listPics){ if (pic.lastIndexOf(".") != -1){ String temp = htmlContent.substring( htmlContent.indexOf("<img src=\""+pic.replace("\\","/")+"\"")); temp = temp.substring(0,temp.indexOf(">")+1); htmlContent = htmlContent.replaceAll(temp,"<span>{{title}}</span>"); }else {
String temp = htmlContent.substring( htmlContent.indexOf("<img src=\""+pic.replace("\\","/")+"\"")); temp = temp.substring(0,temp.indexOf(">")+1); htmlContent = htmlContent.replaceAll(temp,"<span></span>"); } } writeFile(htmlContent, temporary); html2Doc(temporary,newFileName); } catch (Exception e) { e.printStackTrace(); } }
public static void writeFile(String content, String path) {
FileOutputStream fos = null; BufferedWriter bw = null; org.jsoup.nodes.Document doc = Jsoup.parse(content); String styleOld=doc.getElementsByTag("style").html(); styleOld=styleOld.replaceAll("font-family:.+(?=;\\b)", "font-family:SimSun");
doc.getElementsByTag("head").empty(); doc.getElementsByTag("head") .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></meta>"); doc.getElementsByTag("head") .append(" <style type=\"text/css\"></style>"); doc.getElementsByTag("style") .append(styleOld);
content=doc.html(); content=content .replace("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">", "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></meta>"); try { File file = new File(path); fos = new FileOutputStream(file); bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8")); bw.write(content); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if (bw != null) bw.close(); if (fos != null) fos.close(); } catch (IOException ie) { } } }
public static Map<String,Object> convert2Html(String fileName) throws Exception {
Map<String,Object> map = new HashMap<>(); List<String> listPics = new ArrayList<>(); String imgPath = new File(fileName).getParent()+ File.separator+"img"+File.separator; File file = new File(imgPath); if (!file.exists()){ file.mkdirs(); }
HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument()); wordToHtmlConverter.setPicturesManager( new PicturesManager() { public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches ) { return imgPath.replace("\\","/") + suggestedName; } } ); wordToHtmlConverter.processDocument(wordDocument);
List pics=wordDocument.getPicturesTable().getAllPictures(); if(pics!=null){ for(int i=0;i<pics.size();i++){ Picture pic = (Picture)pics.get(i); try { String picAbsolutePath = imgPath + pic.suggestFullFileName(); listPics.add(picAbsolutePath); pic.writeImageContent(new FileOutputStream(picAbsolutePath));
} catch (FileNotFoundException e) { e.printStackTrace(); } } } Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty(OutputKeys.METHOD, "HTML"); serializer.transform(domSource, streamResult); out.close();
String htmlContent=new String(out.toByteArray()); htmlContent=htmlContent.replaceAll("“","\"").replaceAll("”","\"") .replaceAll("—","-");
map.put("htmlContent",htmlContent); map.put("listPics",listPics);
return map; }
public static void html2Doc(String source , String target) throws Exception { POIFSFileSystem poifs = new POIFSFileSystem(); DirectoryEntry directory = poifs.getRoot(); OutputStream out = new FileOutputStream(target); try { directory.createDocument("WordDocument", new FileInputStream(source)); poifs.writeFilesystem(out); out.close(); System.out.println("success"); } catch (IOException e) { e.printStackTrace(); } }
|