C# 需要通过oledb连接从azure存储blob容器读取excel文件

C# 需要通过oledb连接从azure存储blob容器读取excel文件,c#,asp.net,azure,azure-web-app-service,azure-storage-blobs,C#,Asp.net,Azure,Azure Web App Service,Azure Storage Blobs,我已将我的网站托管到azure中…我已通过中间服务器将Excel文件上载到azure存储blob容器中,因为我无法直接从网站访问azure。。因此,我需要的是,我需要使用“oledb连接”从azure存储blob容器(或)中间服务器本地路径读取excel文件,并将其存储到数据表中以验证excel。因此,任何人都可以帮助我进一步操作。公共字符串[]GetExcelSheets(字符串文件名) public string[] GetExcelSheets(string filename)

我已将我的网站托管到azure中…我已通过中间服务器将Excel文件上载到azure存储blob容器中,因为我无法直接从网站访问azure。。因此,我需要的是,我需要使用“oledb连接”从azure存储blob容器(或)中间服务器本地路径读取excel文件,并将其存储到数据表中以验证excel。因此,任何人都可以帮助我进一步操作。

公共字符串[]GetExcelSheets(字符串文件名)
public string[] GetExcelSheets(string filename)
        {
            String[] excelSheets = null;
            StorageCredentials creds = new StorageCredentials("<accountname>", "<key>");
            CloudStorageAccount account = new CloudStorageAccount(creds, useHttps: true);
            CloudBlobClient blobClient = account.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference("documents");
            blobContainer.CreateIfNotExists();
           blobContainer.SetPermissions(new BlobContainerPermissions
{  PublicAccess = BlobContainerPublicAccessType.Blob
            });
            CloudBlockBlob blob1 = blobContainer.GetBlockBlobReference(filename);
            try
            {
                using (var stream = blob1.OpenRead())
                {
                    OleDbConnection connection = new OleDbConnection();
                    var localPath = @"https://xxyyyyyyyyyyy.blob.core.windows.net/";
                    var fileName = @"C:\xxx" + @"\" + "9370.XLS";
                    var fullPathToFile = System.IO.Path.Combine(localPath, fileName);
                    string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + fileName + "';Extended Properties=Excel 8.0";
                   OleDbConnection oledbConn = new OleDbConnection(connString);
                    DataTable dt = new DataTable();
                    if (oledbConn.State == ConnectionState.Closed) oledbConn.Open();
                    // Get the data table containg the schema guid.
                    dt = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    oledbConn.Close();
                    if (dt == null) return null;
                    excelSheets = new String[dt.Rows.Count];
                    int i = 0;
                    // Add the sheet name to the string array.
                    foreach (DataRow row in dt.Rows)
                    {
                        excel Sheets[i] = row["TABLE_NAME"].ToString();
                        i++;
                    }
                }
            }
            catch (Exception ex)
            {
                HTTP Context.Current.Res`enter code here`ponse.Write(ex.Message);
            }
            return excel Sheets; }
this is the code i used to read the excel file but oledb data-source path is incorrect...can u suggest what path i should use to read the excel file in azure blob container 
{ 字符串[]excelSheets=null; StorageCredentials creds=新的StorageCredentials(“,”); CloudStorageAccount=新的CloudStorageAccount(creds,useHttps:true); CloudBlobClient blobClient=account.CreateCloudBlobClient(); CloudBlobContainer blobContainer=blobClient.GetContainerReference(“文档”); blobContainer.CreateIfNotExists(); 设置权限(新的BlobContainerPermissions {PublicAccess=BlobContainerPublicAccessType.Blob }); CloudBlockBlob blob1=blobContainer.GetBlockBlobReference(文件名); 尝试 { 使用(var stream=blob1.OpenRead()) { OleDbConnection connection=新的OleDbConnection(); var localPath=@”https://xxyyyyyyyyyyy.blob.core.windows.net/"; 变量文件名=@“C:\xxx”+@“\”+“9370.XLS”; var fullPathToFile=System.IO.Path.Combine(localPath,fileName); string connString=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=”“+fileName+”;扩展属性=Excel 8.0”; OleDbConnection oledbConn=新的OleDbConnection(连接字符串); DataTable dt=新的DataTable(); if(oledbConn.State==ConnectionState.Closed)oledbConn.Open(); //获取包含架构guid的数据表。 dt=oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null); oledbConn.Close(); 如果(dt==null)返回null; excelSheets=新字符串[dt.Rows.Count]; int i=0; //将工作表名称添加到字符串数组中。 foreach(数据行中的数据行) { excel工作表[i]=行[“表格名称”]。ToString(); i++; } } } 捕获(例外情况除外) { HTTP Context.Current.Res`在此处输入代码`ponse.Write(例如Message); } 返回excel工作表;} 这是我用来读取excel文件的代码,但是oledb数据源路径不正确…你能建议我应该使用什么路径来读取azure blob容器中的excel文件吗
您从未编辑您的问题以显示代码,并且您作为答案错误发布的代码已被删除,因此我在这里进行猜测,但是。。。我很确定问题是您正试图通过
OleDbConnection
以文件形式打开blob

Azure存储Blob无法作为文件打开。如果要像文件一样使用blob的内容,首先需要下载它并将其保存为文件(或者使用内存流,但在您的情况下,您需要一个文件)。然后你可以像平常一样打开文件

由于您在基于Azure的Web应用程序中运行代码,因此理想情况下,您希望Web应用程序与存储帐户位于同一区域。您将下载到web应用程序的本地磁盘(web应用程序为您提供本地存储以供使用)


操作完文件后,您需要将其上载回blob。

我建议包含一些代码,以便其他人更容易重建您的问题。@kavitha-我强烈建议您立即重新生成存储帐户密钥,因为您已在代码中发布了它(意味着整个世界现在都可以访问你的存储帐户)。你永远不应该这样做。另外:编辑你的问题-不要更新答案中的信息。@kavitha-即使你编辑了这篇文章(我也编辑过),历史记录仍然存在,任何有足够声誉的人都可以看到所有历史记录,仍然可以看到您的密钥。您需要删除此答案并更改存储帐户密钥。@david:谢谢您提供的信息。我已重新生成存储帐户key@david:你能建议我如何直接从azure存储blob中读取excel文件吗tainer。这意味着以32位运行应用程序。注意:正如你提到的,使用内存流,我已经使用downloadtostream()将文件下载到本地磁盘上了;注意:1)你能详细解释一下这一点吗?“你将下载到web应用程序的本地磁盘上(web应用程序为你提供本地存储空间)。”,使用内存流我已经使用downloadtostream()将文件下载到本地磁盘;但是它无法识别路径…3)如果我需要上传回blob,这意味着它们在缺少内存空间的情况下会有任何可能性…4)我无法为问题添加代码。,。