使用ruby脚本对excel数据进行读写
一、基本的文件读写操作
- readLine读取操作
def readfile
puts "请输入您要导入的文件?"
path = gets.chomp
datas = []
begin
file = IO.readlines("#{
path}")
puts file
titname = []
file.each_with_index do |line,i|
a = line.split(",")
if i == 0
titname = a
end
linedata = {
}
titname.each_with_index do |tn,i|
linedata[tn] = a[i]
end
datas[i] = linedata
end
puts datas
puts "请输入您要查询的内容?"
find = gets.chomp
termname = find.split("#")[0]
termdata = find.split("#")[1]
data = datas[termdata.to_i]
puts "termname:"+data["#{
termname}"].to_s
puts "请输入您要修改的内容?"
phone = gets.chomp
puts "是否保存新文件?"
yesorno = gets.chomp
if yesorno == "是"
data["#{
termname}"] = phone
aFile = File.new("/Users/touyoshitaka/Desktop/test.csv", "r+")
if aFile
datas.each_with_index do |data,i|
myfile = ""
titname.each_with_index do |tn,j|
myfile << data[tn].chomp+","
end
file[i] =myfile
aFile.syswrite(file[i])
aFile.syswrite("\r\n")
end
puts "文件保存成功"
else
puts "文件保存失败"
end
else
puts "操作完成"
end
rescue => e
puts e
end
end
readfile
- 面向对象的方式封装
class ReadFile
def initialize(filepath)
@filepath = filepath
@datas = []
@titname = []
@termname = ""
@termdata = ""
@data = []
end
def readfile()
if @filepath.size == 0
@filepath = '/Users/touyoshitaka/Desktop/test.csv'
end
file = IO.readlines("#{
@filepath}")
file.each_with_index do |line,i|
a = line.split(",")
if i == 0
@titname = a
end
linedata = {
}
@titname.each_with_index do |tn,i|
linedata[tn] = a[i]
end
@datas[i] = linedata
end
puts @datas
end
def search(find)
@termname = find.split("#")[0]
@termdata = find.split("#")[1]
@data = @datas[@termdata.to_i]
puts "#{
@termname}:"+@data["#{
@termname}"].to_s
end
def update(phone)
file = []
@data["#{
@termname}"] = phone
aFile = File.new(@filepath, "r+")
if aFile
@datas.each_with_index do |da,i|
myfile = ""
@titname.each_with_index do |tn,j|
myfile << da[tn].chomp+","
end
file[i] =myfile
aFile.syswrite(file[i])
aFile.syswrite("\r\n")
end
puts "文件保存成功"
else
puts "文件保存失败"
end
end
end
class Perform
def initialize()
@rf
@phone
end
def read
puts "请输入您要导入的文件?"
filepath = gets.chomp
@rf = ReadFile.new(filepath)
@rf.readfile()
end
def seach
puts "请输入您要查询的内容?"
find = gets.chomp
@rf.search(find)
end
def update
puts "请输入您要修改的内容?"
@phone = gets.chomp
end
def save
puts "是否保存新文件?"
yesorno = gets.chomp
if yesorno == "是"
@rf.update(@phone)
else
puts "操作完成"
end
end
end
pf = Perform.new()
pf.read
pf.seach
pf.update
pf.save
- Excel数据存储到mysql中
require "mysql2"
def readWriteMysql
client = Mysql2::Client.new(
host: "127.0.0.1",
username: "root",
password: "123456",
database: "rubytools"
)
client.query("drop table tools")
client.query("create table tools(id int(20),
phone int(12),
areacode int(4))")
puts "请输入您要导入的文件?"
path = gets.chomp
file = IO.readlines("#{
path}")
titname = []
insertcol = ""
insertval = ""
file.each_with_index do |line,i|
a = line.split(",")
if i == 0
titname = a
else
titname.each_with_index do |tn,i|
if i == 0
insertcol = tn
insertval = a[i]
else
insertcol = insertcol+","+tn
insertval = insertval+","+a[i]
end
end
client.query("INSERT INTO tools (#{
insertcol}) VALUES (#{
insertval})")
end
end
puts "请输入您要查询的内容?"
find = gets.chomp
termname = find.split("#")[0]
termdata = find.split("#")[1]
seachData = client.query("SELECT #{
termname} FROM tools WHERE id=#{
termdata}")
seachData.each do |data|
puts termname+":"+data["phone"].to_s
end
puts "请输入您要修改的内容?"
phone = gets.chomp
puts "是否保存新文件?"
yesorno = gets.chomp
if yesorno == "是"
client.query("UPDATE tools SET #{
termname} = #{
phone} WHERE id = #{
termdata}")
puts "文件保存成功"
else
puts "操作完成"
end
end
readWriteMysql
二、总结
- 安装mysql2
gem install mysql2
- 连接数据库
client = Mysql2::Client.new(
host: "127.0.0.1",
username: "root",
password: "123456",
database: "rubytools"
)
- 对数据库读写进行操作
client.query("drop table tools")
client.query("create table tools(id int(20),
phone int(12),
areacode int(4))")
client.query("INSERT INTO tools (列名1,列名2...) VALUES (数据1,数据2....)")
result = client.query("SELECT #{
termname} FROM tools WHERE id=#{
termdata}")
result.each do |data|
puts data
end
client.query("UPDATE tools SET 字段名 = 数据 WHERE id = 1")