Jacob操作office文档

2019-01-26 14:42

public boolean doc2pdf(String srcFilePath, String pdfFilePath) { ActiveXComponent app = null; Dispatch doc = null; try {

ComThread.InitSTA();

app = new ActiveXComponent(\ app.setProperty(\

Dispatch docs = app.getProperty(\ doc = Dispatch.invoke(docs, \ new Object[] { srcFilePath,

new Variant(false),

new Variant(true),//是否只读 new Variant(false), new Variant(\ new int[1]).toDispatch();

// Dispatch.put(doc, \ //兼容性检查,为特定值false不正确

Dispatch.put(doc, \

Dispatch.call(doc, \pdfFilePath, wdFormatPDF); // word保存为pdf格式宏,值为17

return true; // set flag true; } catch (ComFailException e) { return false; } catch (Exception e) { return false; } finally {

if (doc != null) {

Dispatch.call(doc, \ }

if (app != null) {

app.invoke(\ }

ComThread.Release(); } }

Jacob操作ppt

public boolean ppt2pdf(String srcFilePath, String pdfFilePath) { ActiveXComponent app = null; Dispatch ppt = null; try {

ComThread.InitSTA();

app = new ActiveXComponent(\

Dispatch ppts = app.getProperty(\

// 因POWER.EXE的发布规则为同步,所以设置为同步发布 ppt = Dispatch.call(ppts, \ true,// Untitled指定文件是否有标题 false// WithWindow指定文件是否可见 ).toDispatch();

Dispatch.call(ppt, \pdfFilePath, ppSaveAsPDF); //ppSaveAsPDF为特定值32

return true; // set flag true; } catch (ComFailException e) { return false; } catch (Exception e) { return false; } finally {

if (ppt != null) {

Dispatch.call(ppt, \ }

if (app != null) {

app.invoke(\ }

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; //Excel对象(防止打开多个) private static Dispatch workbooks = null; //工作簿对象 private Dispatch workbook = null; //具体工作簿

private Dispatch sheets = null;// 获得sheets集合对象 private Dispatch currentSheet = null;// 当前sheet /**

* 打开excel文件

* @param filepath 文件路径名称 * @param visible 是否显示打开

* @param readonly 是否只读方式打开 */

private void OpenExcel(String filepath, boolean visible) { try {

initComponents(); //清空原始变量 ComThread.InitSTA(); if(xl==null)

xl = new ActiveXComponent(\对象

xl.setProperty(\设置是否显示打开excel if(workbooks==null)

workbooks = xl.getProperty(\//打开具体工作簿

workbook = Dispatch.invoke(workbooks, \ new Object[] { srcFilePath,

new Variant(false), // 是否以只读方式打开

new Variant(true), \

\}, //输入密码\若有密码则进行匹配,无则直接打开

new int[1]).toDispatch(); } catch (Exception e) { e.printStackTrace(); releaseSource(); } } /**

* 工作簿另存为

* @param filePath 另存为的路径 * 例如 SaveAs=\ */

private void SaveAs(String filePath){

Dispatch.call(workbook, \ } /**

* 关闭excel文档

* @param f 含义不明 (关闭是否保存?默认false) */

private void CloseExcel(boolean f) { try {

Dispatch.call(workbook, \

Dispatch.call(workbook, \ } 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(\ xl = null; }

workbooks = null; ComThread.Release(); System.gc(); } /**

* 得到当前sheet * @return */

private Dispatch getCurrentSheet() {

currentSheet = Dispatch.get(workbook, \ return currentSheet; } /**

* 修改当前工作表的名字 * @param newName

*/

private void modifyCurrentSheetName(String newName) { Dispatch.put(getCurrentSheet(), \ }

/**

* 得到当前工作表的名字 * @return */

private String getCurrentSheetName(Dispatch sheets) { return Dispatch.get(sheets, \ } /**

* 通过工作表名字得到工作表 * @param name sheetName * @return */

private Dispatch getSheetByName(String name) {

return Dispatch.invoke(getSheets(), \Dispatch.Get, new Object[]{name}, new int[1]).toDispatch(); } /**

* 得到sheets的集合对象 * @return */

private Dispatch getSheets() { if(sheets==null)

sheets = Dispatch.get(workbook, \ return sheets; } /**

* 通过工作表索引得到工作表(第一个工作簿index为1) * @param index

* @return sheet对象 */

private Dispatch getSheetByIndex(Integer index) {

return Dispatch.invoke(getSheets(), \Dispatch.Get, new Object[]{index}, new int[1]).toDispatch(); }

/**

* 得到sheet的总数 * @return */


Jacob操作office文档.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:上海自贸区的建立对加工贸易企业的影响

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: