Word转HTML

用poi框架技术doc2html

/**
	 * 
	 * @param file wrod文档file
	 * @param path 图片路径
	 * @throws TransformerException
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws ParserConfigurationException
	 */
 public void doc2html(File file,String path) throws TransformerException, FileNotFoundException, IOException, ParserConfigurationException {
	   ByteArrayOutputStream out = new ByteArrayOutputStream();
	   HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(file));
		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 suggestedName;
			}
		});
		wordToHtmlConverter.processDocument(wordDocument);
       //获取图片
		List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();
		Document htmlDocument = wordToHtmlConverter.getDocument();
		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());
		for (int i = 0; i < pics.size(); i++) {
			Picture pic = (Picture) pics.get(i);
			//替换图片路径
			String filename = pic.suggestFullFileName();
			String substring = filename.substring(filename.lastIndexOf(".") + 1);
			htmlContent = htmlContent.replaceAll(filename,
					"image"+"/"+"image"+ (i + 1) + "." + substring + "");
			byte[] temp = pic.getContent();
            //保存html文本
			   FileWriter htmlWriter =new  FileWriter(path+"index.html");
			   htmlWriter.write(htmlContent);
			   htmlWriter.close();
            //保存图片
			   OutputStream out1 = new FileOutputStream(path+"image\\image"+(i+1)+"."+substring);
			   out1.write(temp);
			   out1.close();
		}
   }

用docx4J框架技术 docx2html


 /**
      * 
      * @param file wrod文档file
      * @param path 图片路径
      * @throws Exception
      */
public void docx2html(File file,String path) throws Exception   {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
  WordprocessingMLPackage wordMLPackage = new WordprocessingMLPackage();
     wordMLPackage = WordprocessingMLPackage.load(file);
      // 创建html配置项
  	HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
      //保存图片
      List<String> lists = saveDocxImg(wordMLPackage,path);
  	htmlSettings.setWmlPackage(wordMLPackage);
      //配置文本图片给路径
  	htmlSettings.setImageDirPath("iamge1");
  	htmlSettings.setImageIncludeUUID(false);
  	String userCSS = "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img,  ol, ul, li, table, caption, tbody, tfoot, thead, tr, th, td "
  			+ "{ margin: 0; padding: 0; border: 0;}" + "body {line-height: 1;} ";
  	htmlSettings.setUserCSS(userCSS);
  	Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
  	//转换html
      Docx4J.toHTML(htmlSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL);
  	out.close();
  	File flie = new File(path+"\\image1");
  	File[] files = flie.listFiles();
  	String htmlContent = new String(out.toByteArray());//htm内容文本
       // 替换图片名称
  	for (int i = 0; i < files.length; i++) {
  		String substring = files[i].getName().substring(files[i].getName().lastIndexOf(".") + 1);
  		htmlContent = htmlContent.replaceAll(files[i].getName(),
  				"image1/" + "image" + (i + 1) + "." + substring);
  	}
  	 FileWriter htmlWriter =new  FileWriter(path+"index1.html");//保存html
  	   htmlWriter.write(htmlContent);
  	   htmlWriter.close();
 }
 public List<String> saveDocxImg(WordprocessingMLPackage wordMLPackage,String path) throws Exception {
  	List<String> paths = new ArrayList<>();
  	for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
  		if (entry.getValue() instanceof BinaryPartAbstractImage) {
  			BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
  			// 图片minetype
  			String imgContentType = binImg.getContentType();
  			PartName pt = binImg.getPartName();
  			String fileName = null;
  			if (pt.getName().indexOf("word/media/") != -1) {
  				fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
  			}
  			byte[] temp = ((BinaryPart) entry.getValue()).getBytes();
  			 OutputStream out1 = new FileOutputStream(path+"image1\\"+fileName);
  			   out1.write(temp);
  			   out1.close();
  		}
  	}
  	return paths;
  }

main

public static void main(String[] args) throws Exception
{
    Word2html w=new Word2html();
     // w.doc2html(new File("C:\\Users\\chenqiao\\Desktop\\阅卷\\测试v(1).doc"),"C:\\Users\\chenqiao\\Desktop\\阅卷\\html\\"); 
   w.docx2html(new File("C:\\Users\\chenqiao\\Desktop\\阅卷\\阅卷.docx"),"C:\\Users\\chenqiao\\Desktop\\阅卷\\html\\");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值