OFFICE(word/ppt/excel)를 HTML로 변환

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.*;

public class OfficeToXML {

	private final static OfficeToXML oOfficeToXML = new OfficeToXML();

	public static OfficeToXML getInstance() {
		return oOfficeToXML;
	}

	public OfficeToXML() {
	}

	public boolean WordtoHtml(String s, String s1) {
		ComThread.InitSTA();
		ActiveXComponent activexcomponent = new ActiveXComponent(
				"Word.Application");
		String s2 = s;
		String s3 = s1;
		boolean flag = false;
		try {
			activexcomponent.setProperty("Visible", new Variant(false));
			Dispatch dispatch = activexcomponent.getProperty("Documents").toDispatch();
			Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1,
					new Object[] { s2, new Variant(false), new Variant(true) },
					new int[1]).toDispatch();
			Dispatch.invoke(dispatch1, "SaveAs", 1, new Object[] { s3,
					new Variant(8) }, new int[1]);
			Variant variant = new Variant(false);
			Dispatch.call(dispatch1, "Close", variant);
			flag = true;
		} catch (Exception exception) {
			exception.printStackTrace();
		} finally {
			activexcomponent.invoke("Quit", new Variant[0]);
			ComThread.Release();
			ComThread.quitMainSTA();
		}
		return flag;
	}

	public boolean PPttoHtml(String s, String s1) {
		ComThread.InitSTA();
		ActiveXComponent activexcomponent = new ActiveXComponent(
				"PowerPoint.Application");
		String s2 = s;
		String s3 = s1;
		boolean flag = false;
		try {
			Dispatch dispatch = activexcomponent.getProperty("Presentations")
					.toDispatch();
			Dispatch dispatch1 = Dispatch.call(dispatch, "Open", s2,
					new Variant(-1), new Variant(-1), new Variant(0))
					.toDispatch();
			Dispatch.call(dispatch1, "SaveAs", s3, new Variant(12));
			Variant variant = new Variant(-1);
			Dispatch.call(dispatch1, "Close");
			flag = true;
		} catch (Exception exception) {
			System.out.println("|||" + exception.toString());
		} finally {
			activexcomponent.invoke("Quit", new Variant[0]);
			ComThread.Release();
			ComThread.quitMainSTA();
		}
		return flag;
	}

	public boolean ExceltoHtml(String s, String s1) {
		 ComThread.InitSTA();
		 ActiveXComponent activexcomponent = new
		 ActiveXComponent("Excel.Application");
		 String s2 = s;
		 String s3 = s1;
		 boolean flag = false;
		 try
		 {
		 activexcomponent.setProperty("Visible", new Variant(false));
		 Dispatch dispatch =
		 activexcomponent.getProperty("Workbooks").toDispatch();
		 Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1, new
		 Object[] {
		 s2, new Variant(false), new Variant(true)
		 }, new int[1]).toDispatch();
		 Dispatch.call(dispatch1, "SaveAs", s3, new Variant(44));
		 Variant variant = new Variant(false);
		 Dispatch.call(dispatch1, "Close", variant);
		 flag = true;
		 }
		 catch(Exception exception)
		 {
		 System.out.println("|||" + exception.toString());
		 }
		 finally
		 {
		 activexcomponent.invoke("Quit", new Variant[0]);
		 ComThread.Release();
		 ComThread.quitMainSTA();
		 }
		 return flag;
	}

	public static void main(String args[]) {
		OfficeToXML otx = OfficeToXML.getInstance();
		boolean flag1 = otx.PPttoHtml("e:/test/test3.pptx", "e:/test/test3.html");
		if(flag1){
			System.out.println("PPT HTML !");
		}else{
			System.out.println("PPT HTML !");
		}
		boolean flag2 = otx.WordtoHtml("e:/test/test2.docx", "e:/test/test2.html");
		if(flag2){
			System.out.println("WORD HTML !");
		}else{
			System.out.println("WORD HTML !");
		}
		boolean flag3 = otx.ExceltoHtml("e:/test/test1.xlsx", "e:/test/test1.html");
		if(flag3){
			System.out.println("EXCEL HTML !");
		}else{
			System.out.println("EXCEL HTML !");
		}
	}
}

 
실행 조건:
1.JDK1.6
2.jacob.jar와 jacob.dll
1) 제이콥을.dll은...\Java\jdk1.6.0_10\bin、..\Java\jdk1.6.0_10\jre\bin, C:\WINDOWS\system32 디렉터리에 각각 1부씩 넣음) jacob.jar는 프로젝트의lib 패키지에 넣고 "java 구축 경로"에서도 이jar 패키지를 불러옵니다.3) 프로젝트를 실행하면 컴파일하여 통과할 수 있다.
주: 제이콥.jar와 jacob.dll버전은 반드시 jdk버전과 일치해야 합니다. 그렇지 않으면 결과는 자부합니다!
 
 
프로그래밍에서 즐거움을 찾고, 즐거움에서 자유롭게 프로그래밍!

좋은 웹페이지 즐겨찾기