写了一段Ruby操作Excel的脚本,使用OLE方式,功能是从Excel中读取第一列有效数据至数组,分析后将数据分类存入几个集合,并输出至Excel中
- require "win32ole"
- excel = WIN32OLE.new('excel.application')
- excel.Visible = true
- workBook = excel.WorkBooks.Open('c:/a.xlsx')
- workSheet = workBook.Worksheets(1)
- workSheet.Select
- line = 1
- data = []
- query = []
- statistic = []
- # 读取第一列数据Ax
- while workSheet.Range("a#{line}").Value
- value = workSheet.Range("a#{line}").Value
- keyWord = value.split('=')[0].upcase
- if keyWord.include? 'QUERY'
- query << value
- elsif keyWord.include? 'STATIS'
- statistic << value
- else
- data << value
- end
- line += 1
- end
- # 写入数据Hx
- for i in 0...data.size
- workSheet.Range("h#{i + 1}").Value = data[i]
- end
- excel.ActiveWorkBook.Close(1)
- excel.Quit
- p "query=#{query.size}"
- p "statis=#{statistic.size}"
- p "data=#{data.size}"