Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update .XLS export to not produce corrupt file warnings #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lib/make_exportable/exportable_formats/excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Excel < ExportableFormat #:nodoc:

attr_accessor :data_set, :data_headers


def initialize(data_set, data_headers=[])
self.long = 'Microsoft Excel'
self.mime_type = 'application/vnd.ms-excel; charset=utf-8;'
Expand All @@ -16,18 +17,31 @@ def initialize(data_set, data_headers=[])

def generate
generate_header_option(data_headers)
output = "<table>\n"
output = "<?xml version='1.0'?>\n"
output << "<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'\n"
output << "\txmlns:o='urn:schemas-microsoft-com:office:office'\n"
output << "\txmlns:x='urn:schemas-microsoft-com:office:excel'\n"
output << "\txmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'\n"
output << "\txmlns:html='http://www.w3.org/TR/REC-html40'>\n"
output << "\t<Worksheet ss:Name='Sheet1'>\n"
output << "\t\t<Table>\n"

unless data_headers.blank?
output << "\t<tr>\n"
output << data_headers.map {|h| "\t\t<th>#{sanitize(h.humanize.titleize)}</th>\n" }.join
output << "\t</tr>\n"
output << "\t\t\t<Row>\n"
output << data_headers.map {|h| "\t\t\t\t<Cell><Data ss:Type='String'>#{sanitize(h.humanize.titleize)}</Data></Cell>\n" }.join
output << "\t\t\t</Row>\n"
end

data_set.each do |row|
output << "\t<tr>\n"
output << row.map {|field| "\t\t<td>#{sanitize(field)}</td>\n"}.join
output << "\t</tr>\n"
output << "\t\t\t<Row>\n"
output << row.map {|field| "\t\t\t\t<Cell><Data ss:Type='String'>#{sanitize(field)}</Data></Cell>\n" }.join
output << "\t\t\t</Row>\n"
end
output << "</table>\n"

output << "\t\t</Table>\n"
output << "\t</Worksheet>\n"
output << "</Workbook>\n"

return output
end

Expand Down