I used an xlsx.erb
template in my Rails application to generate an Excel report (as office XML). The excel file generates fine but I get an error while opening it in Excel software (extension or file format maybe be invalid
)
This is the template I used to generate the open xml excel:
<?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="Forecast">
<Names>
<NamedRange ss:Name="Print_Area"/>
</Names>
<Table>
<Row>
<% (1..5).each do |dayi| %>
<Cell><Data ss:Type="String"><%= 'Day' + dayi.to_s %></Data><NamedCell
ss:Name="pRange_default"/><NamedCell ss:Name="Print_Area"/></Cell>
<% end %>
<Cell/>
<% (25..30).each do |i| %>
<Cell><Data ss:Type="String"><%= 'NP' + i.to_s %></Data></Cell>
<% end %>
</Row>
<% (1..5).each do |rowi| %>
<Row>
<% (1..5).each do |ci| %>
<Cell><Data><%= ci %></Data><NamedCell
ss:Name="pRange_default"/><NamedCell ss:Name="Print_Area"/></Cell>
<% end %>
<Cell/>
<% (25..30).each do |i| %>
<Cell><Data><%= i %></Data></Cell>
<% end %>
</Row>
<% end %>
</Table>
</Worksheet>
</Workbook>
This is the open XML that got generated:
<?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="Forecast">
<Names>
<NamedRange ss:Name="Print_Area"/>
</Names>
<Table>
<Row>
<Cell> <Data ss:Type="String">Day1 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data ss:Type="String">Day2 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data ss:Type="String">Day3 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data ss:Type="String">Day4 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data ss:Type="String">Day5 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell/>
<Cell> <Data ss:Type="String">NP25 </Data> </Cell>
<Cell> <Data ss:Type="String">NP26 </Data> </Cell>
<Cell> <Data ss:Type="String">NP27 </Data> </Cell>
<Cell> <Data ss:Type="String">NP28 </Data> </Cell>
<Cell> <Data ss:Type="String">NP29 </Data> </Cell>
<Cell> <Data ss:Type="String">NP30 </Data> </Cell>
</Row>
<Row>
<Cell> <Data>1 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>2 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>3 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>4 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>5 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell/>
<Cell> <Data>25 </Data> </Cell>
<Cell> <Data>26 </Data> </Cell>
<Cell> <Data>27 </Data> </Cell>
<Cell> <Data>28 </Data> </Cell>
<Cell> <Data>29 </Data> </Cell>
<Cell> <Data>30 </Data> </Cell>
</Row>
<Row>
<Cell> <Data>1 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>2 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>3 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>4 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>5 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell/>
<Cell> <Data>25 </Data> </Cell>
<Cell> <Data>26 </Data> </Cell>
<Cell> <Data>27 </Data> </Cell>
<Cell> <Data>28 </Data> </Cell>
<Cell> <Data>29 </Data> </Cell>
<Cell> <Data>30 </Data> </Cell>
</Row>
<Row>
<Cell> <Data>1 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>2 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>3 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>4 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>5 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell/>
<Cell> <Data>25 </Data> </Cell>
<Cell> <Data>26 </Data> </Cell>
<Cell> <Data>27 </Data> </Cell>
<Cell> <Data>28 </Data> </Cell>
<Cell> <Data>29 </Data> </Cell>
<Cell> <Data>30 </Data> </Cell>
</Row>
<Row>
<Cell> <Data>1 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>2 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>3 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>4 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>5 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell/>
<Cell> <Data>25 </Data> </Cell>
<Cell> <Data>26 </Data> </Cell>
<Cell> <Data>27 </Data> </Cell>
<Cell> <Data>28 </Data> </Cell>
<Cell> <Data>29 </Data> </Cell>
<Cell> <Data>30 </Data> </Cell>
</Row>
<Row>
<Cell> <Data>1 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>2 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>3 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>4 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell> <Data>5 </Data> <NamedCell
ss:Name="pRange_default"/> <NamedCell ss:Name="Print_Area"/> </Cell>
<Cell/>
<Cell> <Data>25 </Data> </Cell>
<Cell> <Data>26 </Data> </Cell>
<Cell> <Data>27 </Data> </Cell>
<Cell> <Data>28 </Data> </Cell>
<Cell> <Data>29 </Data> </Cell>
<Cell> <Data>30 </Data> </Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
Why am I not able to open the above open xml using Excel software?
Please help!