2017-04-05 137 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