ruby on rails 3 - generating xls documents representing model objects keep being read as corrupt in excel 2010 -


i'm generating xls document each item of model weeklyreport. have basic structure of document working, i'm finding small changes keep making file read corrupt , unopenable excel 2010, , don't understand why. i've been using railscast here:http://railscasts.com/episodes/362-exporting-csv-and-excel figure out how this.

in model controller, have following:

def show     @report=weeklyreport.find(params[:id])     @engagements=@report.engagements end  def index     @reports=weeklyreport.all     respond_to |format|         format.html         format.xls     end end 

and in show.xls file, have following:

<?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">   <documentproperties xmlns="urn:schemas-microsoft-com:office:office">     <author>ourappname</author>     <created><%= @report.created_at%> </created>   </documentproperties>   <worksheet ss:name="engagements">       <table>       <row>         <cell><data ss:type="string">first name</data></cell>         <cell><data ss:type="string">email</data></cell>         <cell><data ss:type="string">date</data></cell>         <cell><data ss:type="string">version</data></cell>         <cell><data ss:type="string">rating</data></cell>         <cell><data ss:type="string">comment</data></cell>       </row>       <% @engagements.each |e| %>       <row>         <cell><data ss:type="string"> <%= e.user.first_name%></data></cell>         <cell><data ss:type="string"> <%= mail_to("#{e.user.email}")%></data></cell>         <cell><data ss:type="string"> <%= e.created_at.strftime("%b %d, %y")%></data></cell>         <cell><data ss:type="string"> <%= e.version_made %></data></cell>         <cell><data ss:type="string"> <%= e.rating %></data></cell>         <cell><data ss:type="string"> <%= e.comment %></data></cell>       </row>       <% end %>     </table>   </worksheet> </workbook> 

i'm trying understand makes files read corrupt. adjusted document in excel how want to, , trying reverse engineer it. when copy , pasted xls code app (figuring go , change iterating on data after got working), said file corrupt. when make small changes of sorts, says file corrupt. here of things have made file corrupt:

  • changing rating data cell type number
  • copying on style information xml document saved excel file as
  • adding column width information basically, don't understand why making files corrupt , causing them corrupt, seems happens half time no reason (obviously not case)

i want our admins able download files confidence. warning when opening files "the file trying open in different format specified file extension. verify file not corrupted , trusted source before opening file. want open file now?"

most of answers i'm seeing dealing corrupted files aren't corrupted in excel involves changing protection settings trusted documents. don't think appropriate solution problem, reduce security of our admins computers.

how can create secure (as in, reads trustworthy , not-scary our less technical admins), non-corrupt xls files? i've been doing isn't working.

i figured out, part, why happening. in case set type number, corrupt because <%= e.rating %> string. though rating integer, embedded ruby returns string. think of rest of time, i'm not quite closing tags right. numbers thing confusing problem.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -