![](/img/trans.png)
[英]Accessing a file located in Azure Blob, from Azure Timer Trigger Function
[英]Access excel file from blob trigger azure function using epplus
我创建了一个 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 感到有些困惑。 任何人,请建议如何做到这一点。
下面的示例代码将帮助您将我的 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 自定义标题列名称将显示如何获取System.ComponentModel.Description
的输出 excel 中的标题
正如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());
}
}
}
}
如果您不想在文件存储在 Blob 后立即读取文件,则需要使用 BlobTrigger。 这是您设置功能的方式 -
[FunctionName("XlsxFunction")]
public static async Task ReadXlsx(
[BlobTrigger("mycontainer/{fileName}.xlsx", Connection = "MyBlobConnection")] Stream file, string fileName,
ILogger log)
{
//install EPPlus nuget
//using OfficeOpenXml;
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage(file))
{
var worksheet = package.Workbook.Worksheets[0];
for(int row = 0; row < worksheet.Dimension.End.Row; row++)
{
for(int col = 0; col < worksheet.Dimension.End.Column; col++)
{
//process things;
}
}
//final thing here & save;
}
}
我从未使用过 EPPlus,但这是您可以开始使用该库读取文件的方式。
笔记:
您可以按照此处的自述文件以不同的方式添加许可证 - https://github.com/EPPlusSoftware/EPPlus
最后,
在 BlobTrigger 中,我添加了 Connection="MyBlobConnection",这意味着我需要在 local.settings.json 中包含这个键(& 函数发布到 Azure 后的应用程序环境配置)。
这看起来像这样 -
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"MyBlobConnection": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
从上面,请注意,您可以直接使用“AzureWebJobsStorage”而不是自定义“MyBlobConnection”。 这取决于您的用例。 您的 blob 连接字符串可以在 Azure 门户的 Azure 存储服务中找到。 有关详细信息 - Azure 云存储帐户的连接字符串
此外,请注意我已经使用 BlobTrigger 捕获了“文件名”。 如果文件名不是您所期望的,您可以使用它来确保不运行代码。
例如,您可以在函数方法的开头添加此条件 -
if(!fileName.Contains("accounts")) return;
// continue the processing of data;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.