代码之家  ›  专栏  ›  技术社区  ›  wzl

如何使用POI绘制圆(而不是椭圆)

  •  0
  • wzl  · 技术社区  · 2 年前

    我试着在excel中的一个单元格上画一个圆(而不是椭圆),但总是得到一个椭圆。 我想要什么: enter image description here 我得到的: enter image description here

    这是代码:

        Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("Sheet1");
            Drawing<?> drawing = sheet.createDrawingPatriarch();
            XSSFClientAnchor anchor = new XSSFClientAnchor();
            anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
            anchor.setCol1(0);
            anchor.setCol2(1);
            anchor.setRow1(0);
            anchor.setRow2(1);
            
            // here I set the size of the circle, making its diameter in x-axis equals to diameter y-axis, but didn't work.  
            // anchor.setDx1(0);
            // anchor.setDx2(255);
            // anchor.setDy1(0);
            // anchor.setDy2(255);
    
            XSSFSimpleShape circle = ((XSSFDrawing) drawing).createSimpleShape(anchor);
            circle.setShapeType(ShapeTypes.ELLIPSE);
            CTShape ctShape = circle.getCTShape();
            
            // change an ellipse into a circle
            CTShapeProperties spProps = ctShape.getSpPr();
            if (spProps == null) {
                spProps = ctShape.addNewSpPr();
            }
            CTTransform2D xfrm = spProps.isSetXfrm() ? spProps.getXfrm() : spProps.addNewXfrm();
            CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm.addNewExt();
            long length = Units.toEMU(0.5 * 914400 / 2);
            ext.setCx(length);
            ext.setCy(length);
    
            // set color
            CTSolidColorFillProperties fillProps = spProps.isSetSolidFill() ? spProps.getSolidFill() : spProps.addNewSolidFill();
            fillProps.addNewSrgbClr().setVal(new byte[] { (byte) 0xFF, 0x00, 0x00 });
            
            workbook.write(new FileOutputStream("example.xlsx"));
            workbook.close();
    
    0 回复  |  直到 2 年前
    推荐文章