Jacob操作office文档(Word,PPT,Excel)
jacob 操作 word
- 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, "Compatibility", false); //兼容性检查,为特定值false不正确
- Dispatch.put(doc, "RemovePersonalInformation", false);
- Dispatch.call(doc, "ExportAsFixedFormat", 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, "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();
- // 因POWER.EXE的发布规则为同步,所以设置为同步发布
- ppt = Dispatch.call(ppts, "Open", srcFilePath, true,// ReadOnly
- true,// Untitled指定文件是否有标题
- false// WithWindow指定文件是否可见
- ).toDispatch();
- Dispatch.call(ppt, "SaveAs", 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, "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; //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("Excel.Application"); //Excel对象
- xl.setProperty("Visible", new Variant(visible));//设置是否显示打开excel
- 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" }, //输入密码"pwd",若有密码则进行匹配,无则直接打开
- new int[1]).toDispatch();
- } catch (Exception e) {
- e.printStackTrace();
- releaseSource();
- }
- }
- /**
- * 工作簿另存为
- * @param filePath 另存为的路径
- * 例如 SaveAs="D:TEST/c.xlsx"
- */
- private void SaveAs(String filePath){
- Dispatch.call(workbook, "SaveAs",filePath);
- }
- /**
- * 关闭excel文档
- * @param f 含义不明 (关闭是否保存?默认false)
- */
- 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();
- }
- /**
- * 得到当前sheet
- * @return
- */
- private Dispatch getCurrentSheet() {
- currentSheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
- return currentSheet;
- }
- /**
- * 修改当前工作表的名字
- * @param newName
- */
- private void modifyCurrentSheetName(String newName) {
- Dispatch.put(getCurrentSheet(), "name", newName);
- }
- /**
- * 得到当前工作表的名字
- * @return
- */
- private String getCurrentSheetName(Dispatch sheets) {
- return Dispatch.get(sheets, "name").toString();
- }
- /**
- * 通过工作表名字得到工作表
- * @param name sheetName
- * @return
- */
- private Dispatch getSheetByName(String name) {
- return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[]{name}, new int[1]).toDispatch();
- }
- /**
- * 得到sheets的集合对象
- * @return
- */
- private Dispatch getSheets() {
- if(sheets==null)
- sheets = Dispatch.get(workbook, "sheets").toDispatch();
- return sheets;
- }
- /**
- * 通过工作表索引得到工作表(第一个工作簿index为1)
- * @param index
- * @return sheet对象
- */
- private Dispatch getSheetByIndex(Integer index) {
- return Dispatch.invoke(getSheets(), "Item", Dispatch.Get, new Object[]{index}, new int[1]).toDispatch();
- }
- /**
- * 得到sheet的总数
- * @return
- */
- private int getSheetCount() {
- int count = Dispatch.get(getSheets(), "count").toInt();
- return count;
- }
- /**
- * 给所有的sheet添加背景
- * @param filepath 图片路径
- */
- 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);
- }
- }
- /**
- * 添加新的工作表(sheet),并且隐藏(添加后为默认为当前激活的工作表)
- */
- public void addSheet(String name) {
- // for (int i = 1; i <= this.getSheetCount(); i++) {
- // Dispatch sheets=this.getSheetByIndex(i);
- // if(name.equals(this.getCurrentSheetName(sheets)))
- // {
- // return false;
- // }
- // }
- currentSheet=Dispatch.get(Dispatch.get(workbook, "sheets").toDispatch(), "add").toDispatch();
- // Dispatch.put(currentSheet,"Name",name);
- Dispatch.put(currentSheet, "Visible", new Boolean(false));
- System.out.println("插入信息为:"+name);
- }
- /**
- * 得到工作薄的名字
- * @return
- */
- 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);
- }
- /**
- * 设置名称管理器
- * @param name 名称管理器名 不能以数字或者下划线开头,中间不能包含空格和其他无效字符
- * @param comment 备注
- * @param place 备注位置
- * @return
- */
- 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); //插入备注
- }
- /**
- * 获取名称管理器
- * @param name 名称管理器名
- * @return
- */
- 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();
- }
- /**
- * 单元格写入值
- * @param sheet 被操作的sheet
- * @param position 单元格位置,如:C1
- * @param type 值的属性 如:value
- * @param value
- */
- 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,"FontStyle", "Bold Italic");
- 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;
- }
- /**
- * 单元格读取值
- * @param position 单元格位置,如: C1
- * @param sheet
- * @return
- */
- 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;
- }
- /**
- * 获取最大行数
- * @return
- */
- 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;
- }
- /**
- * 获取最大列数
- * @return
- */
- 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;
- }
- /**
- * 获取位置
- * @param rnum 最大行数
- * @param cnum 最大列数
- */
- 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); //值为100或false
- Dispatch.put(page, "FitToPagesTall", false); //所有行为一页
- Dispatch.put(page, "FitToPagesWide", 1); //所有列为一页(1或false)
- }
- }
- }
相关推荐
1.如何将word文档转成html 2.ChangeWordToHtml
Jacob Word excel 转PDF 合并PDF文件,需要安装微软的转换插件,压缩包内含插件
java 使用jacob Word转PDF 完美格式,包内含有 jacob.jar包,office提供的SaveAsPDFandXPS.exe插件,以及源码、和步骤操作说明书
JacobWord2010-03-31.rar
Jacob word 转 PDF 导出/打印,要求电脑安装10版以上office,不然转化会报错!
Jacob word,excel转换html,txt 只要改变一个参数就可随意转化 WORD_HTML = 8; WORD_TXT = 7; EXCEL_HTML = 44; 替换这几个参数即可
NULL 博文链接:https://lixiamani.iteye.com/blog/581465
用于word的处理的工具类,可以转换word成pdf,或是操作word。jacob.jar内涵2个驱动文件
运用jacob将word文件转成pdf文件,word文件支持xml格式,首先将文档中的dll放到jdk的bin文件夹下,运行java文件即可
java操作word 超简单实用:第一步: 引入包jacob.jar 第二步:参考工具类MsWordTool.java 含概基本WORD 所有操作方法 【特别难点】解决实例: 1)word书签内容替换后,书签会丢失问题 2)word定点批量插入图片...
NULL 博文链接:https://kuaile863.iteye.com/blog/1390551
用Jacob,把word文档转换为html,还是很好用的,格式什么的也都保持很好; 需要说明的几点:1.要保证本地有office软件,才可以正常转换;2.jacob.dll放到jdk-->jre-->bin 路径下;3.把jacob.jar包加入到你的工程中;
Java 实现word文件增加图片水印 jacob实现 用到的jar itextpdf-5.5.2和 jacob
使用jacob通过word样表生成制式表格,包括文字的替换,图片的插入以及动态在word表格中追加数据,将生成的word文档转位pdf格式。
jacob操作word
里面包含了自己对word的基本操作的总结。
/** word文档 * * 在本类中有两种方式可以进行文档的创建, * 第一种调用 createNewDocument * 第二种调用 openDocument */
Java开发 利用Jacob 实现了word文件转成PDF文件
java打印word的jacob java打印word的jacobjava打印word的jacob
可以给Word文档使用jacob控件来添加页脚,然而使用jacob来处理Word文档的页脚自己找了很久都没能找到解决的办法,只有自己动手解决了。 此函数可以自动把页码作为文档的页脚,页脚可以随着页码的值自动改变,页码的...