2017-04-05 135 views
1

惠,我在Excel工作产生与Apache POI文件。该文件必须是xlsx而不是xls。 我需要绘制一些箭头,但我无法绘制向上的箭头。 我用XSSFClientAnchor创建我的箭头,并指定行/ CEL 1和行/ CEL 2.的Apache POI XSSF不能创建一个向上的箭头

XSSFClientAnchor(INT DX1,DY1 INT,INT DX2,诠释DY2,诠释COL1,诠释ROW1,诠释COL2,INT 2行)

它只能在COL1>第2栏和行1> 2行。所以我不能得出一个向上的箭头。 如果我尝试更改值以获得向上的箭头,则生成的文件不能被Excel读取,Excel会修复其中的箭头隐藏。

这里是我的代码:

public static void test() { 
    XSSFWorkbook wb = new XSSFWorkbook(); 
    XSSFSheet sheet = wb.createSheet("linechart"); 
    XSSFDrawing pat = sheet.createDrawingPatriarch(); 

    XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, 10, 10, 5, 5); 

    XSSFSimpleShape shape = pat.createSimpleShape(anchor); 
    shape.setShapeType(ShapeTypes.LINE); 
    shape.setLineWidth(4); 
    shape.setLineStyle(0); 
    shape.setLineStyleColor(0, 0, 0); 

    FileOutputStream fileOut; 
    try { 
     fileOut = new FileOutputStream(
       "C:\\monfichier" + new Date().toString().replace(':', '_') + ".xlsx"); 
     wb.write(fileOut); 
     fileOut.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

如果我试图取代: XSSFClientAnchor锚=新XSSFClientAnchor(0,0,0,0,10,10,5,5); 由: XSSFClientAnchor锚=新XSSFClientAnchor(0,0,0,0,5,5,10,10); 它的确定...

你能考这个,说我什么,你想一想。这真的很难找到信息约POI,我并没有发现这个问题...

回答

2

锚决定了形状的大小。对于线形,线条默认从第一个锚点单元格的左上角到左上角加上最后一个锚点单元的dxdy。第一个锚点单元是锚点左上角的单元格,而最后一个锚点单元格是锚点的单元格右下角。因此,默认情况下,线条形状将从左上角到右下角。

如果我们希望它是从左下到右上,那么我们就必须翻转的形状。

如果我们在Excel中从左下角到右上角画一条线,然后看看存储的/xl/drawings/drawing1.xml,我们将看到这个手动绘制的线条也被翻转过来。

实施例与两行:

import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*; 

import java.io.FileOutputStream; 

class CreateExcelLineShapes { 

public static void main(String[] args) throws Exception{ 

    Workbook workbook = new XSSFWorkbook(); 
    Sheet sheet = workbook.createSheet("Sheet1"); 

    CreationHelper helper = workbook.getCreationHelper(); 
    Drawing drawing = sheet.createDrawingPatriarch(); 

    ClientAnchor anchor = helper.createClientAnchor(); 

    //Anchor B2:C4 
    //This determines the size of the line shape to be from 
    //upper left edge of B2 to upper left edge of C4 plus dx and dy. 
    //Since dx and dy are both 0, this is the bottom right edge of B3. 
    anchor.setCol1(1); 
    anchor.setRow1(1); 
    anchor.setCol2(2); 
    anchor.setRow2(3); 

    //From here on XSSF only. 
    XSSFDrawing xssfdrawing = (XSSFDrawing)drawing; 
    XSSFClientAnchor xssfanchor = (XSSFClientAnchor)anchor; 

    //Draw a line from upper left edge of B2 to upper left edge of C4 = bottom right edge of B3. 
    //This is the default. 
    XSSFSimpleShape xssfshape = xssfdrawing.createSimpleShape(xssfanchor); 
    xssfshape.setShapeType(ShapeTypes.LINE); 
    xssfshape.setLineWidth(4); 
    xssfshape.setLineStyle(0); 
    xssfshape.setLineStyleColor(0, 0, 0); 

    //This sets the arrow line end type: 
    xssfshape.getCTShape().getSpPr().getLn().addNewTailEnd().setType(
    org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType.TRIANGLE); 

    //Again draw a line from upper left edge of B2 to upper left edge of C4 = bottom right edge of B3. 
    xssfshape = xssfdrawing.createSimpleShape(xssfanchor); 
    xssfshape.setShapeType(ShapeTypes.LINE); 
    xssfshape.setLineWidth(4); 
    xssfshape.setLineStyle(0); 
    xssfshape.setLineStyleColor(0, 0, 0); 

    //Now flip this vertically. 
    //So it now will to be from bottom left edge of B3 to upper left edge of B2. 
    xssfshape.getCTShape().getSpPr().getXfrm().setFlipV(true); 

    xssfshape.getCTShape().getSpPr().getLn().addNewTailEnd().setType(
    org.openxmlformats.schemas.drawingml.x2006.main.STLineEndType.TRIANGLE); 

    workbook.write(new FileOutputStream("CreateExcelLineShapes.xlsx")); 
    workbook.close(); 

} 
} 

结果:

enter image description here