I am trying to read all the Excel worksheets of a given workbook asyncronously, but it is somehow not happenning. The idea is to sum the first 123 cells in every Excel sheet and to print it at the end. The code compiles and runs without errors, but it does not read all the worksheets, it simply skips that part, because of the async
.
namespace SyncAndAsync
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
class Startup
{
static void Main()
{
string filePath = @"C:\Users\Desktop\Sample.xlsx";
Excel.Application excel = new Excel.Application();
excel.Visible = true;
excel.EnableAnimations = false;
Excel.Workbook wkb = Open(excel, filePath);
var calculation = CalculateAllWorksheetsAsync(wkb);
//foreach (var item in calculation)
//{
// Console.WriteLine(item);
//}
excel.EnableAnimations = true;
wkb.Close(true);
excel.Quit();
}
static async Task<List<Information>> CalculateAllWorksheetsAsync(Excel.Workbook wkb)
{
List<Task<Information>> tasks = new List<Task<Information>>();
foreach (Excel.Worksheet wks in wkb.Worksheets)
{
Task.Run(() => CalculateSingleWorksheetAsync(wks));
}
var results = await Task.WhenAll(tasks);
return new List<Information>(results);
}
static async Task<Information> CalculateSingleWorksheetAsync(Excel.Worksheet wks)
{
Information output = new Information();
int result = 0;
await Task.Run(() =>
{
for (int i = 1; i <= 123; i++)
{
result += (int)(wks.Cells[i, 1].Value);
}
});
output.WorksheetName = wks.Name;
output.WorksheetSum = result;
Console.WriteLine($"{wks.Name} - {result}");
return output;
}
static Excel.Workbook Open(Excel.Application excelInstance,
string fileName, bool readOnly = false,
bool editable = true, bool updateLinks = true)
{
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
}
}
The Information
class is added to use the Task, probably it could be skipped:
namespace SyncAndAsync
{
class Information
{
public string WorksheetName { get; set; } = "";
public int WorksheetSum { get; set; } = 0;
}
}
Dependencies:
- The reference
Microsoft.Office.Interop.Excel
should be added; - Change the
string filePath = @"C:\Users\Desktop\Sample.xlsx";
to something relevant and make sure in the Excel file there are some numbers in the first column on every sheet, to get results;
The question - How to make the asynchronous run and display the sums of all worksheets?