【问题标题】:Access excel file from blob trigger azure function using epplus使用 epplus 从 blob 触发 azure 函数访问 excel 文件
【发布时间】:2021-12-10 22:18:54
【问题描述】:

我创建了一个 blob 触发 azure 函数,每当将新文件(在我的情况下为 excel 文件)添加到 blob 存储时都会调用该函数。谁能建议如何从blob存储中获取文件数据并使用epplus将其转换为excel。

public void Run([BlobTrigger("myblobcontainer/{name}", Connection = "AzureStorage")]CloudBlockBlob myBlob, string name, ILogger log)
{
 try
 {
  ProcessData(myBlob,name);
 }
 catch (Exception ex)
 {
 }
}

public int ProcessData(CloudBlockBlob myBlob, string name)
{
   CloudStorageAccount IMAccount;
   IMAccount = CloudStorageAccount.Parse("my azure storage connection");

   var blobReference= //Get the path of file in blob

  //Basically here I want to read 'myBlob' and convert it back into excel
}

我有点困惑如何在 blob 中获取文件的路径(在 var blobReference 中)并使用 epplus 将其转换为 excel。 任何人,请建议如何做到这一点。

【问题讨论】:

标签: c# azure-functions blob export-to-excel epplus


【解决方案1】:

以下示例代码将帮助您将我的 excel 数据从集合填充到您的 blob 容器并创建一个新的CloudBlockBlob

[FunctionName("WriteExcelToBlob")]
public async Task Run(
    [TimerTrigger("*/30 * * * * *")] TimerInfo timer,
    [Blob("excelFiles", FileAccess.Write, Connection = "Storage")] CloudBlobContainer blobContainer,
    ILogger log
)
{
    var fileNameSuffix = DateTime.Now.ToString("yyyyMMdd_HHmmss");

    var myCollection = new List<MyObject>();

    var newBlobName = $"myFile_{fileNameSuffix}.xlsx";
    var newBlob = blobContainer.GetBlockBlobReference(newBlobName);

    using (var excel = new ExcelPackage())
    {
        var worksheet = excel.Workbook.Worksheets.Add("My Worksheet");
        worksheet.Cells.LoadFromCollection(myCollection);

        using (var stream = await newBlob.OpenWriteAsync())
        {
            excel.SaveAs(stream);
        }
    }
}

下面是使用类的例子

using System.ComponentModel;

public class MyObject
{
    [Description("Name")]
    public string Name { get; set; }
    [Description("Home Address")]
    public string HomeAddress { get; set; }
}

链接 EPPlus custom header column names 将显示如何在输出 excel 中为 System.ComponentModel.Description 获取标题

正如DeepDave-MT 建议的那样,我们不能直接从 Blob 读取 excel,我们需要下载它,下面是下载它的示例代码。

string connectionString = "";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("test");
            BlobClient blobClient = containerClient.GetBlobClient("sample.xlsx");
            
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            using (var stream = await blobClient.OpenReadAsync(new BlobOpenReadOptions(true)))
            using (ExcelPackage package = new ExcelPackage(stream))
            {
                //get the first worksheet in the workbook
                ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
                int colCount = worksheet.Dimension.End.Column;  //get Column Count
                int rowCount = worksheet.Dimension.End.Row;     //get row count
                for (int row = 1; row <= rowCount; row++)
                {
                    for (int col = 1; col <= colCount; col++)
                    {
                        Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
                    }
                }
               
            }




        }

更多信息请查看SO1SO2

【讨论】:

  • 我试过了,但收到错误“找不到有效的帐户信息组合”。我的连接字符串就像 CloudStorageAccount StorageConn; CloudBlobClient BlobClient; StorageConn = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("MyConn")); BlobClient = StorageConn.CreateCloudBlobClient(); BlobServiceClient blobServiceClient = new BlobServiceClient(StorageConn.ToString()); 在 local.settings.json 中是:"Values": { "MyConn": "DefaultEndpointsProtocol=https;AccountName=***;AccountKey=***", }
猜你喜欢
  • 2018-06-08
  • 1970-01-01
  • 2019-01-02
  • 2021-12-26
  • 2021-01-17
  • 2023-03-22
  • 1970-01-01
  • 2021-06-01
  • 2021-10-08
相关资源
最近更新 更多