java实现在线预览----poi操作word转html及03、07版本兼容问题


实现页面预览word文档,用的是poi3.9,以下代码支持doc和docx。

 /**
	  * word07版本(.docx)转html
	  * poi:word07在线预览
	  * */
	 public static void PoiWord07ToHtml (HttpServletRequest request) throws IOException{
		 
		  String path= "C:\\Users\\Administrator\\Desktop\\";
		 String file = "C:\\Users\\Administrator\\Desktop\\word07.docx";
		  String file2 ="C:\\Users\\Administrator\\Desktop\\word07.html";
		 File f = new File(file);  
		  if (!f.exists()) {  
	            System.out.println("Sorry File does not Exists!");  
	        } else {  
	            if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {  
	                //读取文档内容  
	                InputStream in = new FileInputStream(f);  
	                XWPFDocument document = new XWPFDocument(in);  
	                
	                File imageFolderFile = new File(path);  
	                //加载html页面时图片路径
	                XHTMLOptions options = XHTMLOptions.create().URIResolver( new BasicURIResolver("./")); 
	                //图片保存文件夹路径
	                options.setExtractor(new FileImageExtractor(imageFolderFile));  
	                OutputStream out = new FileOutputStream(new File(file2));  
	                XHTMLConverter.getInstance().convert(document, out, options);  
	                out.close();  
	            } else {  
	                System.out.println("Enter only MS Office 2007+ files");  
	            }  
	        }   
	        
	 }
	 /**
	  * word03版本(.doc)转html
	  * poi:word03在线预览
	  * */
	 public static void PoiWord03ToHtml() throws IOException, ParserConfigurationException, TransformerException{
		 final String path = "C:\\Users\\Administrator\\Desktop\\";
		  final String file = "C:\\Users\\Administrator\\Desktop\\word03.doc";
		  InputStream input = new FileInputStream(file);
		  HWPFDocument wordDocument = new HWPFDocument(input);
		  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) {     //图片在html页面加载路径
		    return "image\\"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 {//图片保存在文件夹的路径
		     pic.writeImageContent(new FileOutputStream(path
		       + pic.suggestFullFileName()));
		    } catch (FileNotFoundException e) {
		     e.printStackTrace();
		    }
		   }
		  }
		  //创建html页面并将文档中内容写入页面
		  Document htmlDocument = wordToHtmlConverter.getDocument();
		  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		  DOMSource domSource = new DOMSource(htmlDocument);
		  StreamResult streamResult = new StreamResult(outStream);
		  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);
		  outStream.close();
		  String content = new String(outStream.toString("UTF-8"));
		  FileUtils.writeStringToFile(new File(path, "word03.html"), content, "utf-8");
	 
	 }


最后附上结果图,因为我的是保存在页面做的测试,所以各位小伙伴只要修改路径写一个测试类调用这个方法,就可以完成啦!!!若对你有帮助,不胜荣幸大笑



发布了15 篇原创文章 · 获赞 23 · 访问量 10万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览