PowerPoint表格单元格文本下方的每个单元格中不能包含形状.PowerPoint表格单元格仅为文本形状.因此,它只能包含文本.PowerPoint不提供与文本内联对齐的形状或图片.表格单元格可能有背景,但它不能是形状,而只是图片.
因此,关于此类要求的第一个问题始终是:如何使用PowerPoint图形用户界面创建您需要的内容?
我会使用分组形状来完成这一任务,分组形状将两个文本框和每个选项的矩形形状分组.
完整示例:
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(400d * 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();
}
}
结果: