我想使用 C# 中的 Epplus 包读取作为 Blob 存储在 Azure 存储容器中的 Excel 文件。
我尝试用这段代码做一些事情。
string uri = blob.Uri.AbsoluteUri;
FileInfo fileInfo = new FileInfo(uri);
ExcelPackage p = new ExcelPackage(fileInfo);
ExcelWorksheet ws = p.Workbook.Worksheets[1];
Console.WriteLine(ws.Cells[1,1].Value.ToString());
它抛出一个错误?
我们无法使用 Azure blob url 来初始化FileInfo
对象,它会抛出错误System.NotSupportedException: 'The given path's format is not supported.'
.
因此,如果要读取存储在Azure 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());
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)