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();
|
}
|
}
|
|
}
|