Luan Nico
Luan Nico

Reputation: 5917

Spreadsheets ruby gem encoding not working

I'm getting a weird problem when I try to write strings (that are UTF-8) in a xls with the Spreadsheets gem. It doesn't give errors, but I get an invalid spreadsheet, with random characters (opened on Excel and Calc, same thing).

So I assume it is an encoding error, but I thought the lib would automatically convert my strings to the encoding used by Excel... I tried converting them to ISO by hand (.encode('ISO-8859-1')), force_encoding to UTF-8, and many other combinations of these two methods. Some give execution errors, and the others just don't work. Is there anything special I should do?

Spreadsheets: http://spreadsheet.rubyforge.org/

Code:

book = Spreadsheet::Workbook.new
sheet = book.create_worksheet

lines.each do |line|
  sheet.row(row).concat(line) #line is in utf-8
end

book.write @file

Upvotes: 1

Views: 807

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

You should try adding the following magic comment on top of your ruby script and then try.

# encoding: UTF-8

Before processing your source code interpreter reads this line and sets proper encoding. So, I assume this should solve your problem.

Upvotes: 2

Related Questions