The end of the snippet would be to process a file uploaded by the user. It this what you want ? For now my understanding is that you want to let the user to download a file and want to keep a copy on your server. I believe no upload at all is involved ? Try maybe one step at a time. Also using HTML for an XLS file will be confusing for users. Latest Excel version should also show a warning when using a .xls file while the underlying format is not really .xls. If you want to create a true XLSX file your best bet could be to use https://blogs.msdn.microsoft.com/chrisrae/2011/08/18/creating-a-simple-xlsx-from-scratch-using-the-open-xml-sdk/ or a 3rd party Library which creates a true XLS file.