1

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
Luan Nico
  • 5,376
  • 2
  • 30
  • 60

1 Answers1

2

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.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110