在excel中使用xssf库绘制带有箭头的行

我想以编程方式在excel表格中用箭头绘制一条线,我可以创build线条而不是箭头。 这里是我的代码,我需要改变我想要的东西;

XSSFSimpleShape shape = drawing.createSimpleShape(a); shape.setShapeType(ShapeTypes.LINE); shape.setLineWidth(1.5); shape.setLineStyle(3); 

我尝试使用shape.setShapeType(ShapeTypes.LEFT_ARROW); shape.setShapeType(ShapeTypes.RIGHT_ARROW);shape.setShapeType(ShapeTypes.UP_ARROW); 也是,但这并没有帮助我。

这是我的:

在这里输入图像说明

这就是我要的: 在这里输入图像说明

Drawing支持似乎是不完全在Apache POI。 所以需要使用底层对象。

在Excel中,箭头是行的头部或尾部属性。

例:

 import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import java.io.FileOutputStream; import java.io.IOException; import org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties; import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType; import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndLength; import org.openxmlformats.schemas.drawingml.x2006.main.STLineEndWidth; class ShapeArrow { public static void main(String[] args) { try { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("Sheet1"); CreationHelper helper = wb.getCreationHelper(); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(2); anchor.setRow1(2); anchor.setCol2(5); anchor.setRow2(5); XSSFSimpleShape shape = ((XSSFDrawing)drawing).createSimpleShape((XSSFClientAnchor)anchor); shape.setShapeType(ShapeTypes.LINE); shape.setLineWidth(1.5); shape.setLineStyle(3); shape.setLineStyleColor(0,0,255); //apache POI sets first shape Id to 1. It should be 0. shape.getCTShape().getNvSpPr().getCNvPr().setId(shape.getCTShape().getNvSpPr().getCNvPr().getId()-1); CTShapeProperties shapeProperties = shape.getCTShape().getSpPr(); CTLineProperties lineProperties = shapeProperties.getLn(); CTLineEndProperties lineEndProperties = org.openxmlformats.schemas.drawingml.x2006.main.CTLineEndProperties.Factory.newInstance(); lineEndProperties.setType(STLineEndType.TRIANGLE); lineEndProperties.setLen(STLineEndLength.LG); lineEndProperties.setW(STLineEndWidth.LG); lineProperties.setHeadEnd(lineEndProperties); FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); wb.write(fileOut); } catch (IOException ioex) { } } } 

POI中有一个用于获取CTShape对象的XSSFShape.getCTShape()方法。 但是有了这个,我们就失去了POI文件。 所以看文档http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/drawingml/x2006/spreadsheetDrawing/CTShape.java