我有一张带有一张图表的 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();
}
}
}
}
}