在
XSLFTable
表格中,有两列 - 一列包含文本,另一列是百分比数字,如下图所示。
虽然创建表格和数据非常简单,但如何将圆角矩形形状包含在文本下方的每个单元格中(第一列),其中矩形形状的宽度实际上是第二个单元格中的数字?
这是表格的代码(在
Kotlin
中,但应该可以通过IDEA在
Java
中轻松转换)-
val pptx = XMLSlideShow()
val slide = ppt.createSlide()
val tbl = slide.createTable()
tbl.anchor = Rectangle(10, 90, 200, 150)
// row 1
val tr = tbl.addRow()
val td1 = tr.addCell()
val td2 = tr.addCell()
val r1 = td1.addNewTextParagraph().addNewTextRun()
r1.setText("Option 1")
val r2 = td2.addNewTextParagraph().addNewTextRun()
r2.setText("8.3%")
// row 2
// ...
// row 3
val tr1 = tbl.addRow()
val td3 = tr1.addCell()
val td4 = tr1.addCell()
val r3 = td3.addNewTextParagraph().addNewTextRun()
r1.setText("Option 3")
val r4 = td4.addNewTextParagraph().addNewTextRun()
r4.setText("51.9%")
r4.isBold = true
// row 4
// ...
tbl.setColumnWidth(0, 150.0)
tbl.setColumnWidth(0, 50.0)
PowerPoint 表格单元格中文本下方的每个单元格中不能包含形状。 PowerPoint 表格单元格只是文本形状。因此它只能包含文本。 PowerPoint 不提供与文本内嵌对齐的形状或图片。表格单元格可以有背景,但不能是形状,而只能是图片。
因此,关于此类要求的第一个问题始终是:如何使用 PowerPoint GUI 创建所需的内容?
我会使用将两个文本框和每个选项的矩形形状分组的组形状来完成此操作。
完整示例:
import java.io.FileOutputStream;
import org.apache.poi.sl.usermodel.*;
import org.apache.poi.xslf.usermodel.*;
import java.awt.Rectangle;
public class CreatePPTXGroupShape02 {
public static void main(String[] args) throws Exception {
Object[][] data = new Object[][] {
new Object[] {"Option1", 8.3, java.awt.Color.LIGHT_GRAY},
new Object[] {"Option2", 38.3, java.awt.Color.LIGHT_GRAY},
new Object[] {"Option3", 51.9, java.awt.Color.GREEN},
new Object[] {"Option4", 1.3, java.awt.Color.LIGHT_GRAY}
};
SlideShow slideShow = new XMLSlideShow();
Slide slide = slideShow.createSlide();
int groupLeft = 100;
int groupTop = 50;
int groupWidth = 500;
int groupHeight = 40;
int groupPadding= 10;
TextShape textShape;
Rectangle rect;
String text;
Number value;
AutoShape autoShape;
java.awt.Color color;
for (Object[] row : data) {
GroupShape shapeGroup = slide.createGroup();
shapeGroup.setInteriorAnchor(new Rectangle(groupLeft, groupTop, groupWidth, groupHeight));
shapeGroup.setAnchor(new Rectangle(groupLeft+groupPadding, groupTop+groupPadding, groupWidth-groupPadding, groupHeight-groupPadding));
int x = groupLeft;
int y = groupTop;
int width;
int height = 40;
text = (String)row[0];
value = (Number)row[1];
color = (java.awt.Color)row[2];
// AutoShape = rounded rect bar
width = (int)Math.round(450d * value.doubleValue() / 100d);
autoShape = shapeGroup.createAutoShape();
rect = new Rectangle(x, y, width, height);
autoShape.setAnchor(rect);
autoShape.setShapeType(ShapeType.ROUND_RECT);
autoShape.setFillColor(color);
// TextBox = option text
width = 400;
textShape = shapeGroup.createTextBox();
rect = new Rectangle(x, y, width, height);
textShape.setAnchor(rect);
textShape.setText(text);
// TextBox = percentages
x = groupLeft + 400;
y = groupTop;
width = 100;
height = 40;
textShape = shapeGroup.createTextBox();
rect = new Rectangle(x, y, width, height);
textShape.setAnchor(rect);
text = String.valueOf(value);
textShape.setText(text + " %");
groupTop += 40;
}
FileOutputStream out = new FileOutputStream("./CreatePPTXGroupShape.pptx");
slideShow.write(out);
out.close();
}
}
结果: