2017-09-17 16 views
1

저는 DocumentFormat.OpenXml.SpreadsheetDocument로 작업하고 Excel 문서의 템플릿을 열어서 쓰고 저장합니다.C# Azure 파일 저장 OpenXml.SpreadsheetDocument의 CloudFile.OpenWrite 문제 ... 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

는 지금, 나는 푸른이 응용 프로그램을 다시 작성하고, 푸른 스토리지를 사용하고 그건 있어요. "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 (null가) 정말 스트림을 엽니 다. 이 스트림은 스프레드 시트쪽으로 밀어 그러나

:

SpreadsheetDocument.Open(documentStream, true) 

그것은 함께 나누기 '수는하는 FileMode 또는 FileAccess 값이 유효하지 않습니다 금지 패키지 때문에 :

System.IO.IOException 스트림을 위해. '

그리고 스트림의 설정이 설정되지 않은 때문입니다

System.IO.File.Open ("- somePath--"FileMode.Open, FileAccess.ReadWrite)

누구든지이 문제를 해결하는 방법을 알고 있습니까? 아니면 해결책?

:)하십시오

답변

0

사람이 주위에 얻는 방법을 알고 있나요? 아니면 해결책?

_ GetRootDirectoryOfAccount().GetFileReference("--someRelativePath--").OpenWrite(null))의 반환 유형은 CloudFileStream `

입니다 CloudFileStream하지 SpreadsheetDocument.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

대단히 감사합니다. 늦은 답변 죄송합니다! –