POI Word DOC格式转Html

package com.office;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.w3c.dom.Document;

public class OfficeConvert {

	public static String GetFileExt(File f) {
		String ext = null;
		String s = f.getName();
		int i = s.lastIndexOf('.');
		if (i > 0 && i < s.length() - 1) {
			ext = s.substring(i + 1).toLowerCase();
		}
		return ext;
	}

	public static boolean ConvertToHtml(final String input, final String output) {
		File file = new File(input);

		if (!file.exists())
			return false;
		String ext = GetFileExt(file);
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {

			if (ext.equals("doc")) {
				fileInputStream = new FileInputStream(input);
				HWPFDocument hwpfDocument = new HWPFDocument(fileInputStream);
				Document document = DocumentBuilderFactory.newInstance()
						.newDocumentBuilder().newDocument();
				WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
						document);

				File imageDir = new File(output + ".img");
				final String suggestDirName = imageDir.getName();
				if (!imageDir.isDirectory()) {
					imageDir.mkdirs();
				}
				
				//save image
				wordToHtmlConverter.setPicturesManager(new PicturesManager() {
					@Override
					public String savePicture(byte[] contet,
							PictureType pictureType, String suggestedName,
							float widthInches, float heightInches) {
						String imgagePath = output + ".img/" + suggestedName;
						File file = new File(imgagePath);
						FileOutputStream fos = null;
						try {
							fos = new FileOutputStream(file);
							fos.write(contet);
							fos.close();
						} catch (Exception e) {
							e.printStackTrace();
						}
						return suggestDirName + "/" + suggestedName;
					}
				});

				wordToHtmlConverter.processDocument(hwpfDocument);
				Transformer transformer = TransformerFactory.newInstance()
						.newTransformer();
				transformer.setOutputProperty(OutputKeys.INDENT, "yes");
				transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
				transformer.setOutputProperty(OutputKeys.METHOD, "html");
				fileOutputStream = new FileOutputStream(output);
				transformer.transform(
						new DOMSource(wordToHtmlConverter.getDocument()),
						new StreamResult(new OutputStreamWriter(fileOutputStream, "UTF-8")));

				return true;

			} else if (ext.equals("docx")){
				
			} else if (ext.equals("ppt")) {

			} else if (ext.equals("pptx")) {

			} else if (ext.equals("xls")) {

			} else if (ext.equals("xlsx")) {

			}

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			try {
				if (null != fileOutputStream)
					fileOutputStream.close();
				if (null != fileInputStream)
					fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		return false;

	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值