【问题标题】:C# Azure File Storage CloudFile.OpenWrite issue with OpenXml.SpreadsheetDocument...need FileMode and FileAccess options?C# Azure 文件存储 CloudFile.OpenWrite 问题与 OpenXml.SpreadsheetDocument...需要 FileMode 和 FileAccess 选项?
【发布时间】:2017-09-17 21:33:40
【问题描述】:

我正在使用 DocumentFormat.OpenXml.SpreadsheetDocument 并打开 Excel 文档的模板,写入并保存它。

它就像普通文件流中的魅力一样工作:

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(null)真正打开了一个Stream。

但是,当该流被推向电子表格时:

SpreadsheetDocument.Open(documentStream, true)

它打破了:

System.IO.IOException: '无法打开包,因为 FileMode 或 FileAccess 值对流无效。'

这是因为在 Stream 上没有设置设置:

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

有谁知道如何解决这个问题?还是解决方案?

请:)

【问题讨论】:

    标签: c# excel azure openxml-sdk azure-storage-files


    【解决方案1】:

    有谁知道如何解决这个问题?还是解决方案?

    _GetRootDirectoryOfAccount().GetFileReference("--someRelativePath--").OpenWrite(null))的返回类型是CloudFileStream `

    SpreadsheetDocument.Open() 似乎不支持CloudFileStream

    请尝试使用以下代码,它在我这边可以正常工作。更新内容后,我们可以使用 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
                }
            }
         }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 2021-11-19
      相关资源
      最近更新 更多