I am facing some problems reading excel files created with the Axlsx module via RubyXL. The specific code is this
require 'axlsx'
require 'RubyXL'
#Create the Excel doc with Axlsx with two sheets named My Sheet1 and My Sheet 2
Axlsx::Package.new do |p|
p.workbook.add_worksheet(:name => "My Sheet1") do |sheet|
sheet.add_row ["Simple Pie Chart"]
%w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
end
p.workbook.add_worksheet(:name => "My Sheet 2") do |sheet|
sheet.add_row ["Simple Pie Chart"]
%w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
end
p.serialize('simple.xlsx')
end
#Parse the above created Excel via RubyXL and print the names of the two sheets. This comes up blank
workbook = RubyXL::Parser.parse("simple.xlsx")
workbook.worksheets.each do |worksheet|
puts "Worksheet is #{worksheet.sheet_name}"
end
The problem is that the worksheet names come up blank. If I after having created with axlsx open up the created Excel with MS Excel and Save it, then the RubyXL parser is able to read the sheet names correctly. It seems like MS Excel is fixing the document somehow.
Am I missing something when creating the Excel via Axlsx?
I am using axlsx version 1.3.6 and rubyXL 1.2.10.
I also tried adding p.use_shared_strings = true before serializing with axlsx.
So instead of this in parser.rb of rubyXL.
#sheet_names, dimensions
def Parser.create_matrix(wb,i, files)
sheet_names = files['app'].css('TitlesOfParts vt|vector vt|lpstr').children
sheet = Worksheet.new(wb,sheet_names[i].to_s,[])
I replaced it with
#sheet_names, dimensions
def Parser.create_matrix(wb,i, files)
sheet_names = []
files['workbook'].css('sheet').each do |sheet|
sheet_names.push sheet.attribute('name').to_s
end
sheet = Worksheet.new(wb,sheet_names[i].to_s,[])
Is that the right fix?