我使用c#将文件上传到Azure Blob。 现在我想使用ExcelDataReader读取上传的文件。
我正在使用以下代码。 其中 _imageRootPathNos 是保存文件的路径(http://imor.blob.core.windows.net/files)
FileStream stream = System.IO.File.Open(_imageRootPathNos + "/" + "ImEx.xlsx", FileMode.Open, FileAccess.Read);
我收到错误System.ArgumentException:'不支持URI格式。'
我错过了什么?
答案 0 :(得分:0)
使用Azure存储服务时,建议您使用Azure .NET SDK。 SDK公开了下载,上载和管理容器和blob存储的适当方法。在这种情况下,您的代码应如下所示:
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("files");
// Retrieve reference to a blob named "imex.xlsx".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("Imex.xlsx");
// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
{
blockBlob.DownloadToStream(fileStream);
}
您可以在此处找到有关如何使用SDK的所有信息:https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-blobs
答案 1 :(得分:0)
我使用这段代码将excel文件(上传到azure上)读入数据集
Uri blobUri = new Uri(_imageRootPath + "/" + fileName);
var wc = new WebClient();
var sourceStream = wc.DownloadData(blobUri);
Stream memoryStream = new MemoryStream(sourceStream);
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(memoryStream);
DataSet dsResult = excelReader.AsDataSet();
return dsResult;