一个用 Ruby 读取 Excel 的例子

一个用 Ruby 读取 Excel 的例子

读取当前目录的 “test.xls” 的 “Sheet1” 工作簿的内容,并保存成一个对象,方便以后直接读出

[Copy to clipboard] [ - ]
windows下路径有点问题,改了一下,读取成功了。

代码学习中,谢谢分享
引用:
原帖由 bbschat 于 2008-3-26 20:23 发表
读取当前目录的 “test.xls” 的 “Sheet1” 工作簿的内容,并保存成一个对象,方便以后直接读出


 require 'win32ole'
 $col_map =["","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O"," ...
初学。。感觉用纯Ruby来写这些好像没那么多必要···
如果能加上.net 或者 java。。那感觉好多了
class <span href="tag.php?name=Excel" onclick="tagshow(event)" class="t_tag">Excel</span>_Info
这句我一直执行不过去,不知道为什么??相似的span块也执行不下去
学习了~~谢谢分享~!
SPAN 统统去掉就OK了

require 'win32ole'
$col_map =["","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"]
class Excel_Info
 attr_accessor :file_name
 attr_accessor :range
 def initialize(file_name)
  @file_name = file_name
  @range = {}
 end
end

def load_data(filename)
 obj = nil
 File.open(filename, "rb") {|f| obj = Marshal.load(f)}
 return obj
end

def save_data(obj, filename)
 File.open(filename, "wb") {|f| Marshal.dump(obj, f)}
end

def get_range_name(row, col)
 return "ERR" if (col < 1) || (row < 1) || (col > 26 * 27) || (row > 65536)
 c2 = col % 26
 c2 = 26 if c2 == 0
 c1 = (col - c2) / 26
 return $col_map[c2] + row.to_s if c1 == 0
 return $col_map[c1] + $col_map[c2] + row.to_s
end

def read_excel(e_name, s_name)
 $excel.WorkBooks.Open(e_name)
 $excel.WorkSheets(s_name).Activate
 $excel_info = Excel_Info.new(e_name)
 rows = $max_rows
    cols = $max_cols
 rows = $excel.WorkSheets(s_name).UsedRange.Rows.Count if rows == 0
 for row in 1..rows
  for col in 1..cols
   $excel_info.range[get_range_name(row, col)] = $excel.Cells(row, col).value.to_s
  end
 end

 $excel.WorkBooks.Close()
end

$MY_LOCATION = Dir.getwd
$excel = WIN32OLE.new("excel.application")
$excel.Visible = false
$max_cols = 2
$max_rows = 0
read_excel("d:\\test.xls","Sheet1")
save_data($excel_info, "excel_info.obj")
info = load_data("excel_info.obj")
puts info.file_name
puts info.range["A1"]
puts info.range["A2"]
puts info.range["B1"]
puts info.range["B2"]
$excel.Quit()
谢谢楼上指点,那SPAN语句在这个程序中什么作用呢?
刚开始学习Ruby,嘿嘿~~要学的还很多啊~~~
我一般处理excel,直接解析xml,就不用什么win321ole那么繁琐了.。。。