先说下 需要的依赖包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.12</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.8</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.8</version> </dependency><!-- 生成图片--> <dependency> <groupId>org.jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.19</version> </dependency> <dependency><!--支持插入图片--> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>3.3.1</version> </dependency>
示例 , 下图
如上图 , 需要替换的字符串地方 "$ 1" 为 "1231" , 在指定位置插入书签 , 并命名 "Test" , 插入的图片如下
本人也没太过多去研究 , 亲测通过有效 , 在这分享下
1.Demo
import Java.awt.font; import Java.io.file; import Java.io.fileinputstream; Import Java.io.FileOutputStream; Import Java.io.ioException; Import Java.io.inputStream; Import Java.BiginTeger; java.util.iterator; import Java.util.list; import Java.util.map; import Java.util.set; import org.apache.poi.poixmldocument; import org.apache.poi.openxml4j.opc.opcpackage; imporg.apache.po.poi.util.iutils; org.apache.poi.xwpf.usermodel.xwpfdocument; import org.apache.poi.xwpf.usmodel.xwpf -paragraph; org.apache.poi.xwpf.usermodel.xwpfrun; org.docx4j.dml.wordprocessingdrawing.inline; import org.docx4j.finder.Rangefinder; import org.docx4j.openpackaging.packages.wordprocessingmlpackage; org.docx4j.openpackaging.parts.wordprocessingml.maindocumentArt; import org.docx4j.wml.body; import org.docx4j.wml.boolenandfaultTrue; import org.docx4j.wml.ctbookmark; org.docx4j.wml.document; import org.docx4j.wml.drawing; import org.docx4j.wml.hpsMeasure; org.docx4j.wml.rpr; import org.jfree.chart.chartfactory; import org.jfree.chart.chartutilities; import org.jfree.chart.jfreechart; org.jfree.chart.title.texttitle; import org.jfree.data.general. com.google.common.collect.maps; public class exportbgServiceImpl {public static void main (String [] args) löst eine Ausnahme aus {map <String, String> map = maps.newhashMap (); map.put ("$ 1", "1231"); XWPFDocument document = new XWPFDocument (poixmldocument.openpackage ("d: //tp//test.docx")); Iterator <xWPFTRAGRAPH> ITPARA = DOCUMENT.GETTARAGRAPHSISTERATOR (); while (itpara.hasnext ()) {xwpf -Abschnitt Absatz = (xWPFTRATRAPH) ITPARA.NEXT (); Liste <xwpfrun> runs = Absatz.getruns (); für (int i = 0; i <runs.size (); i ++) {String OneParastring = runs.get (i) .GeteText (runs.get (i) .GetextPosition ()); für (map.entry <String, String> Eintrag: map.enterrySet ()) {if (OneParastring.equals (Eintrag.GeteKey ()) {OneParastring = OneParastring.Replace (Entry.GeteKey (), Entry.getValue ()); }} runs.get (i) .setText (OneParastring, 0); }} FileOutputStream outstream = null; outstream = new FileOutputStream ("d: //tp//test1.docx"); document.write (überstream); outstream.close (); // ------------------------------- DataSet.SetValue ("修改类", 1); DataSet.SetValue ("提示类", 1); Dataset.setValue ("校验不通过", 3); Dataset.setValue ("正常类", 3); JfreEchart chart = chartFactory.CreatePieChart3d (null, dataset, true, false, false); chart.getLegend (). setItemfont (neue Schriftart ("黑体", font.bold, 15)); // 设置图例类别字体 // textTitle title = new textTitle (titlestring); // title.setfont (neue Schriftart ("黑体", font.italic, 20)); // 设置标题字体 // chart.settitle (Titel); Pieplot pieplot = (pieplot) chart.getPlot (); Decimalformat DF = neue Dezimalformat ("0,00%"); NumnFormat nf = numberFormat.getInstance (); StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);// 获得StandardPieSectionLabelGenerator对象,生成的格式, // {0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义pieplot.setLabelGenerator (Generator); // 设置百分比 pieplot.setLabelfont (neue Schriftart ("黑体", font.italic, 15); // 设置饼图中类别字体 pieplot.setnodatamessage ("此时并没有任何数据可用"); pieplot.setCircular (falsch); pieplot.setLabelGap (0,02d); pieplot. Datei Datei = new Datei ("d: // tp", fname); if (file.exists ()) {file.delete (); } try {chartUtilities.savechartaspng (Datei, Diagramm, 800, 500); Datei Datei2 = new Datei ("d: //tp//test1.docx"); WordprocessingMlpackage wpackage = wortprocessingMlpackage.load (neuer FileInputStream (Datei2)); MainDocumentPart mainDocumentPart = wpackage.getMaindocumentPart (); Document wmldoc = (document) mainDocumentPart.getJaxBelement (); Body Body = wmldoc.getBody (); // 提取正文中所有段落 List <Object> Absätze = body.getContent (); // 提取书签并创建书签的游标 RangeFinder rt = neuer Rangefinder ("CTBookmark", "ctmarkuprange"); Neue Traversalutil (Absätze, RT); für (ctbookmark bm: rt.getStarts ()) {if (bm.getName (). Equals ("test") {// 这里的 test 为 word 文档中预设的 书签名 inputStream inputStream = new FileInputStream (Datei); byte [] bytes = ioutils.tobytearray (InputStream); BinaryPartabStractImage ImagePart = binaryPartabStractImage.CreateImageArtArt (Wpackage, Bytes); Inline inline = imagePart.createimageInline (null, null, 0, 1, false, 10000); // 这里的 100000 不是正常屏幕大小 用于设置插入图片的大小 用于设置插入图片的大小 p p = (p) (bm.getParent ()); ObjectFactory Factory = new ObjectFactory (); // r 对象是匿名的复杂类型 , 然而我并不知道具体啥意思 , 估计这个要好好去看看 ooxml 才知道 r run = factory.creater (); // Zeichnen 理解为画布? Zeichnung zeichnen = fabrik.createdrawing (); Drawing.GetanchorInline (). Add (Inline); run.getContent (). Add (Zeichnung); p.getContent (). add (run); }} wpackage.save (new FileOutputStream (neue Datei ("d: //tp//test1.docx")); } catch (ioException e) {}}}最后效果图如下 :
总结
以上所述是小编给大家介绍的 Java 替换 Wort 文档文字并指定位置插入图片 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!