2017-09-17 30 views
1

我正在使用DocumentFormat.OpenXml.SpreadsheetDocument並打開一個Excel文檔模板,寫入並保存它。C#Azure文件存儲CloudFile.OpenWrite問題與OpenXml.SpreadsheetDocument ...需要FileMode和FileAccess選項?

它像魅力從一個正常的文件流:

using (var documentStream = System.IO.File.Open("--somePath--", FileMode.Open, FileAccess.ReadWrite)) 
{ 
    using (var document = SpreadsheetDocument.Open(documentStream, true)) 
    { 
     // do something 
    } 
} 

通知的SpreadsheetDocument.Open

現在,我重寫這個應用程序Azure以及使用Azure存儲,它的。 「WindowsAzure.Storage」包中的NET文件庫。

它的工作原理就像一個魅力,所有這一切都要在Azure中填充相同的Excel文件。

using (var documentStream = _GetRootDirectoryOfAccount().GetFileReference("--someRelativePath--").OpenWrite(null)) 
{ 
    using (var document = SpreadsheetDocument.Open(documentStream, true)) 
    { 
     // do something 
    } 
} 

第一部分 「_GetRootDirectoryOfAccount()。GetFileReference」 工程100%,然後OpenWrite(空)真正開啓流。

然而,當流是對電子表格推:

SpreadsheetDocument.Open(documentStream, true) 

它打破了:

System.IO.IOException:「無法打開包,因爲的FileMode或 的FileAccess值無效爲這條河流。「

而且這是因爲在數據流中的設置不設置:

System.IO.File.Open( 「 - somePath--」 FileMode.Open,FileAccess.ReadWrite

有誰知道如何解決這個問題?還是解決方案?

請:)

回答

0

有誰知道如何解決這個問題?還是解決方案?

的_ GetRootDirectoryOfAccount().GetFileReference("--someRelativePath--").OpenWrite(null))返回類型是CloudFileStream `

似乎CloudFileStreamSpreadsheetDocument.Open()支持

請嘗試使用下面的代碼,它在我身邊正常工作。更新內容後,我們可以使用file.UploadFromFile()或file.UploadFromStream()來上傳文件。

var file = _GetRootDirectoryOfAccount().GetFileReference("--someRelativePath--"); 
var memoryStream = new MemoryStream(); 
file.DownloadToStream(memoryStream); 
using (var document = SpreadsheetDocument.Open(memoryStream, true)) 
{ 
    // do something 
} 

以下是我的演示代碼。

var connectionString = "DefaultEndpointsProtocol=https;AccountName=accountName;AccountKey=xxxxx;EndpointSuffix=core.windows.net"; 
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); 
CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); 

// Get a reference to the file share we created previously. 
CloudFileShare share = fileClient.GetShareReference("test"); //share name 

if (share.Exists()) 
{ 
    // Get a reference to the root directory for the share. 
    CloudFileDirectory rootDir = share.GetRootDirectoryReference(); 

    // Get a reference to the directory we created previously. 
    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("custom"); 
    // Ensure that the directory exists. 
    if (sampleDir.Exists()) 
    { 
     // Get a reference to the file we created previously. 
     var file = sampleDir.GetFileReference("OpenXMl.xlsx"); //file name 

     // Ensure that the file exists. 
     if (file.Exists()) 
     { 
      // Write the contents of the file to the console window. 
      Console.WriteLine(file.DownloadTextAsync().Result); 
      var memoryStream = new MemoryStream(); 
      file.DownloadToStream(memoryStream); 
      using (var document = SpreadsheetDocument.Open(memoryStream, true)) 
      { 
       // do something 
      } 
     } 
    } 

} 
+0

非常感謝!這麼晚纔回復很抱歉! –