2017-04-05 21 views
1

예, Apache POI로 생성 된 파일을 Excel에서 처리하고 있습니다. 파일은 xlsx가 아니고 xls 여야합니다. 화살표를 그릴 필요가 있지만 위쪽 화살표를 그릴 수는 없습니다. 내가 내 화살표와 지정 행/CEL 1 및 행/CEL을 만들 XSSFClientAnchor를 사용 2.Apache POI XSSF가 위 화살표를 만들 수 없습니다.

XSSFClientAnchor (INT의 DX1, INT의 DY1, INT의 DX2, INT의 DY2, INT의 COL1, INT의 ROW1, INT COL2, INT ROW2)

col1> col 2와 row 1> row 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); by : 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