java
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.PictureShape;
import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class PPTGenerator {
public static void main(String[] args) {
try {
String templatePath = "C:\\Users\\Lenovo\\Desktop\\333.pptx";
String outputPath = "C:\\Users\\Lenovo\\Desktop\\222.pptx";
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(templatePath));
Map<String, String> textMap = new HashMap<String, String>();
textMap.put("text1", "上周,全国统调电厂日均供煤752万吨,日均耗煤713万吨,当前库存22901万吨,达到去年极值的109%,可用32天,同比增加2天。");
textMap.put("text2", "国网统调电厂日均供煤663万吨,日均耗煤633万吨,当前库存19521万吨,达到去年极值的107%,可用31天,同比增加2天。四川电煤库存上升至567万吨,完成度冬储煤目标。");
List<List<String>> tableList = new LinkedList<List<String>>();
List<String> rowList = new LinkedList<String>();
rowList.add("湖北");
rowList.add("623");
rowList.add("618");
rowList.add("0.93%");
rowList.add("35");
tableList.add(rowList);
tableList.add(rowList);
List<String> imageList = new LinkedList<>();
imageList.add("C:\\Users\\Lenovo\\Desktop\\图片1.jpg");
int imageNum = 0;//;图片是按顺序替换
for (XSLFSlide slide : ppt.getSlides()) {
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
List<XSLFTextParagraph> paragraphs = textShape.getTextParagraphs();
for (XSLFTextParagraph paragraph : paragraphs) {
List<XSLFTextRun> runs = paragraph.getTextRuns();
for (XSLFTextRun run : runs) {
String textToReplace = run.getRawText();
String textValue = textMap.get(textToReplace);
if (StringUtils.isBlank(textValue)){
continue;
}
textToReplace = textToReplace.replace(textToReplace, textValue);
run.setText(textToReplace);
}
}
} else if (shape instanceof XSLFTable) {// 检查形状是否是表格
XSLFTable table = (XSLFTable) shape;
List<XSLFTableRow> rows = table.getRows();
for (int i = 0; i < tableList.size(); i++) {
XSLFTableRow row = table.getRows().get(i+1);
for (int j = 0; j < tableList.get(i).size(); j++) {
XSLFTableCell cell = row.getCells().get(j);
cell.clearText();
// cell.setText(tableList.get(i).get(j));
XSLFTextParagraph paragraph = cell.addNewTextParagraph();
paragraph.setTextAlign(TextParagraph.TextAlign.CENTER);
XSLFTextRun run = paragraph.addNewTextRun();
run.setText(tableList.get(i).get(j));
// 设置字体样式
run.setFontFamily("黑体"); // 设置字体名称
run.setFontSize(9.0); // 设置字体大小
// run.setBold(true); // 设置加粗
// run.setItalic(true); // 设置斜体
// 如果需要设置字体颜色,可以使用以下代码(这里使用RGB颜色)
// run.setFontColor(new java.awt.Color(255, 0, 0)); // 红色字体
}
}
//删除多余的预留空白行
while (tableList.size()+1<rows.size()){
table.removeRow(tableList.size()+1);
}
} else if (shape instanceof XSLFPictureShape) {// 检查形状是否是图片
byte[] imageData = Files.readAllBytes(Paths.get(imageList.get(imageNum)));
PictureData pictureData = ((PictureShape) shape).getPictureData();
pictureData.setData(imageData);
imageNum++;
}
}
}
FileOutputStream out = new FileOutputStream(outputPath);
ppt.write(out);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("PPT generated successfully!");
}
}
maven依赖
java
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version> <!-- 请使用最新版本 -->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version> <!-- 选择一个合适的版本 -->
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>5.1.1</version>
</dependency>