How to resize a chart using XSSF (Apache POI 3.8)?

后端 未结 2 1304
暗喜
暗喜 2021-01-15 20:52

I am wondering whether there is a way to resize a chart using Apache POI (XSSF). Currently I am using an Excel template, which has a chart that changes when more data is ins

相关标签:
2条回答
  • 2021-01-15 21:16

    After researching how xlsx works, I was able to find how to get it done.

    //Call the partiarch to start drawing
    XSSFDrawing drawing = ((XSSFSheet)currentSheet).createDrawingPatriarch();
    //Create CTMarket for anchor
    CTMarker chartEndCoords = CTMarker.Factory.newInstance();
    //The coordinates are set in columns and rows, not pixels.
    chartEndCoords.setCol(column);
    //Set Column offset
    chartEndCoords.setColOff(0);
    chartEndCoords.setRow(row);
    chartEndCoords.setRowOff(0);
    //drawing.getCTDrawing().getTwoCellAnchorArray(0).setFrom(chartStartCoords);
    drawing.getCTDrawing().getTwoCellAnchorArray(0).setTo(chartEndCoords);
    
    /*
        This line of code allows to resize the chart:
            The Patriarch is what allows to get control over the drawings, since
            a chart is considered a graph in xlsx you can access it with getCTDrawing.
            Each graph is stored in the tag getTwoCellAnchorArray, where the array position
            is the chart you have; for example getTwoCellAnchorArray(3) would refer to the
            forth graph within the sheet.
    
            Each getTwoCellAnchorArray has several properties as FROM and TO, which define
            where the existing graph starts and ends.   
    */
    

    If you have any comments, let me know.

    0 讨论(0)
  • 2021-01-15 21:22
    1. Give your date cells, date format. Apache poi date format

    2. POI cannot modify graphics AFAIK.

    0 讨论(0)
提交回复
热议问题