使用Open XML SDK读取上传的pptx文件

时间:2015-06-08 20:18:25

标签: c# asp.net vb.net visual-studio-2013 openxml-sdk

我正在尝试构建一个可以上传.pptx文件的网站,并会检查一些方面(例如幻灯片计数)。我有Open XML SDK 2.5,我添加了对它的引用。然而,当我尝试使用SDK中的命令时,它们不可用。

(另外,我真的需要一个内存流来处理文件吗?我不想存储文件,只是从中提取一些细节。)

我的代码在VB中,但我也可以阅读C#:

Partial Class uploadpresentation
 Inherits System.Web.UI.Page

 Protected Sub Upload_Click(sender As Object, e As EventArgs) Handles Upload.Click
    If PptxUpload.HasFile AndAlso PptxUpload.FileName.EndsWith(".pptx") Then
        ProcessFile(PptxUpload.PostedFile)
    End If
 End Sub

 Sub ProcessFile(ByVal pptxFile As HttpPostedFile)

    using (MemoryStream memoryStream = new MemoryStream())

    Using doc As PresentationDocument = PresentationDocument.Open(fileName, False)

    outputMessage = "Number of slides: " & argh

 End Sub
End Class

我的Web.config有这个:

  <assemblies>
    <add assembly="DocumentFormat.OpenXml, Version=2.5.5631.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>

因此,无法识别MemoryStream和PresentationDocument。我在这里缺少什么?

非常感谢!

2 个答案:

答案 0 :(得分:2)

上传的文件以流形式发送,直到您实际将其写入某个位置。您不会在问题中提供的代码中执行此操作。您只需将HttpPostedFile的stream属性传递给Open方法。

以下是您应该如何获得幻灯片计数(C#):

public void ProcessFile(HttpPostedFile pptxFile)
{
    int slideCount = 0;

    using (var doc = PresentationDocument.Open(pptxFile.InputStream, false))
    {
        PresentationPart presentationPart = doc.PresentationPart;
        slideCount = presentationPart.SlideParts.Count();
    }
}

有关详细信息,请参阅the documentation有关检索幻灯片计数的信息。

答案 1 :(得分:1)

Wered问题,但是, 你有添加导入部分吗?

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml;

您是使用gac中的引用还是将OpenXml dll存储为文件?