How to solve excel cannot open the file. Because the file format or file extension is not valid. Duration: 3:25. DOCTOR ZENIUS PRODUCTION 305,388 views.
I'm calling the action 'Export' where i pass a list of viewmodels and define the format
The action i call (ExcelEngine is by Syncfusion):
I only posted the code which has an impact on the file and (maybe) could create the error. There is no error, until i open the file, then this exception pops up:
Excel cannot open the file 'Tokenlist_22.05.2018.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
I've tried to change the file format to .xls and .vbs but neither works. With .xls I can open the document but then it has no data in it.
The .close() doesn't change much, it just closes the output stream previously opened.
eDonkey
eDonkeyeDonkey
2 Answers
Use the FileContentResult Overload where you can provide the
fileDownloadName
like this:
And use the stream
ToArray()
extension to return a byte[]
.
(I assume your
Export
method is generating a valid document)
Gergely BakosGergely Bakos
As stream reached end position while returning it, the downloaded file is getting corrupted. So, it is recommended to set its current position to 0 to resolve this issue. Please refer below code to achieve the same.
Code Example:
We have also shared a simple sample for your reference which can be downloaded from following link.
Sample Link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample1020485770.zip
I work for Syncfusion.
Abirami VaradharajanAbirami Varadharajan