這篇文章主要介紹Java如何替換word文檔文字并指定位置插入圖片,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、龍灣ssl等。為超過(guò)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的龍灣網(wǎng)站制作公司
先說(shuō)下 需要的依賴(lài)包
org.apache.poi poi-excelant 3.12 org.apache.poi poi-scratchpad 3.12 org.apache.poi poi-ooxml 3.8 org.apache.poi poi-ooxml-schemas 3.8 org.jfree jfreechart 1.0.19 org.docx4j docx4j 3.3.1
示例,下圖
如上圖,需要替換的字符串地方“$1”為“1231”,在指定位置插入書(shū)簽,并命名“test” ,插入的圖片如下
本人也沒(méi)太過(guò)多去研究,親測(cè)通過(guò)有效,在這分享下
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.math.BigInteger; import java.text.DecimalFormat; import java.text.NumberFormat; import 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; import org.apache.poi.util.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.docx4j.TraversalUtil; import org.docx4j.dml.wordprocessingDrawing.Inline; import org.docx4j.finders.RangeFinder; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.docx4j.wml.Body; import org.docx4j.wml.BooleanDefaultTrue; import org.docx4j.wml.CTBookmark; import org.docx4j.wml.Color; import org.docx4j.wml.Document; import org.docx4j.wml.Drawing; import org.docx4j.wml.HpsMeasure; import org.docx4j.wml.ObjectFactory; import org.docx4j.wml.P; import org.docx4j.wml.R; import org.docx4j.wml.RPr; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.labels.StandardPieSectionLabelGenerator; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset;import com.aisino.qysds.common.constant.ERRORConstants; import com.aisino.qysds.common.exception.SysException; import com.aisino.qysds.service.IExportBgService; import com.google.common.collect.Maps; public class ExportBgServiceImpl { public static void main(String[] args) throws Exception { Mapmap = Maps.newHashMap(); map.put("$1", "1231"); XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("D:\\tp\\test.docx")); Iterator itPara = document.getParagraphsIterator(); while (itPara.hasNext()) { XWPFParagraph paragraph = (XWPFParagraph) itPara.next(); List runs = paragraph.getRuns(); for (int i = 0; i < runs.size(); i++) { String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition()); for (Map.Entry entry : map.entrySet()) { if (oneparaString.equals(entry.getKey())) { oneparaString = oneparaString.replace(entry.getKey(), entry.getValue()); } } runs.get(i).setText(oneparaString, 0); } } FileOutputStream outStream = null; outStream = new FileOutputStream("D:\\tp\\test1.docx"); document.write(outStream); outStream.close(); //-----------------------------------這塊為生成圖片 和 插入圖片 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("修改類(lèi)", 1); dataset.setValue("提示類(lèi)", 1); dataset.setValue("校驗(yàn)不通過(guò)", 3); dataset.setValue("正常類(lèi)", 3); JFreeChart chart = ChartFactory.createPieChart3D(null, dataset, true, false, false); chart.getLegend().setItemFont(new Font("黑體", Font.BOLD, 15)); // 設(shè)置圖例類(lèi)別字體 // TextTitle title = new TextTitle(titleString); // title.setFont(new Font("黑體", Font.ITALIC, 20));//設(shè)置標(biāo)題字體 // chart.setTitle(title); PiePlot piePlot = (PiePlot) chart.getPlot(); DecimalFormat df = new DecimalFormat("0.00%"); NumberFormat nf = NumberFormat.getInstance(); StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);// 獲得StandardPieSectionLabelGenerator對(duì)象,生成的格式, // {0}表示section名,{1}表示section的值,{2}表示百分比??梢宰远x piePlot.setLabelGenerator(generator);// 設(shè)置百分比 piePlot.setLabelFont(new Font("黑體", Font.ITALIC, 15));// 設(shè)置餅圖中類(lèi)別字體 piePlot.setNoDataMessage("此時(shí)并沒(méi)有任何數(shù)據(jù)可用"); piePlot.setCircular(false); piePlot.setLabelGap(0.02D); piePlot.setIgnoreNullValues(true);// 設(shè)置不顯示空位 piePlot.setIgnoreZeroValues(true);// 設(shè)置不顯示負(fù)值或零值 String fName = "pie.png"; File file = new File("D:\\tp", fName); if (file.exists()) { file.delete(); } try { ChartUtilities.saveChartAsPNG(file, chart, 800, 500); File file2 = new File("D:\\tp\\test1.docx"); WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(file2)); MainDocumentPart mainDocumentPart = wPackage.getMainDocumentPart(); Document wmlDoc = (Document) mainDocumentPart.getJaxbElement(); Body body = wmlDoc.getBody(); // 提取正文中所有段落 List
最后效果圖如下:
以上是“Java如何替換word文檔文字并指定位置插入圖片”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!