public boolean doc2pdf(String srcFilePath, String pdfFilePath) {
- ActiveXComponent app = null;
- Dispatch doc = null;
- try {
- ComThread.InitSTA();
- app = new ActiveXComponent("Word.Application");
- app.setProperty("Visible", false);
- Dispatch docs = app.getProperty("Documents").toDispatch();
- doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
- new Object[] { srcFilePath,
- new Variant(false),
- new Variant(true),
- new Variant(false),
- new Variant("pwd") },
- new int[1]).toDispatch();
- Dispatch.put(doc, "RemovePersonalInformation", false);
- Dispatch.call(doc, "ExportAsFixedFormat", pdfFilePath, wdFormatPDF);
-
- return true;
- } catch (ComFailException e) {
- return false;
- } catch (Exception e) {
- return false;
- } finally {
- if (doc != null) {
- Dispatch.call(doc, "Close", false);
- }
- if (app != null) {
- app.invoke("Quit", 0);
- }
- ComThread.Release();
- }
- }
Jacob操作ppt
- public boolean ppt2pdf(String srcFilePath, String pdfFilePath) {
- ActiveXComponent app = null;
- Dispatch ppt = null;
- try {
- ComThread.InitSTA();
- app = new ActiveXComponent("PowerPoint.Application");
- Dispatch ppts = app.getProperty("Presentations").toDispatch();
-
-
- ppt = Dispatch.call(ppts, "Open", srcFilePath, true,
- true,
- false
- ).toDispatch();
-
- Dispatch.call(ppt, "SaveAs", pdfFilePath, ppSaveAsPDF);
-
- return true;
- } catch (ComFailException e) {
- return false;
- } catch (Exception e) {
- return false;
- } finally {
- if (ppt != null) {
- Dispatch.call(ppt, "Close");
- }
- if (app != null) {
- app.invoke("Quit");
- }
- ComThread.Release();
- }
- }
Jacob操作Excel
- package com;
- import java.io.ObjectInputStream.GetField;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
-
- import com.jacob.activeX.ActiveXComponent;
- import com.jacob.com.ComThread;
- import com.jacob.com.Dispatch;
- import com.jacob.com.Variant;
-
-
- public class ready {
- private static ActiveXComponent xl = null;
- private static Dispatch workbooks = null;
- private Dispatch workbook = null;
- private Dispatch sheets = null;
- private Dispatch currentSheet = null;
-
- private void OpenExcel(String filepath, boolean visible) {
- try {
- initComponents();
- ComThread.InitSTA();
- if(xl==null)
- xl = new ActiveXComponent("Excel.Application");
- xl.setProperty("Visible", new Variant(visible));
- if(workbooks==null)
- workbooks = xl.getProperty("Workbooks").toDispatch();
- workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method,
- new Object[] { srcFilePath,
- new Variant(false),
- new Variant(true),
- "1",
- "pwd" },
- new int[1]).toDispatch();
- } catch (Exception e) {
- e.printStackTrace();
- releaseSource();
- }
- }
-
- private void SaveAs(String filePath){
- Dispatch.call(workbook, "SaveAs",filePath);
- }
-
- private void CloseExcel(boolean f) {
- try {
- Dispatch.call(workbook, "Save");
- Dispatch.call(workbook, "Close", new Variant(f));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- releaseSource();
- }
- }
-
- private void initComponents(){
- workbook = null;
- currentSheet = null;
- sheets = null;
- }
-
- private static void releaseSource(){
- if(xl!=null){
- xl.invoke("Quit", new Variant[] {});
- xl = null;
- }
- workbooks = null;
- ComThread.Release();
- System.gc();
- }
-
- private Dispatch getCurrentSheet() {
- currentSheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
- return currentSheet;
- }
-
- private void modifyCurrentSheetName(String newName) {
- Dispatch.put(getCurrentSheet(), "name", newName);
- }
-
-
- private String getCurrentSheetName(Dispatch sheets) {
- return Dispatch.get(sheets, "name").toString();
- }
-
- private Dispatch getSheetByName(String name) {
- return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[]{name}, new int[1]).toDispatch();
- }
-
- private Dispatch getSheets() {
- if(sheets==null)
- sheets = Dispatch.get(workbook, "sheets").toDispatch();
- return sheets;
- }
-
- private Dispatch getSheetByIndex(Integer index) {
- return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[]{index}, new int[1]).toDispatch();
- }
-
-
- private int getSheetCount() {
- int count = Dispatch.get(getSheets(), "count").toInt();
- return count;
- }
-
- public void setBlackGroudPrituce(String filepath)
- {
- int num=this.getSheetCount();
- for (int i = 1; i <= num; i++) {
- Dispatch sheets=this.getSheetByIndex(i);
- Dispatch.call(sheets,"SetBackgroundPicture",filepath);
- }
- }
-
- public void addSheet(String name) {
- currentSheet=Dispatch.get(Dispatch.get(workbook, "sheets").toDispatch(), "add").toDispatch();
-
- Dispatch.put(currentSheet, "Visible", new Boolean(false));
- System.out.println("插入信息为:"+name);
- }
-
- private String getWorkbookName() {
- if(workbook==null)
- return null;
- return Dispatch.get(workbook, "name").toString();
- }
-
- public List findSheetName()
- {
- int num=this.getSheetCount();
- List list=new ArrayList();
- for (int i = 1; i <= num; i++) {
- currentSheet=this.getSheetByIndex(i);
- list.add(this.getCurrentSheetName(currentSheet));
- }
- return list;
- }
-
- private void setFooter(String foot) {
- currentSheet=this.getCurrentSheet();
- Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
- Dispatch.put(PageSetup,"CenterFooter",foot);
- }
-
- private String getFooter() {
- currentSheet=this.getCurrentSheet();
- Dispatch PageSetup=Dispatch.get(currentSheet,"PageSetup").toDispatch();
- return Dispatch.get(PageSetup,"CenterFooter").toString();
- }
-
- private void setPassword() {
- Dispatch.call(workbook, "Protect",123,true,false);
- }
-
- public void setName(String name,String place,String comment) {
- Dispatch Names=Dispatch.get(workbook, "Names").toDispatch();
- Dispatch.call(Names,"Add",name,place,false).toDispatch();
- Dispatch.put(Names, "Comment", comment);
- }
-
- public String getName(String name) {
- Dispatch Names=Dispatch.get(workbook, "Names").toDispatch();
- Dispatch Name=Dispatch.call(Names,"Item",name).toDispatch();
- return Dispatch.get(Name, "Value").toString();
- }
-
- private void setValue(String position, Object value) {
- currentSheet=this.getCurrentSheet();
- Dispatch cell = Dispatch.invoke(currentSheet, "Range",
- Dispatch.Get, new Object[] { position }, new int[1])
- .toDispatch();
- Dispatch.put(cell, "Value", value);
- String color=this.getColor(cell);
- this.setFont(cell,color);
- }
-
- private void setFont(Dispatch cell,String color)
- {
- Dispatch font=Dispatch.get(cell, "Font").toDispatch();
-
- Dispatch.put(font,"size", "1");
- Dispatch.put(font,"color",color);
- }
-
- private String getColor(Dispatch cell)
- {
- Dispatch Interior=Dispatch.get(cell, "Interior").toDispatch();
- String color=Dispatch.get(Interior, "color").toString();
- return color;
- }
-
- private Variant getValue(String position) {
- currentSheet=this.getCurrentSheet();
- Dispatch cell = Dispatch.invoke(currentSheet, "Range", Dispatch.Get,
- new Object[] { position }, new int[1]).toDispatch();
- Variant value = Dispatch.get(cell, "Value");
- return value;
- }
-
- private int getRowCount() {
- currentSheet=this.getCurrentSheet();
- Dispatch UsedRange=Dispatch.get(currentSheet, "UsedRange").toDispatch();
- Dispatch rows=Dispatch.get(UsedRange, "Rows").toDispatch();
- int num=Dispatch.get(rows, "count").getInt();
- return num;
- }
-
- private int getColumnCount() {
- currentSheet=this.getCurrentSheet();
- Dispatch UsedRange=Dispatch.get(currentSheet, "UsedRange").toDispatch();
- Dispatch Columns=Dispatch.get(UsedRange, "Columns").toDispatch();
- int num=Dispatch.get(Columns, "count").getInt();
- return num;
- }
-
- private String getCellPosition(int rnum,int cnum)
- {
- String cposition="";
- if(cnum>26)
- {
- int multiple=(cnum)/26;
- int remainder=(cnum)%26;
- char mchar=(char)(multiple+64);
- char rchar=(char)(remainder+64);
- cposition=mchar+""+rchar;
- }
- else
- {
- cposition=(char)(cnum+64)+"";
- }
- cposition+=rnum;
- return cposition;
- }
-
-
- private viod setCheckCompatibility(){
- Dispatch.put(wookbook, "CheckCompatibility", false);
- }
-
-
- private void setPrintArea(){
- int count = Dispatch.get(sheets, "count").changeType(Variant.VariantInt).getInt();
- for (int i = count; i >= 1; i--) {
- sheet = Dispatch.invoke(sheets, "Item",
- Dispatch.Get, new Object[] { i }, new int[1]).toDispatch();
- Dispatch page = Dispatch.get(sheet, "PageSetup").toDispatch();
- Dispatch.put(page, "PrintArea", false);
- Dispatch.put(page, "Orientation", 2);
- Dispatch.put(page, "Zoom", false);
- Dispatch.put(page, "FitToPagesTall", false);
- Dispatch.put(page, "FitToPagesWide", 1);
- }
- }
- }
Jacob操作office文档(Word,PPT,Excel)
原文:http://www.cnblogs.com/jeecg/p/4387779.html