If you use Environment.CurrentDirectory
to get the executing directory, it will response the directory you displaying. And this directory in the azure kudu, it's not allowed to create files so your excel file is not there. You could use context.FunctionDirectory
to get the current function directory (e.g. when running on Azure)
ex:
public static void Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
var excelFilePath = context.FunctionDirectory + @"\Book.xlsx";
var destinationCsvFilePath = context.FunctionDirectory + @"\test.csv";
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
var stream = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
IExcelDataReader reader = null;
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
var ds = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
{
UseHeaderRow = false
}
});
var csvContent = string.Empty;
int row_no = 0;
while (row_no < ds.Tables[0].Rows.Count)
{
var arr = new List<string>();
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
arr.Add(ds.Tables[0].Rows[row_no][i].ToString());
}
row_no++;
csvContent += string.Join(",", arr) + "\n";
}
StreamWriter csv = new StreamWriter(destinationCsvFilePath, false);
csv.Write(csvContent);
csv.Close();
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
And just update your excel here: