C#Azure文件存储CloudFile.OpenWrite与OpenXml.SpreadsheetDocument问题…需要FileMode和FileAccess选项?
我正在处理DocumentFormat.OpenXml.SpreadsheetDocument并打开Excel文档的模板,写入并保存它。
它像一个正常的文件stream的魅力:
using (var documentStream = System.IO.File.Open("--somePath--", FileMode.Open, FileAccess.ReadWrite)) { using (var document = SpreadsheetDocument.Open(documentStream, true)) { // do something } }
注意SpreadsheetDocument.Open
现在,我正在将此应用程序重写为Azure,并在“WindowsAzure.Storage”包中使用Azure存储和.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。
但是,当该stream被推向电子表格时:
SpreadsheetDocument.Open(documentStream, true)
它打破了:
System.IO.IOException:'无法打开包,因为FileMode或FileAccess值对stream无效。
这是因为在stream上的设置没有设置:
System.IO.File.Open(“ – somePath–”, FileMode.Open,FileAccess.ReadWrite )
有谁知道如何解决这个问题? 还是解决scheme?
请 :)
有谁知道如何解决这个问题? 还是解决scheme?
_ GetRootDirectoryOfAccount().GetFileReference("--someRelativePath--").OpenWrite(null))
的返回types是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 } } } }
- Windows Azure远程站点“Microsoft.ACE.OLEDB.12.0”提供程序未在本地计算机上注册
- Server.MapPath访问被拒绝在Azure上
- 从Excel中读取存储在Azure Blob存储中的.csv
- 从Azure下载的Excel文件格式不正确
- 如果连接到Azure的Excel工具保存在OneDrive上,我的团队是否必须连接到ODBC?
- 将数据从Azure SQL数据库通过参数化的存储过程导入到Excel中
- VBA / .Net通过Windows安全从受密码保护的网站下载文件
- 尝试从Mac Excel 2016连接到Azure SQL Server时发生ODBC“无效连接string”
- 如何将Azure机器学习输出导出到CSV