1

I am downloading data in excel format in my Rails 4 app using send_data method as mentioned below.

send_data collection.to_csv(col_sep: "\t"), 
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    filename: 'filename.xls'

I am able to download the data, but while opening the downloaded excel file, i am getting below warning message.

The file format and extension of 'file.xls' don't match. The file could be corrupted or unsafe.
Unless you trust its source, dont open it. D want to open it anyway?

Even it is warning, it would cause confusion to customers while downloading and opening the file. Any one has any clue about, what is it about and how could it be fixed?

Any help would be greatly appreciated.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Prem
  • 5,844
  • 3
  • 25
  • 23

1 Answers1

1

It isn't an XLS file you are creating but a CSV file, so you should change the filename to 'filename.csv'. It will still be openable in Excel.

You might also want to change the MIME type to text/csv.

The correct MIME for an Excel file can be found here.

Kris
  • 19,188
  • 9
  • 91
  • 111
  • 1
    Additionally the MIME type is wrong for an "xls" file any way. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" would be for a "xlsx" file and "xls" would be "application/vnd.ms-excel" – engineersmnky Oct 15 '19 at 20:13
  • I have implemented to_csv custom method to return csv/excel depending upon the separator. So, i am trying to download excel fiel here in 'xls' format. Tried type: 'application/vnd.ms-excel'. I am able to see the data after opening the file. But want to avoid the warning message given above, which could demotivate my customers from downloading/viewing data. – Prem Oct 15 '19 at 22:09
  • I'd recommend not outputing non-CSV from a method called `to_csv`. But instead using a separate object, e.g. `ConvertToSpreadsheet`, which can output the correct format. But anyway it proberbly what @Prem said that the MIME is wrong. – Kris Oct 16 '19 at 12:53
  • @Prem as suggested use 2 different mechanisms one for csv and one for excel. In addition I would recommend generating "xslx" files as they can be read by more than just excel (e.g. OpenOffice, googledocs, etc.) Also what are you using to generate this document as there are very few if any libraries that will generate a "xls" file anyway since that is a Microsoft proprietary binary format – engineersmnky Oct 16 '19 at 15:51