使用 epplus 从 blob 触发 azure 函数访问 excel 文件
Posted
技术标签:
【中文标题】使用 epplus 从 blob 触发 azure 函数访问 excel 文件【英文标题】:Access excel file from blob trigger azure function using epplus 【发布时间】: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。
任何人,请建议如何做到这一点。
【问题讨论】:
以下内容可能会有所帮助:***.com/questions/68982086/… 可以参考How to read an excel file stored in an Azure Storage container as a Blob using Epplus package 【参考方案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());
更多信息请查看SO1 和SO2。
【讨论】:
我试过了,但收到错误“找不到有效的帐户信息组合”。我的连接字符串就像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=***",
以上是关于使用 epplus 从 blob 触发 azure 函数访问 excel 文件的主要内容,如果未能解决你的问题,请参考以下文章
Azure 函数从门户或 Visual Studio-Blob 触发器执行
Blob 存储上的 Azure 触发器,从图像 (Blob) 中提取 EXIF (lat/long/direction...) 数据