How to move position of chart in excel by Java POI -
i want add row in excel java poi , try both shiftrows()
function , createrow()
function
both function can add row in excel below chart position remain , not move
i move (shift down) position of chart
i use poi version 3.9
can give me advice or idea move position of chart image
as fact, data range of chart not changed. need not move position of charts need increase data range of chart
thanks!!
the shifting of drawing anchors determine chart positions possible. method void insertrowsshiftshapes(sheet sheet, int startrow, int n)
drawing anchors affected of row inserting process sheet.
the correcting of chart data ranges affected of row inserting sheet complicated said already. not tested , not ready yet. provide working draft. hope useful start point further programming.
for running code ooxml-schemas-1.3.jar
needed mentioned in apache poi faq
a resource documentation of ooxml-schema objects me grepcode
examples: cttwocellanchor, ctpiechart, ctpieser
import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.openxml4j.exceptions.invalidformatexception; import java.io.*; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetdrawing.cttwocellanchor; import org.openxmlformats.schemas.drawingml.x2006.chart.ctpiechart; import org.openxmlformats.schemas.drawingml.x2006.chart.ctpieser; import java.util.list; class insertrowsabovechart { //a method shift rows , shift anchors in drawing below shifted rows private static void insertrowsshiftshapes(sheet sheet, int startrow, int n) { java.util.list<cttwocellanchor> drawinganchors = ((xssfdrawing)sheet.getdrawingpatriarch()).getctdrawing().gettwocellanchorlist(); (cttwocellanchor drawinganchor : drawinganchors) { int fromrow = drawinganchor.getfrom().getrow(); int torow = drawinganchor.getto().getrow(); if (fromrow >= startrow) { drawinganchor.getfrom().setrow(fromrow + n); drawinganchor.getto().setrow(torow + n); } } sheet.shiftrows(startrow, sheet.getlastrownum(), n); correctdatarangesofcharts(sheet, startrow, n); } //a method correcting data ranges charts affected of shifted rows //!!working draft, not ready yet!! private static void correctdatarangesofcharts(sheet sheet, int startrow, int n) { java.util.list<xssfchart> charts = ((xssfdrawing)sheet.getdrawingpatriarch()).getcharts(); (xssfchart chart : charts) { //pie charts java.util.list<ctpiechart> piecharts = chart.getctchart().getplotarea().getpiechartlist(); (ctpiechart piechart : piecharts) { java.util.list<ctpieser> pieseries = piechart.getserlist(); (ctpieser pieserie : pieseries) { boolean strrefchanged = false; if (pieserie.getcat().issetmultilvlstrref()) { string strref = pieserie.getcat().getmultilvlstrref().getf(); //todo: corrects end row of ranges, should correct start row if affected int strrefendrow = integer.parseint(strref.substring(strref.lastindexof('$') + 1)); if (strrefendrow >= startrow) { strref = strref.substring(0, strref.lastindexof('$') +1) + (strrefendrow + n); pieserie.getcat().getmultilvlstrref().setf(strref); strrefchanged = true; } } else if (pieserie.getcat().issetstrref()) { string strref = pieserie.getcat().getstrref().getf(); int strrefendrow = integer.parseint(strref.substring(strref.lastindexof('$') + 1)); if (strrefendrow >= startrow) { strref = strref.substring(0, strref.lastindexof('$') +1) + (strrefendrow + n); pieserie.getcat().getstrref().setf(strref); strrefchanged = true; } } if (strrefchanged) { string numref = pieserie.getval().getnumref().getf(); int numrefendrow = integer.parseint(numref.substring(numref.lastindexof('$') + 1)); if (numrefendrow >= startrow) { numref = numref.substring(0, numref.lastindexof('$') +1) + (numrefendrow + n); pieserie.getval().getnumref().setf(numref); } } } } //pie charts end } } public static void main(string[] args) { try { inputstream inp = new fileinputstream("workbook.xlsx"); workbook wb = workbookfactory.create(inp); sheet sheet = wb.getsheetat(0); //sheet.shiftrows(3, 5, 4); insertrowsshiftshapes(sheet, 2, 4); fileoutputstream fileout = new fileoutputstream("workbook.xlsx"); wb.write(fileout); wb.close(); } catch (invalidformatexception ifex) { } catch (filenotfoundexception fnfex) { } catch (ioexception ioex) { } } }
Comments
Post a Comment