1

我有一张带有一张图表的 Excel 表格,它是来源。我的目标是带有 3 张幻灯片的 powerpoint 演示文稿。我需要用 Excel 文件中的图表更新幻灯片 #3。

执行应用程序后,当我尝试打开 pptx 文件时,我收到“Powerpoint 发现内容有问题”。修复后,我注意到幻灯片 #2 中总是出现空白幻灯片,这表明我没有更新正确的幻灯片。

我应该怎么做才能根据幻灯片编号转到幻灯片。(我在 powerpoint 幻灯片和 excel 图表中没有图表标题)以及为什么我收到无效内容错误。

非常感谢您的帮助。谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OpenXmlPkg = DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace ExportChart
{
class Program
{
    static void Main(string[] args)
    {
        string SourceFile = "Projected Sales.xlsx";
        string TargetFile = "Projected Sales.pptx";
        string targetppt = "Generatedppt.pptx";

        ChartPart chartPart;
        ChartPart newChartPart;
        SlidePart slidepartbkMark = null;
        string chartPartIdBookMark = null;

        File.Copy(TargetFile, targetppt, true);

        //Powerpoint document 
        using (OpenXmlPkg.PresentationDocument pptPackage = OpenXmlPkg.PresentationDocument.Open(targetppt, true))
        {

            OpenXmlPkg.PresentationPart presentationPart = pptPackage.PresentationPart;

            var secondSlidePart = pptPackage.PresentationPart.SlideParts.Skip(1).Take(1);
            foreach (var slidepart in pptPackage.PresentationPart.SlideParts)
            {

            slidepartbkMark = slidepart;

                if (slidepart.GetPartsCountOfType<ChartPart>() != 0)
                {
                    chartPart = slidepart.ChartParts.First();
                    chartPartIdBookMark = slidepart.GetIdOfPart(chartPart);
                    slidepart.DeletePart(chartPart);
                    slidepart.Slide.Save();
                }

                //return;
            }

            newChartPart = slidepartbkMark.AddNewPart<ChartPart>(chartPartIdBookMark);

            ChartPart saveXlsChart = null;
            using (SpreadsheetDocument xlsDocument = SpreadsheetDocument.Open(SourceFile.ToString(), true))
            {
                WorkbookPart xlsbookpart = xlsDocument.WorkbookPart;

                foreach (var worksheetPart in xlsDocument.WorkbookPart.WorksheetParts)
                {
                    if (worksheetPart.DrawingsPart != null)
                        if (worksheetPart.DrawingsPart.ChartParts.Any())
                        {
                            saveXlsChart = worksheetPart.DrawingsPart.ChartParts.First();
                        }
                }


                newChartPart.FeedData(saveXlsChart.GetStream());
                slidepartbkMark.Slide.Save();
                xlsDocument.Close();
                pptPackage.Close();
            }
        }
    }
}     

}

4

1 回答 1

2

如果有人正在寻找如何识别带有幻灯片编号的幻灯片,您需要执行以下操作

//Get the relationship id 
SlideIdList s = documentPart.Presentation.SlideIdList;
SlideId nslideid = (SlideId)s.ElementAt(slideno-1); //slide no is the number of the slide
string slidRelId = nslideid.RelationshipId;

接下来使用循环遍历每张幻灯片。在 foreach 循环中添加以下代码以查看幻灯片是否具有特定的关系 id

//  get the relationshipid of the current slide 
    string thisSlideno = documentPart.GetIdOfPart(slidePart);
    if (thisSlideno == slidRelId)
    {
        //do your actions
    }

我希望这是有帮助的

于 2012-04-30T12:46:09.027 回答