ruby读取exce文件,使用roo---Gem
module SEquipsHelper
#设备台账,从excel文件读取信息
require 'roo'
#require 'roo-xls' #读取excel文件
# SEquipsHelper.read_excel
def self.read_excel(path)
#path = 'http://172.168.10.7:6060/file/102.xlsx'
#xlsx = Roo::Spreadsheet.open('/Users/h/Documents/rubyProject/新版本文案.xls',extension: :xls) # if type == 'xlsx'
# end xlsx = Roo::Spreadsheet.open(path, extension: :xlsx) # if type == 'xls' #exce中日期类型 不能自动转化
# xlsx = Roo::Spreadsheet.open(path, extension: :xls)
# end #sheets = xlsx.sheets #返回所有的工作表名
# cell = xlsx.sheet(0).cell('C',1) #取第一个工作表的C1
# cell2 = xlsx.sheet(0).cell('D',2) cols = ['B','C','D','E','F','G','H','I','J','K','L','M','N']
ods = xlsx.sheet(0) #定义默认第一个表
#n1 = ods.first_column #第一列数
#n1 = ods.last_column #最后一列数
#n1 = ods.first_row #第一行数 n2 = ods.last_row #最后一行数
rows = n2 -2
Rails.logger.info "-----------总行数---#{n2}-----"
Rails.logger.info "-----------有效数据行数---#{rows}-----" @excel_datas =[] #存所有行的数据
rows.times do |r| #行数
row_data=[] #存每行的数据
cols.each do |c|
c1 = xlsx.sheet(0).cell(c, r+3)
Rails.logger.info "---------第#{r+3}行,第#{c}列---#{c1}-----"
row_data.push(c1)
end
@excel_datas.push(row_data)
end @excel_datas.each do |e|
#将区域转为 id
area_name = e[12]
area = SArea.find_by(:name => area_name)
s_area_id = area.present? ? area.id : ''
e[12] = s_area_id e.push(Time.now) #添加 时间
e.push(Time.now)
e.push(1) # 设备状态 equip_status 1 启用
end return @excel_datas
end #保存excel到数据库
# SEquipsHelper.save_excel
def self.save_excel(excel_datas)
Rails.logger.info "---save_data----#{excel_datas}---" # SEquip.bulk_insert(:bit_code,
# :equip_code,
# :equip_name,
# :equip_location,
# :equip_norm,
# :equip_nature,
# :equip_material,
# :equip_num,
# :apper_code,
# :apper_time,
# :factory,
# :equip_note,
# :s_area_id,
# :created_at,
# :updated_at,
# :equip_status,
# :s_region_code_id,
# :region_name,
# update_duplicates: true
# ) do |worker|
# excel_datas.each do |attrs|
# Rails.logger.info "导入台账信息----#{attrs}"
# worker.add(attrs)
# end
# end destination_columns = [:bit_code,
:equip_code,
:equip_name,
:equip_location,
:equip_norm,
:equip_nature,
:equip_material,
:equip_num,
:apper_code,
:apper_time,
:factory,
:equip_note,
:s_area_id,
:created_at,
:updated_at,
:equip_status,
:s_region_code_id,
:region_name] SEquip.bulk_insert(*destination_columns, update_duplicates: true) do |worker|
excel_datas.each do |attrs|
Rails.logger.info "导入台账信息----#{attrs}"
worker.add(attrs)
end
end
end # SEquipsHelper.save
# def self.save
# hash = [
# {:bit_code => 'L-FJ0101', :equip_code => 'GJSW12-08-005',:equip_name => '龙门吊(东)' },
# {:bit_code => 'L-FJ0102', :equip_code => 'GJSW12-08-005',:equip_name => '附防爆电机' }
# ] # SEquip.bulk_insert(update_duplicates: true) do |worker|
# hash.each do |attrs|
# Rails.logger.info "导入台账信息----#{attrs}"
# worker.add(attrs)
# end
# end # end end
调用:
#从excel导入台账信息
# post s_equips/excel_import
def excel_import
s_region_code_id = params[:s_region_code_id]
excel = params[:excel]
filename = excel.original_filename Rails.logger.info "----------filename------#{filename}-------"
names = filename.split(".")
file_type = names[1] if file_type == 'xlsx'
region_code = SRegionCode.find_by(:id => s_region_code_id)
region_name = region_code.present? ? region_code.region_name : ''
@excel_datas = SEquipsHelper.read_excel(excel) #从excel读取信息
@excel_datas.each do |e|
e.push(s_region_code_id.to_i)
e.push(region_name)
end
SEquipsHelper.save_excel(@excel_datas) #保存读取到的信息
render json: {status: 200}
# else
# render json: {status: '请选择.xlsx文件'}
end
end
ruby读取exce文件,使用roo---Gem的更多相关文章
- Ruby 读取文件
Ruby 读取文件 一次全读出来 textAll = File.read("fileName.txt") puts textAll 一次读取一行 file = File.open( ...
- C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)
环境:win7+iis7+Office2007 在asp.net网站中导出Excel文件后,再把文件导入到数据库中. 读取Excel文件时,打开连接出错. 错误为:外部表不是预期的格式 解决:检查了一 ...
- LitJson(读Exce文件写入到json文件):
读Exce文件写入到json文件汇总: //命名空间 using System.Collections; using System.Collections.Generic; using System. ...
- Unity3D移动平台动态读取外部文件全解析
前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖,总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑 ...
- python读取caffemodel文件
caffemodel是二进制的protobuf文件,利用protobuf的python接口可以读取它,解析出需要的内容 不少算法都是用预训练模型在自己数据上微调,即加载"caffemodel ...
- informatica读取FTP文件
以下为一个完整的informatica读取ftp文件,并导入到系统中. 第一步: 通过shell脚本下载压缩包文件 /server/infa_shared/crm_prod/shell/ftpFrom ...
- Java读取word文件,字体,颜色
在Android读取Word文件时,在网上查看时可以用tm-extractors,但好像没有提到怎么读取Word文档中字体的颜色,字体,上下标等相关的属性.但由于需要,要把doc文档中的内容(字体,下 ...
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- Javascript写入txt和读取txt文件的方法
文章主要介绍了Javascript写入txt和读取txt文件的方法,需要的朋友可以参考下1. 写入 FileSystemObject可以将文件翻译成文件流. 第一步: 例: 复制代码 代码如下: Va ...
随机推荐
- Codeforces1101G (Zero XOR Subset)-less 【线性基】【贪心】
题目分析: 考虑到这是一个区间的异或问题,不妨求出前缀和,令$sum[i] = Xor_{j=1}^{i}a[j]$. 对于区间$[l,r]$的异或结果,等于$sum[r] \oplus sum[l- ...
- robotframework用例标签的使用
*** Settings ***Force Tags req-42Default Tags owner-john smoke *** Variables ***${HOST} 10.0.1.42 ** ...
- 【XSY2774】学习 带花树
题目描述 给你一个图,求最大匹配. 边的描述方式很特殊,就是一次告诉你\(c_i\)个点:\(d_1,d_2,\ldots,d_{c_i}\),表示这些点两两之间都有连边,也就是说,这是一个团.总共有 ...
- 微信开发使用webstorm&phpstrom 配置
材料: 下载链接:https://pan.baidu.com/s/1pLn6jFl 密码:fgo5 -----(将其中的wecharCode.jar 下载下来,然后在webStorm 的 File ...
- linux命令:查看系统版本
debian系统 (1) lsb_release -a No LSB modules are available. Distributor ID: Debian Description: Debia ...
- BZOJ4695 最假女选手(势能线段树)
BZOJ题目传送门 终于体会到初步掌握势能分析思想的重要性了. 一开始看题,感觉套路还是很一般啊qwq.直接在线段树上维护最大值和最小值,每次递归更新的时候,如果不能完全覆盖就暴力递归下去.挺好写的欸 ...
- ZJOI 2019 划水记
作为一个极其蒟蒻的OIer,虽然没有省选资格但还是去见见世面. ZJOI2019一试是在浙江省镇海中学.听名字就很霸气. 学习OI的最后一年,记录下一些事情,即使最终走到最后也一无所获,也是一段美好的 ...
- Nginx加载模块
1. /usr/local/nginx/sbin/nginx -V 查看nginx版本与编译安装了哪些模块nginx version: nginx/1.10.3built by gcc 4.4.7 2 ...
- Linux网络那点事(CentOS、Ubuntu、Kali)
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux 在Ubuntu18.04中,传统的配置/etc/network/interfa ...
- A1144. The Missing Number
Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...