主要參考的是http://www.cnblogs.com/luckyxiaoxuan/archive/2012/06/13/2548510.html
轉(zhuǎn)word的代碼如下:
public void word2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "Word.Application");// 啟動(dòng)word
app.put("Visible", false); // 設(shè)置word不可見
IDispatch docs = (IDispatch) app.get("Documents"); // 獲得word中所有打開的文檔
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打開文檔
doc.method("SaveAs", new Object[] { pdfFile, 17 });// 轉(zhuǎn)換文檔為pdf格式
doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
參考文獻(xiàn):
http://msdn.microsoft.com/en-us/library/office/ff198122%28v=office.15%29.aspx
http://msdn.microsoft.com/en-us/library/office/bb241296%28v=office.12%29.aspx
使用ExportAsFixedFormat方法而不是SaveAs方法,Excel的SaveAs方法不支持pdf。
代碼如下:
package main;
import java.io.File;
import javax.xml.ws.Dispatch;
import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelRange;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheets;
public class JComConvertor {
/**
* JCom調(diào)用MS Office轉(zhuǎn)換word為PDF
*
* @param inputFile
* doc文檔的絕對(duì)路徑
* @param pdfFile
* 輸出pdf文檔的絕對(duì)路徑,例如D:\\folder\\test.pdf
*/
public void word2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "Word.Application");// 啟動(dòng)word
app.put("Visible", false); // 設(shè)置word不可見
IDispatch docs = (IDispatch) app.get("Documents"); // 獲得word中所有打開的文檔
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打開文檔
doc.method("SaveAs", new Object[] { pdfFile, 17 });// 轉(zhuǎn)換文檔為pdf格式
doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom調(diào)用MS Office轉(zhuǎn)換excel為HTML
*
* @param inputFile
* 源文件絕對(duì)路徑
* @param htmlFile
* 目標(biāo)文件絕對(duì)路徑
*/
public void excel2HTML(String inputFile, String htmlFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
ExcelApplication ex = new ExcelApplication(rm);
ex.put("Visible", false);
IDispatch excs = (IDispatch) ex.get("Workbooks");
IDispatch doc = (IDispatch) excs.method("Open", new Object[] {
inputFile, false, true });// 打開文檔
doc.method("SaveAs", new Object[] { htmlFile, 44 });
doc.method("Close", new Object[] { false });
ex.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom調(diào)用MS Office轉(zhuǎn)換Excel為PDF
*
* @param inputFile
* 源文件絕對(duì)路徑
* @param htmlFile
* 目標(biāo)文件絕對(duì)路徑
*/
public void excel2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
ExcelApplication ex = new ExcelApplication(rm);
ex.put("Visible", false);
IDispatch excs = (IDispatch) ex.get("Workbooks");
IDispatch doc = (IDispatch) excs.method("Open", new Object[] {
inputFile, false, true });// 打開文檔
doc.method("ExportAsFixedFormat", new Object[] { 0, pdfFile });
doc.method("Close", new Object[] { false });
ex.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom調(diào)用MS Office轉(zhuǎn)換Powerpoint為PDF
*
* @param inputFile
* 源文件絕對(duì)路徑
* @param pdfFile
* 目標(biāo)文件絕對(duì)路徑
*/
public void powerpoint2PDF(String inputFile, String pdfFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "PowerPoint.Application");// 啟動(dòng)word
// app.put("Visible", false); // 設(shè)置word不可見
IDispatch docs = (IDispatch) app.get("Presentations"); // 獲得word中所有打開的文檔
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打開文檔
doc.method("SaveAs", new Object[] { pdfFile, 32 });// 轉(zhuǎn)換文檔為pdf格式
// doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* JCom調(diào)用MS Office轉(zhuǎn)換Powerpoint為JPG
*
* @param inputFile
* @param pdfFile
*/
public void powerpoint2JPG(String inputFile, String jpgFile) {
ReleaseManager rm = null;
IDispatch app = null;
try {
rm = new ReleaseManager();
app = new IDispatch(rm, "PowerPoint.Application");// 啟動(dòng)word
// app.put("Visible", false); // 設(shè)置不可見
IDispatch docs = (IDispatch) app.get("Presentations"); // 獲得word中所有打開的文檔
IDispatch doc = (IDispatch) docs.method("Open", new Object[] {
inputFile, false, true });// 打開文檔
doc.method("SaveAs", new Object[] { jpgFile, 17 });// 轉(zhuǎn)換文檔為pdf格式
// doc.method("Close", new Object[] { false });
app.method("Quit", null);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
app = null;
rm.release();
rm = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}