[Ruby] dump excel data example.

require 'win32ole'

def getAbsolutePath filename
  fso = WIN32OLE.new('Scripting.FileSystemObject')
  return fso.GetAbsolutePathName(filename)
end

filename = getAbsolutePath("sample1.xls")
xl = WIN32OLE.new('Excel.Application')
book = xl.Workbooks.Open(filename)

begin
book.Worksheets.each do |sheet|
  sheet.UsedRange.Rows.each do |row|
    record = []
    row.Columns.each do |cell|
      record << cell.Value
    end
    puts record.join(",")
  end
end

ensure 
  book.Close
  xl.Quit
end

Reference: Win32OLE 活用法 【第 2 回】 Excel

2 則迴響 »

  1. Takol said

    哇!三個迴圈,好笨喔。

  2. Philip said

    這位爺~
    您是來找碴的嗎? 囧

RSS feed for comments on this post · TrackBack URI

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

%d 位部落客按了讚: