Improve save_as_excel function: use enumerate and check the file extension