2015-12-31 3 views
0

Я хочу нарисовать линию со стрелкой в ​​листе листа программно, я могу создать линию, но не стрелку в этом. Вот мой код, где мне нужно что-то изменить;Линия рисования со стрелкой в ​​excel с использованием библиотек xssf

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); тоже, но это не помогло мне.

Это то, что у меня есть:

enter image description here

Это то, что я хочу: enter image description here

ответ

1

Поддержка 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 есть XSSFShape.getCTShape() способ получения CTShape объекта. Но с этим мы теряемся с документацией POI. См. http://grepcode.com/file/repo1.maven.org/maven2/org.apache.poi/ooxml-schemas/1.1/org/openxmlformats/schemas/drawingml/x2006/spreadsheetDrawing/CTShape.java для документации.

+0

Большое спасибо .. !!! Я получил то, что хочу, просто используя приведенный вами пример. – Shivam