ruby csv2excel.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby csv2excel.rb相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'minitest/autorun'
require 'optparse'
require 'stringio'
require 'tempfile'
require 'win32ole'
require './csv2excel'

class TestCsv2Excel < Minitest::Unit::TestCase
  def setup
    @data = <<-END_CSV.gsub(/^\s+/, '')
    'Date','System','User'
    '2050-10-01 01:00:00','9.8246527778e-01','1.0555555556e-02'
    '2050-10-01 03:00:00','1.0382986111e+00','0.0000000000e+00'
    END_CSV
    @temp_csv = Tempfile.new %w[temp .csv]
    @temp_csv.close
    @path = @temp_csv
    File.open @path, 'wb' do |file|
      file << @data
    end
    opt = OptionParser.new
    test_opt = ['--in_csv',
                "./csv/xyz.csv",
                '--in_column', 'Date',
                '--in_skip_lines', '0',
                '--out_excel', './xlsx/xyz.xlsx',
                '--out_sheet', 'Sheet1',
                '--out_range', 'A2:A100']
    @o = opt.getopts(test_opt, '', 'in_csv:', 'in_column:', 'in_skip_lines:0',
                     'out_excel:', 'out_sheet:', 'out_range:')
  end

  def test_initialize
    t = Csv2Excel.new(@o)
    assert_equal t.absolute_path(@o['in_csv']), t.in_csv
    assert_equal 'Date', t.in_column
    assert_equal true, t.in_skip_lines.integer?
    assert_equal t.absolute_path(@o['out_excel']), t.out_excel
    assert_equal 'Sheet1', t.out_sheet
    assert_equal 'A2:A373', t.out_range
  end

  def test_open_csv
    expected = ['2050-10-01 01:00:00', '2050-10-01 03:00:00']
    t = Csv2Excel.new(@o)
    assert_equal expected, t.open_csv(@path, t.in_column, t.in_skip_lines)
  end

  def test_open_book
    t = Csv2Excel.new(@o)
    t.open_book t.out_excel do |book|
      sheet = book.Worksheets t.out_sheet
      str = sheet.Range('A1').Value
      assert_equal false, str.nil?
    end
  end

  def test_write_excel
    expected = ['2050-10-01 01:00:00 +0900', '2050-10-01 03:00:00 +0900']
    t = Csv2Excel.new(@o)
    t.open_book t.out_excel do |book|
      sheet = book.Worksheets t.out_sheet
      cells = sheet.Range t.out_range
      cells.value = ''
      col_array = t.open_csv @path, t.in_column, t.in_skip_lines
      cells.to_a.each_with_index { |cell, i| cell.Value = col_array[i] }
      assert_equal expected, [cells[1].value.to_s, cells[2].value.to_s]
      book.Save
    end
  end
end
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# ruby csv2excel.rb --in_csv "./csv/abc.csv" --in_column "Date" --in_skip_lines 0 --out_excel "./xlsx/xyz.xlsx" --out_sheet "Sheet1" --out_range "A2:A100"
class WIN32OLE
  def to_a
    [].tap do |array|
      each { |o| array.push o }
    end
  end
end

class Csv2Excel
  attr_reader :in_csv, :in_column, :in_skip_lines,
              :out_excel, :out_sheet, :out_range
  def initialize(params)
    @in_csv = absolute_path params['in_csv']
    @in_column = params['in_column']
    @in_skip_lines = params['in_skip_lines'].to_i
    @out_excel = absolute_path params['out_excel']
    @out_sheet = params['out_sheet']
    @out_range = params['out_range']
  end

  def open_book(file)
    excel = WIN32OLE.new 'Excel.Application'
    excel.visible = false
    # excel.displayAlerts = false

    begin
      book = excel.Workbooks.Open file
      yield book
    rescue WIN32OLERuntimeError => e
      e.to_str
    ensure
      book.Close
      excel.Quit
    end
  end

  def write_excel
    open_book @out_excel do |book|
      sheet = book.Worksheets @out_sheet
      cells = sheet.Range @out_range
      cells.value = ''
      col_array = open_csv @in_csv, @in_column, @in_skip_lines
      cells
        .to_a
        .each_with_index { |cell, i| cell.Value = col_array[i] }
      book.Save
    end
  end

  def absolute_path(file)
    fso = WIN32OLE.new 'Scripting.FileSystemObject'
    fso.GetAbsolutePathName file
  end

  def open_csv(file, col, skip_lines)
    csv_droped = CSV.read(file).drop(skip_lines)
    csv = CSV.new(
      csv_droped.map(&:to_csv).join(''), headers: true
    )
    table = csv.read
    table[col]
  end
end

if $PROGRAM_NAME == __FILE__
  params = ARGV.getopts('', 'in_csv:', 'in_column:', 'in_skip_lines:0',
                        'out_excel:', 'out_sheet:', 'out_range:')
  win = Csv2Excel.new(params)
  win.write_excel
end

以上是关于ruby csv2excel.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee