Vardarac
Vardarac

Reputation: 563

Spreadsheet gem and XML/XLS-ERB templates: OLE2 signature invalid

My web app outputs a mixture of prefab Excel 97 spreadsheets (these made in Excel, so exhibit no problems) and Rails-generated templates. The latter are what our users download when they want an excel spreadsheet of data:

<?xml version="1.0"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
        xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
        xmlns:html="http://www.w3.org/TR/REC-html40">
        <Worksheet ss:Name="Sheet1">
            etc

Downloading sheets made from these xls.erb templates is no problem. Neither is parsing the prefab templates with the Spreadsheet gem. However, re-uploading the Rails-generated sheets to controller actions, then trying to use the Spreadsheet gem to parse them is a problem:

Ole::Storage::FormatError in FoosController#update_foos

    OLE2 signature is invalid

Others in the past have had this problem because they attempted to parse XLSX files with Spreadsheet; the type is incompatible with the gem. My problem appears to be that I need to generate an Excel 2004 file from a Ruby template, but my current format seems to output XML that merely had the file extension incorrectly named.

Is it possible to generate a 2004 Excel spreadsheet, or parse the Excel files I've already generated, without using a gem? Is there an easier solution to what I'm trying to do, which is just to output an Excel spreadsheet based on my Rails data that can be re-uploaded to the system and parsed by Spreadsheet?

Again, I'm trying to avoid gems, but if there's a gem solution that either reads xml-erb templates or reads and writes 2004-compatible xls files, I'm all ears.

Upvotes: 0

Views: 1030

Answers (1)

Vardarac
Vardarac

Reputation: 563

I wound up combining two old solutions using the Spreadsheet gem my app already has loaded:

How can I create new spreadsheet worksheets in Ruby using the Spreadsheet gem?

Direct downloading a xls file without writing it to the directory by Spreadsheet gem

I moved the code from the first solution into a private controller method create_spreadsheet that I passed the instance variable containing the data I needed to convert into a spreadsheet. No longer any compatibility issues.

Upvotes: 0

Related Questions