Fangdidididi 发表于 2022-3-14 00:21

ruby转换当前目录下json文件为excel

本帖最后由 Fangdidididi 于 2022-3-14 00:24 编辑

require 'rubyXL'
require 'json'

`ls *.json`.split("\n").each do |name|
name = name.gsub(".json","")
workbook = RubyXL::Workbook.new
worksheet = workbook
File.open("#{name}.json","r").readlines.each_with_index do |line,i|
    puts "----------------#{i}"
   # next if i >80000
    puts doc = JSON.parse(line)
    if i == 0
      doc.keys.each_with_index do |k,l|
      worksheet.add_cell(i,l,k)
      end
    end
    p doc.keys
    doc.keys.each_with_index do |key,l|
      worksheet.add_cell(i+1,l,doc.to_s)
    end
end

workbook.write("#{name}.xlsx")
end

读取当前目录下的json后缀文件,并将json数据转换成excel
页: [1]
查看完整版本: ruby转换当前目录下json文件为excel