Excel Apache POI-带有文本的形状

时间:2019-09-03 17:23:35

标签: java apache-poi

我创建了一个带有textString的形状。我已经能够居中放置文本框,但是似乎无法居中对齐中心。

使用Apache Poi 3.9版

EX:

enter image description here

我尝试了以下方法:

XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 1, 25, 3, 32);
XSSFSimpleShape shape = drawing.createSimpleShape(anchor);
XSSFRichTextString textString = new XSSFRichTextString();
XSSFFont font = workbook.createFont();

font.setColor(HSSFColor.WHITE.index);
shape.setShapeType(ShapeTypes.ELLIPSE);

textString.setString(LeadSheetCoverPage.ADD_YOUR_LOGO.getText(dataStore.getLanguage()));
textString.applyFont(font);

shape.getCTShape().getTxBody().getBodyPr().setAnchor(STTextAnchoringType.CTR);
// This line below doesn't seem to align the Paragraph text to center 
shape.getCTShape().getTxBody().getPList().get(0).getPPr().setAlgn(STTextAlignType.CTR);

shape.setFillColor(211, 211, 211);
shape.setLineStyleColor(0, 0, 0);
shape.setText(textString);

2 个答案:

答案 0 :(得分:2)

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2使用当前的apache poi 4.1.0提供了XSSFSimpleShape.setVerticalAlignment。文本在提供XSSFTextParagraph.setTextAlignXSSFSimpleShape中。

示例:

XSSFTextParagraph

答案 1 :(得分:0)

对我有用的不是以上两种,我也不想升级。

CTTextParagraphProperties lv1PPr = 
        shape.getCTShape().getTxBody().getLstStyle().addNewLvl1PPr();
lv1PPr.setAlgn(STTextAlignType.CTR);

由于某种原因,上述方法不起作用,但对STTextAnchoringType起作用。