# Failed to write unicode string to excel

**URL:** <https://www.ruby-forum.com/t/failed-to-write-unicode-string-to-excel/226075>\
**Category:** Ruby\
**Created:** [September 3, 2010, 7:24am UTC](https://www.ruby-forum.com/t/failed-to-write-unicode-string-to-excel/226075 "2010-09-03T07:24:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Author](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Author](https://www.ruby-forum.com/u/Author)\
**Post date:** [September 3, 2010, 7:24am UTC](https://www.ruby-forum.com/t/failed-to-write-unicode-string-to-excel/226075/1 "2010-09-03T07:24:17Z")

</div>

Hi,  
I am trying to do some converting from xml data and write the reports to  
Excel. it works very well for English, however, the Unicode data will be  
messing in the excel, how should I do it.  
the class I am using to write Excel:

require ‘win32ole’  
require “iconv”

### 

# 

class ExcelConst  
end

class ExcelHelper

def initialize()  
@excel\_app = WIN32OLE.new(“Excel.Application”)  
puts “Excel failed to start” unless @excel\_app  
@excel\_app.Visible = false  
@workbook = @excel\_app.Workbooks.Add  
@sheet = @workbook.Worksheets(1)  
puts “Excel failed to get wowksheet” unless @sheet  
@row =1

```
#WIN32OLE.const_load(@excel_app, ExcelConst)

```

end

def write\_header(header\_data)  
@row = 1  
col = 0  
header\_data.each{ |cell| begin  
col\_name = (?A + col).chr  
range = @sheet.Range("#{col\_name}#{@row}")  
range.value = cell  
col = col+1  
end  
}  
@sheet.Rows(1).Font.Bold = true  
@sheet.Rows(1).Font.Size = 14  
#@sheet.Rows(1).Font.ColorIndex = 5 #set to blue  
@sheet.Rows(1).Interior.ColorIndex = 37 #set to light blue  
@sheet.Rows(1).RowHeight = 25

end

def set\_column\_width(width\_list)  
col =0  
width\_list.each{ |width| begin  
col\_name = (?A + col).chr  
@sheet.Columns(col\_name).ColumnWidth = width  
col = col+1  
end  
}  
end

def write\_row(data)  
@row =2 if @row \<2

```
p data
col = 0
data.each{ |cell| begin
  col_name = (?A + col).chr
  range = @sheet.Range("#{col_name}#{@row}")
  range.value = cell
  p cell
  col = col+1
end
}
@row = @row+1

```

end

def save(filename)  
#@sheet.Columns.AutoFit()  
excel97\_2003\_format = -4143  
#pwd = Dir.pwd.gsub(’/’,’\’) \<\< ‘\’  
#otherwise, it sticks it in default save directory-  
C:\Users\Sam\Documents on my system  
@workbook.SaveAs(filename, excel97\_2003\_format)  
end

def quit()  
if(@excel\_app != nil) then  
@excel\_app.Quit  
@excel\_app = nil  
end  
end  
end

thank you

–

Best regards,  
Liang

---

<div class="post-metadata">

**Author:** ![Author](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Author](https://www.ruby-forum.com/u/Author)\
**Post date:** [September 13, 2010, 10:41am UTC](https://www.ruby-forum.com/t/failed-to-write-unicode-string-to-excel/226075/2 "2010-09-13T10:41:26Z")

</div>

Anybody can give some hints?

On Fri, Sep 3, 2010 at 1:24 PM, Author [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class ExcelConst  
> puts “Excel failed to get wowksheet” unless @sheet  
> range = @sheet.Range(“#{col\_name}#{@row}”)  
> end
> 
> ```
> col = col+1
> 
> ```
> 
> C:\Users\Sam\Documents on my system
> 
> thank you
> 
> –
> 
> Best regards,  
> Liang

–

Best regards,  
Liang
