Hello,
I am using XlsIO version 19.1.0.54 and am accepting a user's upload of an Excel book from a browser to a .Net Core 3.x backend. I have the following code which works when it is in-line in the method (the variable firstSheet has data):
// Instantiate the Excel application object
using var excelEngine = new ExcelEngine();
var application = excelEngine.Excel;
// Assigns default application version
application.DefaultVersion = ExcelVersion.Excel2016;
using var ms = new MemoryStream();
await file.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
var workbook = application.Workbooks.Open(ms);
var firstSheet = workbook.Worksheets[0];
Howeverwhen I extract that to its own method like the following, the last line where I access the Worksheetsproperty just returns null:
private async Task ExtractExcelFromFile(IFormFile file, CancellationToken cancellationToken)
{
// Instantiate the Excel application object
using var excelEngine = new ExcelEngine();
var application = excelEngine.Excel;
// Assigns default application version
application.DefaultVersion = ExcelVersion.Excel2016;
using var ms = new MemoryStream();
await file.CopyToAsync(ms, cancellationToken);
ms.Position = 0;
var workbook = application.Workbooks.Open(ms);
return workbook;
}
Thank you.