【问题标题】:How can I create a csv file on blob storage azure with Object c# .NET Core?如何使用 Object c# .NET Core 在 blob 存储 Azure 上创建 csv 文件?
【发布时间】:2021-11-07 03:18:11
【问题描述】:

我正在尝试从对象创建 CSV 文件并将其上传到 blob 存储。我还希望该列与对象中的属性具有相同的名称。 (.NET Core 5.0/C#)

【问题讨论】:

    标签: c# csv asp.net-core blob azure-storage


    【解决方案1】:

    请使用以下代码:

        using Microsoft.WindowsAzure.Storage;
        using Microsoft.WindowsAzure.Storage.Blob;
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Threading.Tasks;
    
        namespace TestKeyVault.Services
        {
    
        public class Sample
        { 
            public string FirstName { get; set; }
            public int Id { get; set; }
            public string LastName { get; set; }
    
        }
    
        public class Demo
        {
            public async Task startAsync()
            {
                var Friends = new List<Sample> { 
                    new Sample { FirstName="Naman",LastName="Sinha",Id = 1},
                    new Sample {FirstName="Naman",LastName="Sinha",Id = 2},  
                    new Sample {FirstName = "Naman", LastName = "Sinha", Id = 3}
                };
    
                string properties = string.Join(",", Friends[0].GetType().GetProperties().Select(i => i.Name));
    
                var info = from member in Friends
                           let record = string.Join(",", member.GetType().GetProperties().Select(j => j.GetValue(member)))
                           select record;
    
                var csv = new List<string>
                {
                    properties
                };
    
                csv.AddRange(info);
    
                string newPath = Directory.GetCurrentDirectory() + "/Data";
    
                // Determine whether the directory exists.  
                if (Directory.Exists(newPath))
                    Directory.Delete(newPath, true);
    
                Directory.CreateDirectory(newPath);
    
                File.WriteAllLines(newPath + "/FileToBeUploaded.csv", csv);
    
                try
                {
    
                    CloudStorageAccount account = CloudStorageAccount.Parse("YOUR_STORAGE_ACCOUNT_CONNECTION_STRING");
    
                    CloudBlobClient client = account.CreateCloudBlobClient();
    
                    CloudBlobContainer container = client.GetContainerReference("YOUR_CONTAINER_NAME");
    
                    await container.CreateIfNotExistsAsync();
    
                    CloudBlockBlob blob = container.GetBlockBlobReference("FileToBeUploaded.csv");
    
                    await blob.UploadFromFileAsync(newPath);
    
                    Console.WriteLine("File Uploaded!");
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine("File could not be downloaded due to: "+ex.Message);
                }
    
            }
        }
    
    }
    

    代码从示例对象创建一个 CSV 文件并从中生成一个 .csv 文件,然后将其作为 blob 上传到指定存储帐户的容器中。 您只需要更改类和命名空间,并指定 Azure 存储帐户的连接字符串和容器名称。

    【讨论】:

    • 感谢您的回答。你说的工作,但不是我想要的方式,我想让每个值在不同的列中。示例:在 A 列中,只有 FirstName 及其值如下。
    • 不要回答这样不费力的问题。
    • 这对您来说可能很费力,但对于行业初学者和使用该平台的其他语言新手来说可能不是。
    猜你喜欢
    • 2021-12-18
    • 1970-01-01
    • 2021-12-12
    • 2020-11-02
    • 2021-07-24
    • 2014-01-06
    • 2011-06-11
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多