问题描述
我想使用C#中的Epplus包读取作为Blob存储在Azure存储容器中的excel文件。 我试图用这段代码做些事情。
lapply(seq_along(cleanFiles),function(i)
writeLines(cleanFiles[[i]],con = file.path(dummyRPath,basename(functionList[i]))))
抛出错误了吗?
解决方法
我们不能使用Azure blob url初始化FileInfo
对象,它将引发错误`System.NotSupportedException:'不支持给定路径的格式。'。
因此,如果您想读取存储在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());
}
}
}
}