zoukankan      html  css  js  c++  java
  • 用Ruby读取Excel文件

    地址:

    http://www.taobaotest.com/blogs/qa?bid=3377

    http://hlee.iteye.com/blog/356460

     

    操作excel 最好的方式是使用vba进行提供的丰富大量的接口来操作excel ,但是如何使用ruby操作excel?

    1.通常做法,既然是微软提供的软件,就需要查阅,微软提供的API了,需要包含win32,在ruby,前加入,require 'win32ole'

    例如:

    require 'win32ole'

    myexcel = WIN32OLE.new("excel.application")

    myexcel.visible=true

    mywbk = myexcel.Workbooks.Add()

    mywst= mywbk.Worksheets(1)

    mywst.Range('A1:D1').value =['1','2','3','4']

    将['1','2','3','4']写入EXCEL的'A1:D1'区域

    2.使用纯ruby 读写excel

    @file_task_name ="e:/test.xls"

    @fo=File.open(@file_task_name,"r")

    # def total_lines @lines = 0 @fo.each_with_index {|@item,@lines|} puts @lines+1 @lines= @lines+1 File.open(@file_task_name) do |file| #file.each_line{|line| puts line} file.close();

    这里我推荐一款,操作excel的第三方工具

    1.Parseexcel插件(主要是excel读取)

    安装方式:gem install parseexcel

    require 'rubygems' require 'spreadsheet/excel'

    具体操作检测提供帮助文档

    #从命令行输入要打开的excel文件名 workbook = Spreadsheet::Parseexcel.parse"e:/test.xls")#得到第一个表单

    worksheet = workbook.worksheet(0) #遍历行

    worksheet.each { |row| j=0 i=0 if row != nil #遍历该行非空单元格

    row.each { |cell| if cell != nil #取得单元格内容为string类型

    contents = cell.to_s('latin1') puts "Row: #{j}

    Cell: #{i}> #{contents}"

    end i = i+1

    }

    end

    }

    2.spreadsheet插件(主要是excel生成写入数据)

    安装方式:gem install spreadsheet

    require ' parseexcel/parser'

    具体操作检测提供帮助文档

  • 相关阅读:
    事务传播机制,搞懂。
    洛谷 P1553 数字反转(升级版) 题解
    洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here 题解
    洛谷 P1055 ISBN号码 题解
    洛谷 P2141 珠心算测验 题解
    洛谷 P1047 校门外的树 题解
    洛谷 P1980 计数问题 题解
    洛谷 P1008 三连击 题解
    HDU 1013 题解
    HDU 1012 题解
  • 原文地址:https://www.cnblogs.com/qinyan20/p/4022181.html
Copyright © 2011-2022 走看看