Page 1 of 1

Edit Charts in pptx (add new rows)

PostPosted: Wed Jun 18, 2014 12:29 am
by roy
Noob , trying to add news rows (entries) in a ppt ,

I want to open a chart, edit the existing values, and add more rows. but my lack of knowledge of pptx and openXML in general preventing me.

I need a reply very quick. Someone please reply quickly.

I am using the same code posted here in various forums.

Code: Select all
public static void main(String[] args) throws Docx4JException {

      // Input file
      String inputfilepath = System.getProperty("user.home") + "/Downloads/sample-2007.pptx";

      // The names of the parts which will be edited
      // Alter these to match what is in your input pptx
      // .. the chart
      String chartPartName = "/ppt/charts/chart1.xml";
      // .. the xlsx
      String xlsPartName = "/ppt/embeddings/Microsoft_Office_Excel_Worksheet1.xlsx";

      // Output file
      String outputfilepath = "/tmp/OUT_EditEmbeddedCharts-"
            + System.currentTimeMillis() + ".pptx";

      // Values to change
      Random rand = new Random();

      //String firstValue  = String.valueOf(rand.nextInt(99));
      //String secondValue = String.valueOf(rand.nextInt(99));
      String firstValue = "10";
      String secondValue = "20";
      // Open the PPT template file
      PresentationMLPackage ppt = (PresentationMLPackage) OpcPackage.load(new java.io.File(inputfilepath));

      /*
       * Get the Chart object and update the values. Afterwards, we'll update
       * the associated spreadsheet so that the data is synchronized.
       */
      Chart chart = (Chart) ppt.getParts().get(new PartName(chartPartName));

      List<Object> objects = chart.getJaxbElement().getChart().getPlotArea()
            .getAreaChartOrArea3DChartOrLineChart();
      
      for (Object object : objects) {
      
         if (object instanceof CTBarChart) {
            
            List<CTBarSer> ctBarSers = ((CTBarChart) object).getSer();
            
            
            
            
            
            CTUnsignedInt i = new CTUnsignedInt();
            i.setVal(5);
            
            CTNumData z = new CTNumData();
            z.setPtCount(i);
            
            CTNumRef y = new CTNumRef();
            y.setNumCache(z);
            
            CTNumDataSource x = new CTNumDataSource();
            x.setNumRef(y);
            
            CTBarSer ctbs = new CTBarSer();
            ctbs.setVal(x);
            
            ctBarSers.add(ctbs);
            ((CTBarChart) object).se
            for (CTBarSer ctBarSer : ctBarSers)
            {   
               List<CTNumVal> ctNumVals = ctBarSer.getVal().getNumRef().getNumCache().getPt();
               for (CTNumVal ctNumVal : ctNumVals)
               {   
                  System.out.println("ctNumVal Val BEFORE: " + ctNumVal.getV());
                  if (ctNumVal.getIdx() == 0) {
                     ctNumVal.setV(firstValue);
                  }
                  else if (ctNumVal.getIdx() == 1) {
                     ctNumVal.setV(secondValue);   
                  }
                  System.out.println("ctNumVal Val AFTER: " + ctNumVal.getV());
                  
               }
            }
         }
      }

      /*
       * Get the spreadsheet and find the cell values that need to be updated
       */

      EmbeddedPackagePart epp  = (EmbeddedPackagePart) ppt
         .getParts().get(new PartName(xlsPartName));

      if (epp==null) {
         throw new Docx4JException("Could find EmbeddedPackagePart: " + xlsPartName);
      }

      InputStream is = BufferUtil.newInputStream(epp.getBuffer());

      SpreadsheetMLPackage spreadSheet = (SpreadsheetMLPackage) SpreadsheetMLPackage.load(is);

      Map<PartName,Part> partsMap = spreadSheet.getParts().getParts();      
      Iterator<Entry<PartName, Part>> it = partsMap.entrySet().iterator();

      while(it.hasNext()) {
         Map.Entry<PartName, Part> pairs = it.next();

         if (partsMap.get(pairs.getKey()) instanceof WorksheetPart) {

            WorksheetPart wsp = (WorksheetPart) partsMap.get(pairs.getKey()) ;

            List<Row> rows = wsp.getJaxbElement().getSheetData().getRow();
            List<Row> moreRows = new ArrayList<Row>();
            rows.addAll(moreRows);
            for (Row row : rows) {
               List<Cell> cells = row.getC();
               for (Cell cell : cells)
               {
                  if (cell.getR().equals("B2") && cell.getV() != null) {
                     System.out.println("B2 CELL VAL: " + cell.getV());
                     // change the B2 cell value
                     cell.setT(STCellType.STR);
                     cell.setV(firstValue);
                  }
                  else if (cell.getR().equals("B3") && cell.getV() != null) {
                     System.out.println("B3 CELL VAL: " + cell.getV());
                     // Change the B3 cell value
                     cell.setT(STCellType.STR);
                     cell.setV(secondValue);
                  }
               }               
            }
            spreadSheet.addTargetPart(wsp, AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS);
         }
      }

      /*
       * Convert the Spreadsheet to a binary format, set it on the
       * EmbeddedPackagePart, add it back onto the deck and save to a file.
       * 
       */      
      ByteArrayOutputStream baos = new ByteArrayOutputStream();


      SaveToZipFile saver = new SaveToZipFile(spreadSheet);

      saver.save(baos);
      epp.setBinaryData(baos.toByteArray());

      // Write the new file to disk
      ppt.save(new java.io.File(outputfilepath));

      System.out.println("\n\n done .. saved " + outputfilepath);
   }

Re: Edit Charts in pptx (add new rows)

PostPosted: Thu Jun 19, 2014 4:13 am
by roy
Someone please reply quickly.

I do not understand the OpenXML structure very well and the Object structure .

I understand for every Bar there is an individual BarSer. One BarCer contains a Category and a Value section.

TO ADD NEW ROWS , i have to update the Cat and the Val section for both.

Should i be adding to StrRef, StrCache, NumCache, NumRef.. ???