2010-08-02 90 views
1

我想通過使用linq使用二進制數據從sql數據庫創建並打開一個powerpoint。使用c#.net轉換一個PowerPoint文件.ppt - > .pptx使用c#.net 2008

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.模板類在電子商務

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

    public Template() 
    { 

    } 

} 

我用得到的二進制數據對此有幾個問題。

  1. 對於下面的字節流,我得到一個錯誤,說明:「PowerPoint無法打開文件。」從B部分Line Z。 字節數據:「0x00000000000000000000」 這是爲什麼?
  2. 對於某些運行時實例,從B部分行Y再次拋出以下異常。 「從IClassFactory創建具有CLSID的COM組件實例{91493441-5A91-11CF-8700-00AA0060263B}由於以下錯誤而失敗:80010108」。但是當我使用F11鍵進行調試時,不會拋出此異常。有人可以解釋這個嗎?
  3. 對於某些調用B部分的實例,還會拋出一個異常,其中聲明「該powerpoint文件正在被另一個程序/應用程序使用」。當我的任務管理器流程中甚至沒有運行Powerpoint時。

請幫我克服這些障礙。 謝謝, Yasindu。

回答

1

我發現了我的問題的第一部分的原因以及第二個問題的解決方案。

Q1:
發生這種情況是因爲保存的ppt文件的位流表示損壞的文件。所以一旦創建它就無法打開。

Q2: 當我總是試圖在循環內創建一個新的應用程序實例時發生錯誤。 因此, 1.我在我的類的頂部創建了實例,並禁用了app.Quit()方法調用。 2.關閉電源點對象後,我確認對象被等同於Null而被銷燬。(pptx = null;)

Q3對我來說仍然是一個疑問,對任何專業知識的幫助都會感激不盡。