您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > docx4j操作word
Docx4j操作word目录1.Docx4j介绍...............................................................................................................................11.1.Docx4j介绍................................................................................................................12.Docx4j操作...............................................................................................................................12.1.初始读取word..........................................................................................................22.2.标签替换....................................................................................................................22.3.chart图表内容替换..................................................................................................42.4.插入图片....................................................................................................................93.文档的一些操作.....................................................................................................................103.1.合并word................................................................................................................103.2.word转换成pdf......................................................................................................121.Docx4j介绍1.1.Docx4j介绍docx4j是一个用于创建和操作MicrosoftOpenXML(Worddocx,Powerpointpptx,和Excelxlsx)文件的Java类库。官方主页。2.Docx4j操作2.1.初始读取wordWordprocessingMLPackagewordMLPackage=WordprocessingMLPackage.load(WordChartDataRenderTest.class.getClassLoader().getResourceAsStream(tpl.docx));2.2.标签替换在操作word的时候,多数情况的下是通过标签进行替换相关的内容。标签在word中可以定义一个位置,或者标示一个段落内容。在一个模板中定义好标签,在插入-》书签:然后定义好名称。在java的代码中对标签进行遍历,得到该名称,并替换成相关的内容。//try{ListObjectparagraphs=wordMLPackage.getMainDocumentPart().getContent();RangeFinderrt=newRangeFinder(CTBookmark,CTMarkupRange);newTraversalUtil(paragraphs,rt);//遍历所有的标签for(CTBookmarkbm:rt.getStarts()){if(StringUtil.isEmpty(bm.getName()))continue;ReportDataobj=data.get(bm.getName());if(obj==null||!(objinstanceofReportData)){if(bm.getName().indexOf(_)==0){}else{logger.error(bookmarkfilldata,dataisempty!+bm.getName());}continue;}try{ListObjecttheList=null;ParaRPrrpr=null;if(bm.getParent()instanceofP){PPrpprTemp=((P)(bm.getParent())).getPPr();if(pprTemp==null){rpr=null;}else{rpr=((P)(bm.getParent())).getPPr().getRPr();}theList=((ContentAccessor)(bm.getParent())).getContent();}else{continue;}intrangeStart=-1;intrangeEnd=-1;inti=0;for(Objectox:theList){ObjectlistEntry=XmlUtils.unwrap(ox);if(listEntry.equals(bm)){rangeStart=i+1;}elseif(listEntryinstanceofCTMarkupRange){if(((CTMarkupRange)listEntry).getId().equals(bm.getId())){rangeEnd=i-1;break;}}i++;}if(rangeStart=0){for(intj=rangeEnd;j=rangeStart;j--){theList.remove(j);}org.docx4j.wml.Rrun=factory.createR();org.docx4j.wml.Textt=factory.createText();run.getContent().add(t);if(rpr!=null)run.setRPr(paraRPr2RPr(rpr,factory));t.setValue(value);theList.add(rangeStart,run);}}catch(Exceptioncce){logger.error(cce.getMessage(),cce);throwcce;}}}catch(Exceptione){throwe;}找到相应的位置然后替换,相应的文本,需要注意的是替换相关的内容的格式。2.3.chart图表内容替换chart图的替换有两种方法,一种是word里面的插入的图表,这时在进行更改值的时候,需要更改图标的内容,然后再更新excel的相关内容。【没有找到更新一个自动更新另一个的方法。】publicclassBarChartTest{publicstaticvoidmain(String[]args)throwsDocx4JException{WordprocessingMLPackagewordMLPackage=WordprocessingMLPackage.load(WordChartDataRenderTest.class.getClassLoader().getResourceAsStream(tpl.docx));ChartDatadata=newChartData();ObjectFactoryfactory=neworg.docx4j.wml.ObjectFactory();ListObjectparagraphs=wordMLPackage.getMainDocumentPart().getContent();HashMapPartName,Partparts=wordMLPackage.getParts().getParts();parts.forEach((partName,part)-{if(partName.getName().toLowerCase().indexOf(excel)0){//sSystem.out.println(part.getClass().getName());}//if(partName.getName().indexOf(charts)0&&(partinstanceofChart)){Chartchart=(Chart)part;ListObjectobjects;try{objects=chart.getContents().getChart().getPlotArea().getAreaChartOrArea3DChartOrLineChart();if(objects.size()=1){objects.forEach((obj)-{if(objinstanceofCTBarChart){CTBarChartctRadarChart=(CTBarChart)obj;Stringstr=partName.getName();str=str.substring(str.lastIndexOf(/)+1);System.out.println(str);BarChartTest._renderCTBarChart(ctRadarChart,Arrays.asList(Arrays.asList(12,23),Arrays.asList(89,56)),Arrays.asList(A,b),Arrays.asList(A11,b11));}});}}catch(Docx4JExceptione){e.printStackTrace();}}//endif});EmbeddedPackagePartepp=(EmbeddedPackagePart)wordMLPackage.getParts().get(newPartName(/word/embeddings/Microsoft_Excel____1.xlsx));if(epp==null){thrownewDocx4JException(CouldfindEmbeddedPackagePart:/word/embeddings/Microsoft_Excel____1.xlsx);}InputStreamis=BufferUtil.newInputStream(epp.getBuffer());SpreadsheetMLPackagespreadSheet=(SpreadsheetMLPackage)SpreadsheetMLPackage.load(is);MapPartName,PartpartsMap=spreadSheet.getParts().getParts();IteratorEntryPartName,Partit=partsMap.entrySet().iterator();while(it.hasNext()){Map.EntryPartName,Partpairs=it.next();if(partsMap.get(pairs.getKey())instanceofWorksheetPart)
本文标题:docx4j操作word
链接地址:https://www.777doc.com/doc-3875905 .html