从 .ppt 转换 powerpoint 文件 -> .pptx 通过使用 c#.net 2008 使用比特流

发布于 2024-09-12 09:47:31 字数 2625 浏览 6 评论 0原文

我正在尝试使用 linq 使用 sql 数据库中的二进制数据创建并打开一个 powerpoint。

A.首先,我将其读入字节数组,然后创建 .ppt 文件。

public bool createPresentation(string fileName, byte[] powerPoint)
    {
        DirectoryInfo di = new DirectoryInfo(downloadPath);
        if (!di.Exists)
            di.Create();

        fileName = string.Concat(downloadPath, fileName,".PPT");
        //Define a new instance of FileStream
        FileStream powerpointStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
       powerpointStream.Write(powerPoint, 0, powerPoint.Count());
       powerpointStream.Close();

        return True; 
    }

B.然后我尝试打开 .ppt 文件并将其另存为 .pptx 文件

public bool convertPPTtoPPTX(string path)
    {
        string source = path;
        string destination = path.Replace("PPT", "PPTX");

        DirectoryInfo di = new DirectoryInfo(downloadPathPPTX);
        if (!di.Exists)
            di.Create();

        PowerPoint.Application app = new PowerPoint.Application();//Line Y

        PowerPoint.Presentation pptx = app.Presentations.Open(source, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);//Line Z
        pptx.SaveAs(destination, PowerPoint.PpSaveAsFileType.ppSaveAsDefault);
        pptx.Close();
        app.Quit();

       return true;
    }

C.最后,我尝试将 .pptx 文件读入字节数组,以便通过 linq 更新数据库。

    public byte[] convertToBinary(string source)
    {
        byte[] binary = File.ReadAllBytes(source);
        return binary;
    }

E。这就是我通过linq-sql获取二进制数据的方式

public List<Template> getPPTFileBiniary(int ID)
    {
        var ppt = from p in db.paPresentationTemplates
                  where p.ID==ID
                  select new Template { pptFile = p.PPTFile.ToArray() };

        return ppt.ToList();
    }

F. E 中使用的模板类

class Template
{
    public int ID { get; set; }
    public string  FileName { get; set; }
    public Byte[] pptFile { get; set; }

    public Template()
    { 

    }

}

我有几个与此相关的问题。

  1. 对于以下字节流,我收到一个错误,指出:“PowerPoint 无法打开该文件。”来自B部分Z线。 字节数据:“0x00000000000000000000” 这是为什么?
  2. 对于某些运行时实例,B 部分 Y 行再次引发以下异常。 “由于以下错误,从 IClassFactory 创建 CLSID {91493441-5A91-11CF-8700-00AA0060263B} 的 COM 组件实例失败:80010108”。但是当我使用F11键调试时,不会抛出此异常。有人可以解释一下吗?
  3. 此外,在某些情况下,当调用 B 部分时,会引发异常,指出“PowerPoint 文件正在被另一个程序/应用程序使用”。当 powerpoint 甚至没有在我的任务管理器进程中运行时。

请帮助我克服这些障碍。 谢谢, 亚辛杜。

I'm trying to create and open a powerpoint by using binary data from a sql database by using linq.

A. First I'm reading it into a byte array and then creating the .ppt file.

public bool createPresentation(string fileName, byte[] powerPoint)
    {
        DirectoryInfo di = new DirectoryInfo(downloadPath);
        if (!di.Exists)
            di.Create();

        fileName = string.Concat(downloadPath, fileName,".PPT");
        //Define a new instance of FileStream
        FileStream powerpointStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
       powerpointStream.Write(powerPoint, 0, powerPoint.Count());
       powerpointStream.Close();

        return True; 
    }

B. Then I'm trying to open the .ppt file and save it as a .pptx file

public bool convertPPTtoPPTX(string path)
    {
        string source = path;
        string destination = path.Replace("PPT", "PPTX");

        DirectoryInfo di = new DirectoryInfo(downloadPathPPTX);
        if (!di.Exists)
            di.Create();

        PowerPoint.Application app = new PowerPoint.Application();//Line Y

        PowerPoint.Presentation pptx = app.Presentations.Open(source, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoFalse);//Line Z
        pptx.SaveAs(destination, PowerPoint.PpSaveAsFileType.ppSaveAsDefault);
        pptx.Close();
        app.Quit();

       return true;
    }

C. Finally I'm trying to read the .pptx file into a byte array inorder to update the db through linq.

    public byte[] convertToBinary(string source)
    {
        byte[] binary = File.ReadAllBytes(source);
        return binary;
    }

E. This is how i obtain the binary data through linq-sql

public List<Template> getPPTFileBiniary(int ID)
    {
        var ppt = from p in db.paPresentationTemplates
                  where p.ID==ID
                  select new Template { pptFile = p.PPTFile.ToArray() };

        return ppt.ToList();
    }

F. Template class used in E

class Template
{
    public int ID { get; set; }
    public string  FileName { get; set; }
    public Byte[] pptFile { get; set; }

    public Template()
    { 

    }

}

I've got several issues regarding this.

  1. For the following byte stream, I get a error thrown, stating: "PowerPoint could not open the file." from part B Line Z.
    byte data: "0x00000000000000000000"
    Why is that?
  2. For some runtime instances the following exception is thrown again from Part B Line Y.
    "Creating an instance of the COM component with CLSID {91493441-5A91-11CF-8700-00AA0060263B} from the IClassFactory failed due to the following error: 80010108". But when I debug using F11 key, this exception is not thrown. Can someone please explain this?
  3. Also for some instances when calling part B, an exception is thrown which states "the powerpoint file is being used by another program/application." When powerpoint is not even running in my taskmanager processes.

Please help me to overcome these barriers.
Thanks,
Yasindu.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2024-09-19 09:47:31

我找到了问题第一部分的原因以及第二个问题的解决方案。

问题一:
出现这种情况是因为保存的 ppt 文件位流表示文件已损坏。因此一旦创建就无法打开。

问题2:
当我总是尝试在循环内创建新的应用程序实例时,会发生错误。
所以,
1. 我在类的顶部创建了实例并禁用了 app.Quit() 方法调用。
2. 关闭 Power Point 对象后,我通过将其等于 Null 来确保该对象已被销毁。(pptx = null;)

Q3 对我来说仍然是一个疑问,并且将感谢任何专业知识的帮助。

I found out the reason for the 1st part of my question as well as a solution for my 2nd question.

Q1:
This occurs because the saved bit stream of the ppt file represents a corrupted file. Therefore once created it cannot be opened.

Q2:
The error happens when I’m always trying to create a new application instance inside the loop.
Therefore,
1. I created the instance at the top of my class and disabled the app.Quit() method call.
2. After closing the power point object, I made sure that the object was destroyed by equaling it to Null.(pptx = null;)

Q3 is still a doubt for me and would be grateful for any expertise help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文