c# - 无法打开文档,因为存在具有意外内容类型的无效部分

标签 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.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]."

错误发生在 using (PresentationDocument pptPackage = PresentationDocument.Open(fileName, true))

代码适用于许多 PPTX 文件。但它在某些文件上抛出了这个错误。 我找不到任何解决方案。 谢谢你的帮助。

最佳答案

旧帖子,但我遇到了同样的问题。我以编程方式解决了它。 意思是:

我的代码运行 使用 (var document = PresentationDocument.Open(fileName, true)) 如果这遇到异常,我有一个描述的文件。然后我调用 FixPowerpoint() 方法并再次执行其他操作。

这里分享方法(使用System.IO.Packaging):

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();
  }
}

关于c# - 无法打开文档,因为存在具有意外内容类型的无效部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18910623/

相关文章:

c# - 使用 IN 子句的手动(动态)LINQ 子查询

c# - 从 C# 通信和控制网页

vba - VBA 中 msoTextOrientationHorizo​​ntal 的常量值是多少?

VBA 在将包对象 (XML) 读入 ADODB 流时防止键盘输入?

从网络驱动器启动的 c# .net4 应用程序需要很长时间才能加载

C# - 异步方法从 ContinueWith 返回数据

html - ASP.NET GroupBox 看起来像将字段集与 Bootstrap 一起使用

c# - 当所有设置均为最大值时,已超出传入邮件的最大邮件大小配额 (65536)

asp.net - 构建具有离线功能的 ASP.Net Web 应用程序

python-pptx 在布局中更改图片