Jacob操作office文档Word文档格式.docx
- 文档编号:18142096
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:15
- 大小:18.18KB
Jacob操作office文档Word文档格式.docx
《Jacob操作office文档Word文档格式.docx》由会员分享,可在线阅读,更多相关《Jacob操作office文档Word文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
ExportAsFixedFormat"
pdfFilePath,wdFormatPDF);
//word保存为pdf格式宏,值为17
returntrue;
//setflagtrue;
}catch(ComFailExceptione){
returnfalse;
}catch(Exceptione){
}finally{
if(doc!
=null){
Close"
}
if(app!
app.invoke("
Quit"
0);
ComThread.Release();
Jacob操作ppt
publicbooleanppt2pdf(StringsrcFilePath,StringpdfFilePath){
Dispatchppt=null;
PowerPoint.Application"
Dispatchppts=app.getProperty("
Presentations"
//因POWER.EXE的发布规则为同步,所以设置为同步发布
ppt=Dispatch.call(ppts,"
srcFilePath,true,//ReadOnly
true,//Untitled指定文件是否有标题
false//WithWindow指定文件是否可见
).toDispatch();
Dispatch.call(ppt,"
SaveAs"
pdfFilePath,ppSaveAsPDF);
//ppSaveAsPDF为特定值32
if(ppt!
Jacob操作Excel
packagecom;
importjava.io.ObjectInputStream.GetField;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importcom.jacob.activeX.ActiveXComponent;
import.ComThread;
import.Dispatch;
import.Variant;
publicclassready{
privatestaticActiveXComponentxl=null;
//Excel对象(防止打开多个)
privatestaticDispatchworkbooks=null;
//工作簿对象
privateDispatchworkbook=null;
//具体工作簿
privateDispatchsheets=null;
//获得sheets集合对象
privateDispatchcurrentSheet=null;
//当前sheet
/**
*打开excel文件
*@paramfilepath文件路径名称
*@paramvisible是否显示打开
*@paramreadonly是否只读方式打开
*/
privatevoidOpenExcel(Stringfilepath,booleanvisible){
initComponents();
//清空原始变量
if(xl==null)
xl=newActiveXComponent("
Excel.Application"
//Excel对象
xl.setProperty("
newVariant(visible));
//设置是否显示打开excel
if(workbooks==null)
workbooks=xl.getProperty("
Workbooks"
//打开具体工作簿
workbook=Dispatch.invoke(workbooks,"
newVariant(false),//是否以只读方式打开
newVariant(true),
"
1"
},//输入密码"
若有密码则进行匹配,无则直接打开
e.printStackTrace();
releaseSource();
*工作簿另存为
*@paramfilePath另存为的路径
*例如SaveAs="
D:
TEST/c.xlsx"
privatevoidSaveAs(StringfilePath){
Dispatch.call(workbook,"
filePath);
*关闭excel文档
*@paramf含义不明(关闭是否保存?
默认false)
privatevoidCloseExcel(booleanf){
Save"
newVariant(f));
/*
*初始化
**/
privatevoidinitComponents(){
workbook=null;
currentSheet=null;
sheets=null;
*释放资源
privatestaticvoidreleaseSource(){
if(xl!
=null){
xl.invoke("
newVariant[]{});
xl=null;
workbooks=null;
System.gc();
*得到当前sheet
*@return
privateDispatchgetCurrentSheet(){
currentSheet=Dispatch.get(workbook,"
ActiveSheet"
returncurrentSheet;
*修改当前工作表的名字
*@paramnewName
privatevoidmodifyCurrentSheetName(StringnewName){
Dispatch.put(getCurrentSheet(),"
name"
newName);
*得到当前工作表的名字
privateStringgetCurrentSheetName(Dispatchsheets){
returnDispatch.get(sheets,"
).toString();
*通过工作表名字得到工作表
*@paramnamesheetName
privateDispatchgetSheetByName(Stringname){
returnDispatch.invoke(getSheets(),"
Item"
Dispatch.Get,newObject[]{name},newint[1]).toDispatch();
*得到sheets的集合对象
privateDispatchgetSheets(){
if(sheets==null)
sheets=Dispatch.get(workbook,"
sheets"
returnsheets;
*通过工作表索引得到工作表(第一个工作簿index为1)
*@paramindex
*@returnsheet对象
privateDispatchgetSheetByIndex(Integerindex){
Dispatch.Get,newObject[]{index},newint[1]).toDispatch();
*得到sheet的总数
privateintgetSheetCount(){
intcount=Dispatch.get(getSheets(),"
count"
).toInt();
returncount;
*给所有的sheet添加背景
*@paramfilepath图片路径
publicvoidsetBlackGroudPrituce(Stringfilepath)
{
intnum=this.getSheetCount();
for(inti=1;
i<
=num;
i++){
Dispatchsheets=this.getSheetByIndex(i);
Dispatch.call(sheets,"
SetBackgroundPicture"
filepath);
*添加新的工作表(sheet),并且隐藏(添加后为默认为当前激活的工作表)
publicvoidaddSheet(Stringname){
//for(inti=1;
=this.getSheetCount();
//Dispatchsheets=this.getSheetByIndex(i);
//if(name.equals(this.getCurrentSheetName(sheets)))
//{
//returnfalse;
//}
currentSheet=Dispatch.get(Dispatch.get(workbook,"
).toDispatch(),"
add"
//Dispatch.put(currentSheet,"
Name"
name);
Dispatch.put(currentSheet,"
newBoolean(false));
System.out.println("
插入信息为:
"
+name);
*得到工作薄的名字
privateStringgetWorkbookName(){
if(workbook==null)
returnnull;
returnDispatch.get(workbook,"
*获取所有表名
publicListfindSheetName()
Listlist=newArrayList();
currentSheet=this.getSheetByIndex(i);
list.add(this.getCurrentSheetName(currentSheet));
returnlist;
*设置页脚信息
privatevoidsetFooter(Stringfoot){
currentSheet=this.getCurrentSheet();
DispatchPageSetup=Dispatch.get(currentSheet,"
PageSetup"
Dispatch.put(PageSetup,"
CenterFooter"
foot);
*获取页脚信息
privateStringgetFooter(){
returnDispatch.get(PageSetup,"
*锁定工作簿
privatevoidsetPassword(){
Protect"
123,true,false);
*设置名称管理器
*@paramname名称管理器名不能以数字或者下划线开头,中间不能包含空格和其他无效字符
*@paramcomment备注
*@paramplace备注位置
publicvoidsetName(Stringname,Stringplace,Stringcomment){
DispatchNames=Dispatch.get(workbook,"
Names"
Dispatch.call(Names,"
Add"
name,place,false).toDispatch();
Dispatch.put(Names,"
Comment"
comment);
//插入备注
*获取名称管理器
*@paramname名称管理器名
publicStringgetName(Stringname){
DispatchName=Dispatch.call(Names,"
name).toDispatch();
returnDispatch.get(Name,"
Value"
*单元格写入值
*@paramsheet被操作的sheet
*@paramposition单元格位置,如:
C1
*@paramtype值的属性如:
value
*@paramvalue
privatevoidsetValue(Stringposition,Objectvalue){
Dispatchcell=Dispatch.invoke(currentSheet,"
Range"
Dispatch.Get,newObject[]{position},newint[1])
.toDispatch();
Dispatch.put(cell,"
value);
Stringcolor=this.getColor(cell);
this.setFont(cell,color);
*设置字体
privatevoidsetFont(Dispatchcell,Stringcolor)
Dispatchfont=Dispatch.get(cell,"
Font"
//Dispatch.put(font,"
FontStyle"
"
BoldItalic"
Dispatch.put(font,"
size"
color"
color);
*获取背景颜色
privateStringgetColor(Dispatchcell)
DispatchInterior=Dispatch.get(cell,"
Interior"
Stringcolor=Dispatch.get(Interior,"
returncolor;
*单元格读取值
C1
*@paramsheet
privateVariantgetValue(Stringposition){
Dispatch.Get,
newObject[]{position},newint[1]).toDispatch();
Variantvalue=Dispatch.get(cell,"
returnvalue;
*获取最大行数
privateintgetRowCount(){
DispatchUsedRange=Dispatch.get(currentSheet,"
UsedRange"
Dispatchrows=Dispatch.get(UsedRange,"
Rows"
intnum=Dispatch.get(rows,"
).getInt();
returnnum;
*获取最大列数
privateintgetColumnCount(){
DispatchColumns=Dispatch.get(UsedRange,"
Columns"
intnum=Dispatch.get(Columns,"
*获取位置
*@paramrnum最大行数
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Jacob 操作 office 文档