本文介绍了无法在 pptx 中的 BarChart 中看到 Apache POI 更新的数据值而无需编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 pptx 模板,它只有 1 张幻灯片用于测试.幻灯片有一个简单的条形图.我可以通过在 pptx 文件上双击条形图来编辑条形图,并且可以更改 Sheet1(条形图的数据表)中的值,并且我能够立即在条形图中看到更改.

I have a pptx template, it has just 1 slide for testing purpose. The slide has a simple bar chart. I am able to edit the bar chart by double clicking it on pptx file and I could change the values in Sheet1 (Data sheet for Barchart), and, I able to see the changes immediately in BarChart.

现在,我正在尝试使用 POI API 做同样的事情.我在这里执行以下步骤

Now, I am trying to do the same using POI API. I am doing the below steps here

  1. 阅读模板文件 = "MyTemplate.pptx" - https://docs.google.com/file/d/0B-q0lBy0lKLic3dCSUVsZUdGQzA/edit?usp=sharing
  2. 拥有地图中的所有形状
  3. 通过引用名称来读取条形图形状 - MyBarChart"
  4. 读取条形图的excel文件
  5. 更新 Sheet1 中的单元格值
  6. 保存所有内容并写入另一个文件 - MyPresentation.pptx"

当我打开文件 - MyPresentation.pptx"时,它不会在栏中预先显示更新的单元格值.我需要双击图表将其更改为 EDIT 模式以获取反映的最新值.为什么 BarChart 在使用 POI 更新基础数据表时没有刷新?

When I open the file - "MyPresentation.pptx", it does not show up the updated cell value upfront in the Bar. I need to double click the chart to change it to EDIT mode to get the latest value reflected. Why does BarChart is not getting refreshed when it's underlying Data Sheet is updated using POI?

有什么解决问题的建议吗?

Any suggestion to solve the issue?

这是完整的代码,附上pptx模板文件.

Here is the completed code, attached pptx template file as well.

package com.ppt;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFGraphicFrame;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSheet;
import org.apache.poi.xslf.usermodel.XSLFSlide;

public class PPTChart {

    public static void main(String args[]) throws InvalidFormatException, IOException{ 

        XMLSlideShow ppt;

        // Read pptx template
        ppt = new XMLSlideShow(new FileInputStream("MyTemplate.pptx"));

        // Get all slides
        XSLFSlide[] slide = ppt.getSlides();

        // Get working slide that is slide=0
        XSLFSlide slide0 = slide[0];
        XSLFShape[] shapes = slide0.getShapes();

        // Add all shapes into a Map
        Map <String, XSLFShape> shapesMap = new HashMap<String, XSLFShape>();
        for(XSLFShape shape : shapes)
        {
            shapesMap.put(shape.getShapeName(), shape);
            System.out.println(shape.getShapeName() + "  " + shape.getShapeId() + "   " + shape);

        }

        // Read the bar chart
        XSLFGraphicFrame chart = (XSLFGraphicFrame) shapesMap.get("MyBarChart");

        // Get the chart sheet
        XSLFSheet sheet =  chart.getSheet();

        for(int i=0; i<sheet.getRelations().size(); i++)
        {
            System.out.println("Partname =" + sheet.getRelations().get(i).getPackagePart().getPartName());



            if(sheet.getRelations().get(i).getPackagePart().getPartName().toString().contains(".xls"))
            {

                System.out.println("Found the bar chart excel");

                // BarChart Excel package part
                PackagePart barChartExcel  = sheet.getRelations().get(i).getPackagePart();

                // Reference the excel in workbook
                HSSFWorkbook wb = new HSSFWorkbook(barChartExcel.getInputStream());

                // Read sheet where Barchart data is available
                HSSFSheet mysheet =  wb.getSheetAt(1);

                // Read first
                HSSFRow row = mysheet.getRow(1);


                //Print first cell value for debugging
                System.out.println("Updating cell value from - " + row.getCell(1));

                // New value
                double insertValue = 7777777.0;


                wb.getSheetAt(1).getRow(1).getCell(1).setCellValue(insertValue);

                // Set first BarChart as active sheet
                HSSFSheet mysheet0 =  wb.getSheetAt(0);
                mysheet0.setActive(true);

                // Write the updated excel back to workbook
                OutputStream excelOut = barChartExcel.getOutputStream();
                excelOut.flush();
                wb.write(excelOut);
                excelOut.close();

                // Write workbook to file
                FileOutputStream o = new FileOutputStream("MyPresentation.pptx");
                ppt.write(o);
                o.close();
                System.out.println("new ppt is created....");

                break; // Exit
            }

        }
    }
}

推荐答案

这里是代码,你会用它来做 pptx4j.

Here is the code you'd use to do it with pptx4j.

您应该能够将其转换为等效的 POI 代码.

You ought to be able to convert that to equivalent POI code.

请注意,pptx4j 代码正在更新 OpenXML 电子表格,而您的代码针对的是旧二进制格式.

Note that the pptx4j code is updating an OpenXML spreadsheet, whereas your code is targeting the legacy binary format.

这篇关于无法在 pptx 中的 BarChart 中看到 Apache POI 更新的数据值而无需编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 06:16