C# 无法打开文档,因为存在具有意外内容类型的无效部分
使用演示文稿(PPTX文件)创建代码打开时出错。 我使用的代码如下所示:C# 无法打开文档,因为存在具有意外内容类型的无效部分,c#,asp.net,powerpoint,openxml-sdk,powerpoint-automation,C#,Asp.net,Powerpoint,Openxml Sdk,Powerpoint Automation,使用演示文稿(PPTX文件)创建代码打开时出错。 我使用的代码如下所示: public static void UpdatePPT() { const string presentationmlNamespace = "http://schemas.openxmlformats.org/presentationml/2006/main"; const string drawingmlNamespace = "http://schemas.openxml
public static void UpdatePPT()
{
const string presentationmlNamespace = "http://schemas.openxmlformats.org/presentationml/2006/main";
const string drawingmlNamespace = "http://schemas.openxmlformats.org/drawingml/2006/main";
string fileName = Server.MapPath("~/PPT1.pptx"); //path of pptx file
using (PresentationDocument pptPackage = PresentationDocument.Open(fileName, true))
{
} // Using pptPackage
}
我得到的错误是:
"The document cannot be opened because there is an invalid part with an unexpected content type.
[Part Uri=/ppt/printerSettings/printerSettings1.bin],
[Content Type=application/vnd.openxmlformats-officedocument.presentationml.printerSettings],
[Expected Content Type=application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings]."
使用(PresentationDocument pptPackage=PresentationDocument.Open(fileName,true))时出错
对于许多PPTX文件,该代码工作正常。但是它在一些文件上抛出了这个错误。
我找不到任何解决办法。
谢谢你的帮助 我终于解决了我的问题。我得到的PPTX是在mac os中开发的。所以我只是打开了一个工作的pptx文件。并将不工作pptx的所有内容复制到工作pptx
中,并以不工作pptx的名称保存 以前的帖子,但我遇到了同样的问题。我用程序解决了它。
指:
我的代码使用(var document=PresentationDocument.Open(fileName,true))运行
如果这遇到异常,我有一个文档,如下所述。然后我调用fixportpoint()
方法,然后再次执行其他操作
以下是共享的方法(使用System.IO.Packaging
):
我对这个问题也很感兴趣,我认为您试图打开的文件不是有效的open XML PresentationDocument。请查看错误文件,无论该文件是使用PowerPoint 2011还是PowerPoint 2010创建的。也许它可以为您的问题提供一些线索。@DeepakBhatia我可以在MS PowerPoint 20132010中打开这些文件,没有任何问题。DeepSharma,我真的认为您应该将@YvesR answer标记为一个。他是按程序修好的!!!我喜欢他的方法…谢谢你!在过去的几个月里,这个问题一直困扰着我,但我从未真正着手解决它。你的解决方案有效。
private static void FixPowerpoint(string fileName)
{
//Opening the package associated with file
using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite))
{
//Uri of the printer settings part
var binPartUri = new Uri("/ppt/printerSettings/printerSettings1.bin", UriKind.Relative);
if (wdPackage.PartExists(binPartUri))
{
//Uri of the presentation part which contains the relationship
var presPartUri = new Uri("/ppt/presentation.xml", UriKind.RelativeOrAbsolute);
var presPart = wdPackage.GetPart(presPartUri);
//Getting the relationship from the URI
var presentationPartRels =
presPart.GetRelationships().Where(a => a.RelationshipType.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings",
StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
if (presentationPartRels != null)
{
//Delete the relationship
presPart.DeleteRelationship(presentationPartRels.Id);
}
//Delete the part
wdPackage.DeletePart(binPartUri);
}
wdPackage.Close();
}
}