Question : Ruby - load text file to excel

Hello
Experimenting with excel - know how to open new workbook and enter some basic values - but struggling to know how we could import a text file into excel - if possible.
Lets say the text file has 4 columns as ...
Col - A        Col - B      Col - C             Col -D
ABCDE        25.4         ASD-123         TEST
DFRGH        453.36     SSS45             TEST2

My script so far in terms of entering basic data .......
require 'win32ole'  
   excel = WIN32OLE.new('excel.application')
   excel.visible = true
   excel.workbooks.open('U:\Phil_Sivyer\pip.xls')
   excel.range('A1').value = 'Test'
   excel.range('A2').value = 25.4
   a = excel.range('A2').value
   excel.range('A3').value = a*2
   excel.range('A4').value = a*2/4.5

Phil

Answer : Ruby - load text file to excel

Hi Phil,

you could try something like this
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
require 'win32ole'   
heading = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI)
 
excel = WIN32OLE.new('excel.application')
excel.visible = true
excel.workbooks.open('f:/12_ruby/pip.xls')
 
i = 0
File.open('cols.txt') do |file|
			while line = file.gets
          i += 1
          arr = line.split(/\s+/)
          range = 'A' + i.to_s + ':' + heading[arr.length - 1] + i.to_s
          excel.range(range).value = arr
      end
 end
Open in New Window Select All
Random Solutions  
 
programming4us programming4us