ruby xml excel

require 'win32ole'


fns = Dir.glob("*.xls")

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

application.visible = TRUE


f = File.new('errorCauseEN.xml','w')

f.puts('<?xml version="1.0" encoding="utf-8"?>')
f.puts('<!-- edited with XMLSPY v5 U (http://www.xmlspy.com) by et8 (et8) -->')
f.puts('<Category>')


fns.each do |file|
 workbook = application.WorkBooks.Open(Dir.pwd + '/' + file)
 sheet = workbook.WorkSheets(2)
 sheet.Activate
 i = 2
 
 loop do
  break if !sheet.Range('A'+ i.to_s).Value
  
  break if !sheet.Range('C'+ i.to_s).Value
  
  errname = sheet.Range('A'+ i.to_s).Value.to_s.hex.to_i.to_s.strip
  
  valuename = sheet.Range('C'+ i.to_s).Value.strip
  
  valuename.gsub!(//"/,'/'')
  
  f.puts(' '*4 + '<ErrorCode name="' + errname + '" value="' + valuename +'"/>')
   
  i = i + 1
 end
 
 puts file.to_s + ' is OK!'
 workbook.close(0)
end

f.puts('</Category>')
f.close

application.Quit()
#workbook = application.WorkBooks.Open('D:/ErrorCase/TR_ErrCode.xls')

 

--------------------------------------------------------------------------

 

require 'win32ole'
require 'rexml/document'
include REXML
include REXML::

#fns = Dir.glob("*.xls")
#
application = WIN32OLE.new("excel.application") 
#
application.visible = TRUE
#
#fns.each do |file|
# workbook = application.WorkBooks.Open(Dir.pwd + '/' + file)

#end
#
f = File.new('errorC.xml','w')
doc = Document.new('<Category />')


workbook = application.WorkBooks.Open('D:/ErrorCase/TR_ErrCode.xls')

sheet = workbook.WorkSheets(2)

sheet.Activate

i = 2

loop do
 break if !sheet.Range('A'+ i.to_s).Value
 
 ec = Element.new('ErrorCode')
 
 ec.add_attribute('name',sheet.Range('A'+ i.to_s).Value.to_s.hex.to_i.to_s)
 
 ec.add_attribute('value',sheet.Range('C'+ i.to_s).Value)
 
 doc.root.add_element(ec)
 
 i = i + 1
end

doc.write(f)

f.close

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值