[英]File.OpenWrite gives error while FileStream(sFile, FileMode.Open, FileAccess.ReadWrite) does not
[英]C# Azure File Storage CloudFile.OpenWrite issue with OpenXml.SpreadsheetDocument…need FileMode and FileAccess options?
我正在使用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 )
有谁知道如何解决这个问题? 还是解决方案?
请 :)
有谁知道如何解决这个问题? 还是解决方案?
_ 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
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.