Reputation: 315
i need to schedule a work every minute, actually, i need to compare a datetime of file to see if its a new one and if so i use the data of this file to fill another excel file
i find some ideas with timer exemple and its worked well with just a console.writeline for the exemple but when i ask for a bigger thing to do like what i said, its not working....
Startup.cs
in the Configure() method
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(1);
var timer = new System.Threading.Timer((e) =>
{
try
{
string filePath = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\Ressources\mydatafile.txt"}";
DataTable tbl = new DataTable();
string[] lines = System.IO.File.ReadAllLines(filePath);
var titrecolunTest = lines[0].Split('\t');
foreach (var titrecolon in titrecolunTest)
{
tbl.Columns.Add(new DataColumn(titrecolon.ToString()));
}
foreach (string line in lines.Skip(1))
{
var cols = line.Split('\t');
DataRow dr = tbl.NewRow();
for (int cIndex = 0; cIndex < cols.Length; cIndex++)
{
dr[cIndex] = cols[cIndex];
}
tbl.Rows.Add(dr);
}
string fileNameTEST = @"wwwroot/Ressources/Template.xlsx";
FileInfo fileInfo = new FileInfo(fileNameTEST);
using (ExcelPackage pck = new ExcelPackage(fileInfo))
{
ExcelWorksheet worksheetData = pck.Workbook.Worksheets["Data"];
worksheetData.Cells["A2"].LoadFromDataTable(tbl, false);
//mettre number format sur les collones AC AI AJ AK AL AM AN
int nbrRows = worksheetData.Dimension.End.Row;
foreach (var cell in worksheetData.Cells["AI2:AN" + nbrRows])
{
cell.Value = Convert.ToDouble(cell.Value);
}
foreach (var cell in worksheetData.Cells["AC2:AC" + nbrRows])
{
cell.Value = Convert.ToDouble(cell.Value);
}
worksheetData.Cells["AI2:AN" + nbrRows].Style.Numberformat.Format = "0";
worksheetData.Cells["AC2:AC" + nbrRows].Style.Numberformat.Format = "0";
pck.Save();
}
}
catch (Exception)
{
throw;
}
}, null, startTimeSpan, periodTimeSpan);
Upvotes: 2
Views: 557
Reputation: 315
i find a solution by using HangFire, there are nugets for it and some youtube tuto to show how it work. really easy to use !
Upvotes: 1