javajacob操作word文档,进行写操作,如生成表格,添加图片

上传人:小** 文档编号:112519800 上传时间:2022-06-22 格式:DOC 页数:11 大小:343KB
返回 下载 相关 举报
javajacob操作word文档,进行写操作,如生成表格,添加图片_第1页
第1页 / 共11页
javajacob操作word文档,进行写操作,如生成表格,添加图片_第2页
第2页 / 共11页
javajacob操作word文档,进行写操作,如生成表格,添加图片_第3页
第3页 / 共11页
点击查看更多>>
资源描述
javajacob操作word文档,进行写操作,如生成表格,添加图片jacob-1.15-M3-x86.dllcopy到c:/windows/system32引入jacob.jar示例代码javaviewplaincopy1.importjavaio.File;2.importcom.jacob.activeX.ActiveXComponent;3.Dispatch;4.Variant;5.classWordBean6/代表一个word程序7.privateActiveXComponentMsWordApp=null;8./代表进彳亍处理的word文档9.privateDispatchdocument=null;10.publicWordBean()11./OpenWordifwe/venotdoneitalready12.if(MsWordApp=null)13.MsWordApp=newActiveXComponent(WordApplication);14.15.16./设置是否在前台打开word程序,17.publicvoidsetVisible(booleanvisible)18.MsWordApp.setProperty(“Visible,newVariant(visible);19./这一句作用相同20./Dispatch.put(MsWordApp,Visible,newVariant(visible);21.22./创建一个新文档23.publicvoidcreateNewDocument()24./FindtheDocumentscollectionobjectmaintainedbyWord25./documents表示word的所有文档窗口,(word是多文档应用程序)26.Dispatchdocuments=Dispatchget(MsWordApp,Documents)toDispatch();27./CalltheAddmethodoftheDocumentscollectiontocreate28./anewdocumenttoedit29.document=Dispatch.call(documents,Add)toDispatch();30.31./打开一个存在的word文档,并用document引用弓I用它32.publicvoidopenFile(StringwordFilePath)33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77./FindtheDocumentscollectionobjectmaintainedbyWord/documents表示word的所有文档窗口,(word是多文档应用程序)Dispatchdocuments=Dispatchget(MsWordApp,Documents)toDispatch();document=Dispatch.call(documents,Open,wordFilePath,newVariant(true)/*是否进彳亍转换ConfirmConversions*/,newVariant(false)/*是否只读*/).toDispatch();/document=Dispatch.invoke(documents,Open,Dispatch.Method,/newObjectwordFilePath,newVariant(true),/newVariant(false)/,newint1)toDispatch();/向document中插入文本内容publicvoidinsertText(StringtextTolnsert)/GetthecurrentselectionwithinWordatthemoment/anewdocumenthasjustbeencreatedthenthiswillbeat/thetopofthenewdoc获得选中的内容,如果是一个新创建的文件,因里面无内容,则光标应处于文件开头处Dispatchselection=Dispatchget(MsWordApp,Selection)toDispatch();/取消选中,应该就是移动光标,否则新添加的内容会覆盖选中的内容Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1);/PutthespecifiedtextattheinsertionpointDispatch.put(selection,Text,textTolnsert);/取消选中,应该就是移动光标Dispatch.call(selection,MoveRight,newVariant(1),newVariant(1);/向文档中添加一个图片,publicvoidinsertJpeg(StringjpegFilePath)Dispatchselection=Dispatchget(MsWordApp,Selection)toDispatch();Dispatchimage=Dispatchget(selection,InLineShapes)toDispatch();Dispatch.call(image,AddPicture,jpegFilePath);/段落的处理,插入格式化的文本publicvoidinsertFormatStr(Stringtext)DispatchwordContent=Dispatch.get(document,Content)toDispatch();/取得word文件的内容Dispatchcall(wordContent,InsertAfter,text);/插入一个段落到最后Dispatchparagraphs=Dispatchget(wordContent,Paragraphs).toDispatch();/所有段落intparagraphCount=Dispatch.get(paragraphs,Count)changeType(VariantVariantint)getint();/一共的段落数/找到刚输入的段落,设置格式DispatchlastParagraph=Dispatch.call(paragraphs,Item,newVariant(paragraphCount).toDispatch();/最后一段(也就是刚插入的)/Range对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义DispatchlastParagraphRange=Dispatchget(lastParagraph,Range)toDispatch();Dispatchfont=Dispatch.get(lastParagraphRange,Font)toDispatch();Dispatch.putfont,Bold,newVariant(true);/设置为黑体Dispatch.putfont,Italic,newVariant(true);/设置为斜体Dispatch.putfont,Name,newVariant(宋体);/Dispatch.putfont,Size,newVariant(12);/小四Dispatchselection=Dispatchget(MsWordApp,Selection)toDispatch();Dispatch.call(selection,TypeParagraph);/插入一个空行Dispatchalignment=Dispatchget(selection,ParagraphFormat).toDispatch();/段落格式Dispatch.put(alignment,Alignment,2);/(1:置中2:靠右3:靠左)/word中在对表格进彳亍遍历的时候,是先列后行先column后cell/另外下标从1开始publicvoidinsertTable(StringtableTitle,introw,intcolumn)Dispatchselection=Dispatch.get(MsWordApp,Selection)toDispatch();/输入内容需要的对象Dispatch.call(selection,TypeText,tableTitle);/写入标题内容/标题格彳亍Dispatch.call(selection,TypeParagraph);/空一行段落Dispatch.call(selection,TypeParagraph);/空一行段落Dispatch.call(selection,MoveDown);/游标往下一行/建立表格Dispatchtables=Dispatchget(document,Tables)toDispatch();/intcount=Dispatchget(tables,/Count)changeType(VariantVariantInt)getInt();/document中的表格数量/Dispatchtable=Dispatch.call(tables,Item,newVariant(/1).toDispatch();/文档中第一个表格Dispatchrange=Dispatch.get(selection,Range).toDispatch();/当前光标位置或者选中的区域DispatchnewTable=Dispatch.call(tables,Add,range,newVariant(row),newVariant(column),newVariant(1).toDispatch();/设置row,column,表格外框宽度Dispatchcols=Dispatch.get(newTable,Columns).toDispatch();/此表的所有列,intcolCount=Dispatchget(cols,Count)changeType(Variant.Variantint).getint();/共有多少列实际上这个数=columnSystemoutprintln(colCount+列);for(inti=1;i=colCount;i+)/循环取岀每一列Dispatchcol=Dispatch.call(cols,Item,newVariant(i)toDispatch();Dispatchcells=Dispatch.get(col,Cells).toDispatch();/当前列中单元格intcellCount=Dispatchget(cells,Count)changeType(Variant.Variantint).getint();/当前列中单元格数实际上这个数等于rowfor(intj=1;j=cellCount;j+)/每一列中的单元格数/Dispatchcell=Dispatch.call(cells,Item,new/Variant(j).toDispatch();/当前单元格/Dispatchcell=Dispatch.call(newTable,Cell,new/Variant(j),newVariant(i).toDispatch();/取单元格的另一种方法78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122./Dispatch.call(cell,Select);/选中当前单元格/Dispatch.put(selection,Text,/合并两个单元格123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166./第+j+行,第+i+列);/往选中的区域中填值,也就是往当前单元格填值putTxtToCell(newTable,j,i,第+j+行,第+i+列);/与上面四句的作用相同/*/*在指定的单元格里填写数据* paramtableIndex* paramcellRowIdx* paramcellColIdx* paramtxt*/publicvoidputTxtToCell(Dispatchtable,intcellRowIdx,intcellColIdx,Stringtxt)Dispatchcell=Dispatch.call(table,Cell,newVariant(cellRowIdx),newVariant(cellColIdx)toDispatch();Dispatch.call(cell,Select);Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/输入内容需要的对象Dispatch.put(selection,Text,txt);/*/*在指定的单元格里填写数据* paramtableIndex* paramcellRowIdx* paramcellColIdx* paramtxt*/publicvoidputTxtToCell(inttableIndex,intcellRowIdx,intcellColIdx,Stringtxt)/所有表格Dispatchtables=Dispatchget(document,Tables)toDispatch();/要填充的表格Dispatchtable=Dispatch.call(tables,Item,newVariant(tableIndex)toDispatch();Dispatchcell=Dispatch.call(table,Cell,newVariant(cellRowIdx),newVariant(cellColIdx)toDispatch();Dispatch.call(cell,Select);Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/输入内容需要的对象Dispatch.put(selection,Text,txt);/合并两个单元格167.168.169.170.171.172.173.174.175.176.177.178.179.180.181.182.183.184.185.186.187.188.189.190.191.192.193.194.195.196.197.198.199.200.201.202.203.204.205.206.207.208.209.210.211.publicvoidmergeCell(Dispatchcell1,Dispatchcell2)Dispatch.call(cell1,Merge,cell2);publicvoidmergeCell(Dispatchtable,introw1,intcol1,introw2,intcol2)Dispatchcell1=Dispatch.call(table,Cell,newVariant(row1),newVariant(col1)toDispatch();Dispatchcell2=Dispatch.call(table,Cell,newVariant(row2),newVariant(col2)toDispatch();mergeCell(cell1,cell2);publicvoidmergeCellTest()Dispatchtables=Dispatchget(document,Tables)toDispatch();inttableCount=Dispatchget(tables,Count)changeType(VariantVariantint).getInt();/document中的表格数量Dispatchtable=Dispatch.call(tables,Item,newVariant(tableCount).toDispatch();/文档中最后一个tablemergeCell(table,1,1,1,2);/将table中x=1,y=1与x=1,y=2的两个单元格合并/=/*/*把选定的内容或光标插入点向上移动* parampos* 移动的距离*/publicvoidmoveUp(intpos)Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/输入内容需要的对象for(inti=0;ipos;i+)/MoveDownMoveLeftmoveRight/moveStart(Dispatch.call(selection,HomeKey,newVariant(6);/)/moveEndDispatch.call(selection,EndKey,newVariant(6);Dispatch.call(selection,MoveUp);/*/*从选定内容或插入点开始查找文本* paramtoFindText* 要查找的文本* returnbooleantrue-查找到并选中该文本,false-未查找到文本*/合并两个单元格212.213.214.215.216.217.218.219.220.221.222.223.224.225.226.227.228.229.230.231.232.233.234.235.236.237.238.239.240.241.242.243.244.245.246.247.248.249.250.251.252.253.254.255.256.publicbooleanfind(StringtoFindText)if(toFindText=null|toFindTextequals()returnfalse;Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/输入内容需要的对象/从selection所在位置开始查询Dispatchfind=Dispatch.call(selection,Find)toDispatch();/设置要查找的内容/合并两个单元格/合并两个单元格Dispatch.put(find,Text,toFindText);/合并两个单元格/合并两个单元格/向前查找Dispatch.put(find,Forward,True);/合并两个单元格/合并两个单元格/设置格式Dispatch.put(find,Format,True);/合并两个单元格/合并两个单元格/大小写匹配Dispatch.put(find,MatchCase,True);/合并两个单元格/合并两个单元格/全字匹配Dispatch.put(find,MatchWholeWord,True);/查找并选中returnDispatch.call(find,Execute)getBoolean();/*/*把选定选定内容设定为替换文本* paramtoFindText* 查找字符串* paramnewText* 要替换的内容* return*/publicbooleanreplaceText(StringtoFindText,StringnewText)if(!find(toFindText)returnfalse;Dispatchselection=Dispatchget(MsWordApp,Selection).toDispatch();/输入内容需要的对象Dispatch.put(selection,Text,newText);returntrue;publicvoidprintFile()/JustprintthecurrentdocumenttothedefaultprinterDispatch.call(document,Printout);/保存文档的更改publicvoidsave()Dispatch.call(document,Save);publicvoidsaveFileAs(Stringfilename)257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);publicvoidcloseDocument()258.259.260.261.262.263.264.265.266.267.268.269.270.271.272.273.274.275.276.277.278.279.280.281.282.283.284.285.286.287.288.289.290.291.292.293.294.295.296.297.298.299.300.257.Dispatchcall(document,SaveAs,filename);/0=wdDoNotSaveChanges/-1=wdSaveChangesClosethedocumentwithoutsavingchanges257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/-2=wdPromptToSaveChangesDispatch.call(document,Close,newVariant(0);document=null;publicvoidcloseWord()Dispatch.call(MsWordApp,Quit);MsWordApp=null;document=null;/设置wordApp打开后窗口的位置publicvoidsetLocation()DispatchactiveWindow=Dispatchget(MsWordApp,Application)toDispatch();Dispatch.put(activeWindow,Windowstate,newVariant(1);/0=default/1=maximize/2=minimizeDispatch.put(activeWindow,Top,newVariant(0);Dispatch.put(activeWindow,Left,newVariant(0);Dispatch.put(activeWindow,Height,newVariant(600);publicDispatch.put(activeWindow,classJacobTest2width,newVariant(800);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);publicstaticvoidcreateANewFileTest()WordBeanwordBean=newWordBean();程序/wordopenWord(true);/打开wordwordBean.setVisible(true);wordBean.createNewDocument();/创建一个新文档wordBean.setLocation();/设置打开后窗口的位置wordBean.insertText(你好);/向文档中插入字符wordBean.insertJpeg(D:+File.separator+ajpg);/插入图片/如果,想保存文件,下面三句257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/wordsaveFileAs(d:/adoc);257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);/word.closeDocument();/wordcloseWord();257.Dispatchcall(document,SaveAs,filename);257.Dispatchcall(document,SaveAs,filename);publicstaticvoidopenAnExistsFileTest()WordBeanwordBean=newWordBean();301.302.303.304.305.306.307.308.309.310.311.312.313.314.315.316.317.318.319.320.321.322.323.324.325.326.327.328.329.330.331.332.333.334.335.336.337.338.339.340.wordBean.setVisible(true);/是否前台打开word程序,或者后台运行wordBeanopenFile(d:/adoc);wordBean.insertJpeg(D:“+File.separator+a.jpg);/插入图片(注意刚打开的word/,光标处于开头,故,图片在最前方插入)wordBeansave();wordBean.closeDocument();wordBeancloseWord();publicstaticvoidinsertFormatStr(Stringstr)WordBeanwordBean=newWordBean();wordBean.setVisible(true);/是否前台打开word程序,或者后台运行wordBean.createNewDocument();/创建一个新文档wordBean.insertFormatStr(str);/插入一个段落,对其中的字体进行了设置publicstaticvoidinsertTableTest()WordBeanwordBean=newWordBean();wordBean.setVisible(true);/是否前台打开word程序,或者后台运行wordBean.createNewDocument();/创建一个新文档wordBean.setLocation();wordBean.insertTable(“表名,3,2);wordBeansaveFileAs(d:/tabledoc);wordBean.closeDocument();wordBeancloseWord();publicstaticvoidmergeTableCellTest()insertTableTest();/生成d:/tabledocWordBeanwordBean=newWordBean();wordBean.setVisible(true);/是否前台打开word程序,或者后台运行wordBeanopenFile(d:/tabledoc);wordBeanmergeCellTest();publicstaticvoidmain(Stringargs)/进行测试前要保证d:/a.jpg图片文件存在/createANewFileTest();/创建一个新文件/openAnExistsFileTest();/打开一个存在的文件/insertFormatStr(格式化字符串);/对字符串进行一定的修饰/insertTableTest();/创建一个表格mergeTableCellTest();/对表格中的单元格进行合并
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!