JAVA实现替换WORD模板中的文字和图片

更新时间:2023-07-14 03:13:33 阅读: 评论:0

JAVA实现替换WORD模板中的⽂字和图⽚牡丹怎么养
1.本来有其他⽅法可以实现的,但是由于jar包⼀直有问题所以换了种,要是还有其他的更好的⽅法,希望可以讨论
2.废话不说直接上代码;
3.我⾃⼰加了个⽣成图⽚的⽅法,也可以不需要直接⽤已经存在的图⽚替换即可,注意jar包的引⼊,import的时候看清楚是什么包!!!先给出MAVEN中l需要导⼊的包
<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>
ample.demo;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;科学绘本
DecimalFormat;
NumberFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import s.io.IOUtils;
import org.apache.poi.POIXMLDocument;
import org.docx4j.wml.Document;
import org.apache.poi.xwpf.urmodel.XWPFDocument;手机无法充电
import org.apache.poi.xwpf.urmodel.XWPFParagraph;
import org.apache.poi.xwpf.urmodel.XWPFRun;
import util.Maps;
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.*;
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.al.DefaultPieDatat;
public class ReplaceWordUtils {
//模板路径
public static final String DOC_PATH = "D:\\report\\test.docx";
//图⽚路径
public static final String IMAGE_PATH = "D:\\report\\test.png";
//替换⽂字图⽚后的路径
public static final String DOC_PATH2 = "D:\\report\\test1.docx";
过户委托书
/
/word⽂档中替换⽂字⽅法
public static void replaceContent(Map<String, String> map,String wordPath){
XWPFDocument document = null;
try
{
document = new XWPFDocument(POIXMLDocument.openPackage(wordPath));
Iterator<XWPFParagraph> itPara = ParagraphsIterator();
while (itPara.hasNext()) {
XWPFParagraph paragraph = (XWPFParagraph) ();
List<XWPFRun> runs = Runs();
for (int i = 0; i < runs.size(); i++) {
String oneparaString = (i).(i).getTextPosition());
for (Map.Entry<String, String> entry : Set()) {
if (oneparaString.Key())) {
oneparaString = Key(), Value());
}
}
<(i).tText(oneparaString, 0);
}
}
FileOutputStream outStream = null;
outStream = new FileOutputStream(DOC_PATH2);
document.write(outStream);
outStream.clo();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void replaceImage(){
//-----------------------------------插⼊图⽚---------------------------------------
File file = new File(IMAGE_PATH);
try {
性表演File file2 = new File(DOC_PATH2);
WordprocessingMLPackage wPackage = WordprocessingMLPackage.load(new FileInputStream(file2));
MainDocumentPart mainDocumentPart = MainDocumentPart();
Document wmlDoc = (Document) JaxbElement();
Body body = Body();
// 提取正⽂中所有段落
List<Object> paragraphs = Content();
// 提取书签并创建书签的游标
RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
new TraversalUtil(paragraphs, rt);
for (CTBookmark bm : rt.getStarts()) {
if (bm.getName().equals("images")) {// 这⾥的test为 word⽂档中预设的书签名
InputStream inputStream = new FileInputStream(file);
byte[] bytes = ByteArray(inputStream);
BinaryPartAbstractImage imagePart = ateImagePart(wPackage, bytes);
Inline inline = ateImageInline(null, null, 0, 1, fal, 10000);//这⾥的100000不是正常屏幕⼤⼩,⽤于设置插⼊图⽚的⼤⼩                    P p = (P) (bm.getParent());
ObjectFactory factory = new ObjectFactory();
// R对象是匿名的复杂类型,然⽽我并不知道具体啥意思,估计这个要好好去看看ooxml才知道
R run = ateR();
// drawing理解为画布?
Drawing drawing = ateDrawing();
}
}
wPackage.save(new FileOutputStream(new File(DOC_PATH2)));
} catch (Exception e) {
e.printStackTrace();
}
}
//⽣成图⽚
public static void initImg(){
DefaultPieDatat datat = new DefaultPieDatat();
datat.tValue("修改类", 1);
datat.tValue("提⽰类", 1);
datat.tValue("校验不通过", 3);
datat.tValue("正常类", 3);
JFreeChart chart = atePieChart3D(null, datat, true, fal, fal);
// TextTitle title = new TextTitle(titleString);
// title.tFont(new Font("⿊体", Font.ITALIC, 20));//设置标题字体
// chart.tTitle(title);
PiePlot piePlot = (PiePlot) Plot();
DecimalFormat df = new DecimalFormat("0.00%");
NumberFormat nf = Instance();
天津学校排名
StandardPieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator("{0} {2}", nf, df);// 获得StandardPieSectionLabelGenerator对象,⽣成的格式,        // {0}表⽰ction名,{1}表⽰ction的值,{2}表⽰百分⽐。可以⾃定义
piePlot.tLabelGenerator(generator);// 设置百分⽐
piePlot.tLabelFont(new Font("⿊体", Font.ITALIC, 15));// 设置饼图中类别字体
piePlot.tNoDataMessage("此时并没有任何数据可⽤");
piePlot.tCircular(fal);
piePlot.tLabelGap(0.02D);
piePlot.tIgnoreNullValues(true);// 设置不显⽰空位
piePlot.tIgnoreZeroValues(true);// 设置不显⽰负值或零值
try
{
ChartUtilities.saveChartAsPNG(new File(IMAGE_PATH), chart, 800, 500);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Map<String, String> map = new HashMap<>();
map.put("unit","4");
map.put("startValue","85");
map.put("controlRod10","90");
map.put("plan","35计划");
map.put("position","总经理处");
//替换⽂字
replaceContent(map,DOC_PATH);
//⽣成图⽚
initImg();
//替换图⽚
replaceImage();
}
}
4.以上是所有代码,其中模板截图如下:箭头是要替换的⽂字以及图⽚需要插⼊标签,替换的时候需要⽤到
5.最后的结果是这样的
以上就是整个逻辑代码了,可以⾃⼰在尝试下,有的逻辑可以⾃⼰改改,有什么问题可以留⾔。

本文发布于:2023-07-14 03:13:33,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1080661.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:替换   设置   模板   书签
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图