不兼容的字符编码错误 - 在编写xls文件时

时间:2011-10-31 05:55:06

标签: ruby-on-rails spreadsheet

我需要以.xls格式生成报告,在尝试写入数据时,我收到以下错误

~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:515:in `block in _write_sst': incompatible character encodings: ASCII-8BIT and US-ASCII (Encoding::CompatibilityError)
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:502:in `each'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:502:in `each_with_index'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:502:in `_write_sst'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:489:in `write_sst'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:439:in `write_from_scratch'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/excel/writer/workbook.rb:620:in `write_workbook'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/writer.rb:15:in `block in write'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/writer.rb:14:in `open'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/writer.rb:14:in `write'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/spreadsheet-0.6.4/lib/spreadsheet/workbook.rb:106:in `write'
    from lib/parse_fellow.rb:449:in `generate_match_report_fellow'
    from lib/parse_fellow.rb:547:in `<top (required)>'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/railties-3.0.7/lib/rails/commands/runner.rb:48:in `eval'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/railties-3.0.7/lib/rails/commands/runner.rb:48:in `<top (required)>'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/railties-3.0.7/lib/rails/commands.rb:39:in `require'
    from ~/.rvm/gems/ruby-1.9.2-p180@ash/gems/railties-3.0.7/lib/rails/commands.rb:39:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

parse_fellow.rb的第449行是

workbook.write("#{RAILS_ROOT}/lib/#{file}")

在将值传递给我的工作簿对象之前,我尝试使用以下方法将它们编码为ASCII和UTF-8,但它们都没有帮助我

"my text content".force_encoding("ASCII-8BIT")

我的开发环境:

ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
Rails 3.0.7
spreadsheet (0.6.4)

我不知道这里出了什么问题以及如何调试此问题。任何输入对我都有很大帮助。

1 个答案:

答案 0 :(得分:1)

但我真的不知道问题是什么。不幸的是,我找不到其他遇到类似情况的人。

我解决这个问题的方法是尝试使用不同的可用版本的gem,最后它使用以下版本。

gem "spreadsheet", '0.6.5.9'