如何使用 EPPlus 库从 Excel 文件中读取数据
How to read data from Excel File saved in an blob in azure with EPPlus library
我正在尝试像这样读取我的 excel 保存在我的 azure 存储容器中的文件
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("concursos");
foreach (BlobItem blobItem in containerClient.GetBlobs())
{
BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var stream=blobClient.OpenRead(new BlobOpenReadOptions(true)))
using (ExcelPackage package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
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());
}
}
但是句子
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
抛出一个 error:System.NullReferenceException: 'Object reference not set to an instance of an object.' worksheet was null
我调试并查看我的流和我的包
blob中的excel是这样的.xls
有什么想法吗?
谢谢
请检查工作sheet是否空。如果sheet的列和行都是空的,则会出现此错误。
我试着重现了同样的
最初我尝试用 EPplus 读取 excel sheet ,其中填充了起始列和行并且 不为空 并且可以使用与相同的代码成功执行和读取你的。
然后我删除column1为空并存储在blob中并尝试读取它并得到空引用异常。
如果工作sheet刚刚初始化并且为空,ExcelWorksheet的维度对象将为空。
因此抛出空引用异常,据我所知,唯一的方法是检查文件是否为空或向其中添加内容 在访问它们之前,这样如果列为空,它就不会抛出异常。
worksheet.Cells[1, 1].Value = "Some text value";
以同样的方式尝试添加工作sheet,以避免在容器 blob 中没有 sheet 时出现异常。
ExcelWorksheet worksheet = new ExcelPackage().Workbook.Worksheets.Add("Sheet1");
This code will not throw an exception since the Dimension object was initialized by adding content to the worksheet.If the loaded
ExcelWorksheet already contains data, you will not face this issue.
ExcelWorksheet worksheet = package.Workbook.Worksheets.First();
//or ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
// Add below line to add new sheet , if no sheets are present and returning null exception
//ExcelWorksheet worksheet = new ExcelPackage().Workbook.Worksheets.Add("Sheet1");
//Add below line to add column and row , if sheet is empty and returning null exception
worksheet.Cells[1, 1].Value = " This is the end of worksheet";
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
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());
}
}
您也可以检查该值是否为空。
if(worksheet.cells[row,column].value != null)
{
//proceed with code
}
问题出在 blob
中 excel 个文件的文件扩展名
仅适用于 .xlsx 不适用于 .xls
谢谢
我正在尝试像这样读取我的 excel 保存在我的 azure 存储容器中的文件
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("concursos");
foreach (BlobItem blobItem in containerClient.GetBlobs())
{
BlobClient blobClient = containerClient.GetBlobClient(blobItem.Name);
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var stream=blobClient.OpenRead(new BlobOpenReadOptions(true)))
using (ExcelPackage package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
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());
}
}
但是句子
ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
抛出一个 error:System.NullReferenceException: 'Object reference not set to an instance of an object.' worksheet was null
我调试并查看我的流和我的包
blob中的excel是这样的.xls
有什么想法吗?
谢谢
请检查工作sheet是否空。如果sheet的列和行都是空的,则会出现此错误。
我试着重现了同样的 最初我尝试用 EPplus 读取 excel sheet ,其中填充了起始列和行并且 不为空 并且可以使用与相同的代码成功执行和读取你的。
然后我删除column1为空并存储在blob中并尝试读取它并得到空引用异常。
如果工作sheet刚刚初始化并且为空,ExcelWorksheet的维度对象将为空。 因此抛出空引用异常,据我所知,唯一的方法是检查文件是否为空或向其中添加内容 在访问它们之前,这样如果列为空,它就不会抛出异常。
worksheet.Cells[1, 1].Value = "Some text value";
以同样的方式尝试添加工作sheet,以避免在容器 blob 中没有 sheet 时出现异常。
ExcelWorksheet worksheet = new ExcelPackage().Workbook.Worksheets.Add("Sheet1");
This code will not throw an exception since the Dimension object was initialized by adding content to the worksheet.If the loaded ExcelWorksheet already contains data, you will not face this issue.
ExcelWorksheet worksheet = package.Workbook.Worksheets.First();
//or ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
// Add below line to add new sheet , if no sheets are present and returning null exception
//ExcelWorksheet worksheet = new ExcelPackage().Workbook.Worksheets.Add("Sheet1");
//Add below line to add column and row , if sheet is empty and returning null exception
worksheet.Cells[1, 1].Value = " This is the end of worksheet";
int colCount = worksheet.Dimension.End.Column;
int rowCount = worksheet.Dimension.End.Row;
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());
}
}
您也可以检查该值是否为空。
if(worksheet.cells[row,column].value != null)
{
//proceed with code
}
问题出在 blob
中 excel 个文件的文件扩展名仅适用于 .xlsx 不适用于 .xls
谢谢