2017-09-17 57 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

非常感谢!这么晚才回复很抱歉! –