package com.qxueyou.scc.base.util;
|
|
import com.jacob.activeX.ActiveXComponent;
|
import com.jacob.com.ComThread;
|
import com.jacob.com.Dispatch;
|
import com.jacob.com.Variant;
|
|
public class JacobUtil {
|
|
public static final String DOC = "doc";
|
public static final String DOCX = "docx";
|
public static final String PDF = "pdf";
|
public static final String XLS = "xls";
|
public static final String XLSX = "xlsx";
|
public static final String MP4 = "mp4";
|
public static final String PPT = "ppt";
|
public static final String PPTX = "pptx";
|
|
// 8 ´ú±íword±£´æ³Éhtml
|
public static final int WORD2HTML = 8;
|
// 17´ú±íword±£´æ³Épdf
|
public static final int WD2PDF = 17;
|
public static final int PPT2PDF = 32;
|
public static final int XLS2PDF = 0;
|
|
public static void main(String[] args) {
|
String pptfile = "D:/upload/ÐÂÔ±¹¤ÈëÖ°Ïòµ¼¼°ÐÅÏ¢201605.xlsx";
|
//String pdffile = "D:/upload/b.pdf";
|
String htmlfile="D:/upload/excel.pdf";
|
//ppt2pdf(pptfile,pdffile);
|
excel2pdf(pptfile,htmlfile);
|
}
|
|
/**
|
* @param resourceType ×ÊÔ´ÀàÐÍ
|
* @param path ×ÊԴ·¾¶
|
* @return
|
* TODO Îļþת»»
|
*/
|
public static Integer formatConvert(String resourceType, String resourcePath) {
|
Integer pages = 0;
|
String resource = resourcePath.substring(0, resourcePath.lastIndexOf("."));
|
if(resourceType.equalsIgnoreCase(DOC)||resourceType.equalsIgnoreCase(DOCX)){
|
//wordת³ÉpdfºÍͼ??
|
word2pdf(resourcePath, resource+".pdf");
|
}else if(resourceType.equalsIgnoreCase(XLS)||resourceType.equalsIgnoreCase(XLSX)){
|
//excelÎļþת³ÉͼƬ
|
excel2pdf(resourcePath, resource+".pdf");
|
}else if(resourceType.equalsIgnoreCase(PPT)||resourceType.equalsIgnoreCase(PPTX)){
|
ppt2pdf(resourcePath, resource+".pdf");
|
}else if(resourceType.equalsIgnoreCase(MP4)){
|
//ÊÓÆµÎļþ²»×ª??
|
pages = 0;
|
}
|
return pages;
|
}
|
|
|
public static void word2pdf(String docfile, String pdffile)
|
{
|
// Æô¶¯wordÓ¦ÓóÌÐò(Microsoft Office Word 2003)
|
ActiveXComponent app = null;
|
try{
|
ComThread.InitSTA();
|
app = new ActiveXComponent("Word.Application");
|
app.setProperty("Visible", false);
|
System.out.println("*****ÕýÔÚת»»...*****");
|
// ÉèÖÃwordÓ¦ÓóÌÐò²»¿É??
|
// app.setProperty("Visible", new Variant(false));
|
// documents±íʾword³ÌÐòµÄËùÓÐÎĵµ´°¿Ú£¬£¨wordÊǶàÎĵµÓ¦ÓóÌÐò???
|
Dispatch docs = app.getProperty("Documents").toDispatch();
|
// ´ò¿ªÒª×ª»»µÄwordÎļþ
|
/* Dispatch doc = Dispatch.invoke(
|
docs,
|
"Open",
|
Dispatch.Method,
|
new Object[] { docfile, new Variant(false),
|
new Variant(true) }, new int[1]).toDispatch(); */
|
|
Dispatch doc = Dispatch.call(
|
docs,
|
"Open",
|
docfile,
|
false,
|
true).toDispatch();
|
// µ÷ÓÃDocument¶ÔÏóµÄsaveAs·½·¨,½«Îĵµ±£´æÎªpdf¸ñʽ
|
/*Dispatch.invoke(doc, "ExportAsFixedFormat", Dispatch.Method, new Object[] {
|
pdffile, new Variant(wdFormatPDF) }, new int[1]);*/
|
|
Dispatch.call(doc, "ExportAsFixedFormat", pdffile, WD2PDF);
|
// ¹Ø±ÕwordÎļþ
|
Dispatch.call(doc, "Close", false);
|
}
|
catch (Exception e)
|
{ ComThread.Release();
|
e.printStackTrace();
|
}
|
finally
|
{
|
//¹Ø±ÕwordÓ¦ÓóÌÐò
|
app.invoke("Quit", 0);
|
ComThread.Release();
|
}
|
System.out.println("*****ת»»Íê±Ï********");
|
}
|
|
|
|
public static void ppt2pdf(String pptfile,String pdffile){
|
ActiveXComponent app = null;
|
//app.setProperty("Visible", new Variant(false));
|
try {
|
ComThread.InitSTA();
|
app = new ActiveXComponent("PowerPoint.Application");
|
Dispatch files = app.getProperty("Presentations").toDispatch();
|
Dispatch file = Dispatch.call(files, "open", pptfile, false, true).toDispatch();
|
Dispatch.call(file, "SaveAs", pdffile, PPT2PDF);
|
Dispatch.call(file,"Close");
|
} catch (Exception e) {
|
ComThread.Release();
|
e.printStackTrace();
|
//throw e;
|
}finally{
|
app.invoke("Quit");
|
ComThread.Release();
|
}
|
}
|
|
public static void excel2pdf(String excelfile,String pdffile){
|
ActiveXComponent app = null;
|
try{
|
ComThread.InitSTA(true);
|
app = new ActiveXComponent("Excel.Application");
|
app.setProperty("Visible", false);
|
app.setProperty("AutomationSecurity", new Variant(3));//½ûÓÃ??
|
Dispatch excels = app.getProperty("Workbooks").toDispatch();
|
/*Dispatch excel = Dispatch.invoke(excels, "Open", Dispatch.Method, new Object[]{
|
excelfile,
|
new Variant(false),
|
new Variant(false),
|
},new int[9]).toDispatch();*/
|
Dispatch excel = Dispatch.call(excels, "Open",
|
excelfile,false,true).toDispatch();
|
//ת»»¸ñʽExportAsFixedFormat
|
/*Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method, new Object[]{
|
new Variant(0),//pdf¸ñʽ=0
|
pdffile,
|
new Variant(0)//0=±ê×¼(Éú³ÉµÄpdfͼƬ²»»á±äÄ£??) 1=??СÎÄ??(Éú³ÉµÄpdfͼƬģºýµÄÒ»Ëúºý??)
|
}, new int[1]);*/
|
Dispatch.call(excel, "ExportAsFixedFormat",XLS2PDF,
|
pdffile);
|
Dispatch.call(excel, "Close", false);
|
if(app!=null){
|
app.invoke("Quit");
|
app=null;
|
}
|
}catch(Exception e){
|
ComThread.Release();
|
e.printStackTrace();
|
}finally{
|
ComThread.Release();
|
}
|
}
|
|
}
|