Xls and ODS files created with Spreadsheet gem tagged as Virus

I have been using the Spreadsheet gem to create xls files and zip them for transfer using using ruby zip in my application.

     excel_file = Spreadsheet::Workbook.new 
     .#code for xls

And I zip the file like this in my controller

     files = Dir.glob("#{Rails.root}/files/excel/*.xls")
     @original_file = File.basename(files.first)
     @original_path = "#{Rails.root}/files/excel"

     Zip:: File.open(@filename, Zip::File::CREATE) do |z|
    z.get_output_stream("Other details.txt") { |os| os.write "This is a system generated excel sheet" }
send_file @filename, :type => 'application/zip', :filename => "Attendance-#{city}-#{t}.zip", :stream=> false, :x_sendfile=>true

This approach works very well when I download the zip file with by Ubuntu desktop and read the xls file with LibreOffice. The zip is extracted properly without any issues and the xls sheet is read without any warnings for corruption.

But when the download the same zip file with Windows 7 and unzip with File explorer, the xls file when opened with MS Excel the "Error 0x80070057: The parameters are incorrect"
error pops up. And I am unable to read the excel file.

Also when the xls sheets is uploaded via gmail or hotmail, it is being tagged as virus.

Note:- This happens only for the xls or ods files which are greater than 140kB in size.

Both production and local environment generated files have this issue.

Is Spreadsheet gem the cause of this issue?


The mime type for the file generated using Spreadsheet seems to be

application/CDFV2-corrupt; charset=binary

But the default mime type for an xls file should be

application/vnd.ms-office; charset=binary

1 Like

Try compressing and uncompressing other files (like photos) to ascertain whether the problem is with the type of file or the compression. If the latter, then it might be worth posting as a bug at the repo.

Hi Aston, the zip file seems to be fine when I don’t add the xls. Windows file manager was able to unzip without any issues. This issue seems to exist only for larger sized xls files when the smaller one don’t seem to have any issues with Windows and MS Excel.

Also what I have learnt about Spreadsheet gem is that, the entire Worksheet is kept in memory and addition of rows, formatting, etc are performed whilst in memory. And file write to disk happens when the entire operations are over. Is it possible for the xls file to get corrupted ( tagged by MS Excel) due to lack of RAM since my server has only 512mb RAM ? And what confuses me the most is that the corrupted xls sheets open without any issues in my Ubuntu desktop’s Libreoffice.

1 Like

Have you tried generating the spreadsheets with more memory available? I would expect Ruby the raise an OOM exception, but that’s still something you can test.

Also, I think I’ve read somewhere that some mail services raise a virus detected warning for large xls attachments.

1 Like

Yes kofno, I have tried with more RAM in my local instance i.e. 4gb RAM.
The issue still exists. Hence, I have contacted the Spreadsheet Gem library author zdavatz who asked me to open a issue ticket in GitHub.

GitHub ticket with full code block

1 Like