excel2csv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ ~*.*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in excel2csv.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # Excel2CSV: gem for converting Excel files to csv
2
+
3
+ ### Installation and usage
4
+
5
+ gem install excel2csv
6
+
7
+ With Gemfile
8
+
9
+ gem 'excel2csv'
10
+
11
+ Excel2CSV.read "path/to/file.xls"
12
+ Excel2CSV.read "path/to/file.xlsx"
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc "Build java jar with mvn"
6
+ task "build-jar" do
7
+ java_excel2csv_home = "vendor/excel2csv-java"
8
+ system "cd #{java_excel2csv_home} && mvn clean package"
9
+ jar = FileList["#{java_excel2csv_home}/target/excel2csv*.jar"].first
10
+ if File.exist? jar.to_s
11
+ cp jar, "lib/excel2csv.jar"
12
+ else
13
+ raise "Can't find #{vendor/excel2csv-java/target}/excel2csv-x.x.x.jar. Java build is broken?"
14
+ end
15
+ end
16
+
17
+ desc "Run specs"
18
+ RSpec::Core::RakeTask.new do |t|
19
+ t.rspec_opts = %w(-fs --color)
20
+ end
21
+
22
+ task :default => :spec
data/excel2csv.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/excel2csv/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Yury Korolev"]
6
+ gem.email = ["yury.korolev@gmail.com"]
7
+ gem.description = %q{gem for converting Excel files to csv}
8
+ gem.summary = %q{extract excel worksheets to csv files}
9
+ gem.homepage = "https://github.com/anjlab/excel2csv-ruby"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.files.reject! { |fn| fn.include? "vendor/" }
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "excel2csv"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Excel2CSV::VERSION
18
+
19
+ gem.add_development_dependency "rspec", "<= 2.6"
20
+ end
@@ -0,0 +1,3 @@
1
+ module Excel2CSV
2
+ VERSION = "0.0.1"
3
+ end
data/lib/excel2csv.jar ADDED
Binary file
data/lib/excel2csv.rb ADDED
@@ -0,0 +1,74 @@
1
+ require "excel2csv/version"
2
+ require "csv"
3
+ require "tmpdir"
4
+ require "fileutils"
5
+
6
+ module Excel2CSV
7
+
8
+ class Info
9
+ attr_accessor :sheets
10
+ attr_accessor :tmp_dir
11
+ attr_accessor :working_dir
12
+
13
+ def self.read dir, tmp_dir
14
+ info = Info.new dir, tmp_dir
15
+ info.read
16
+ info
17
+ end
18
+
19
+ def read
20
+ @sheets = Dir["#{@working_dir}/*.csv"].map do |file|
21
+ {path: file}
22
+ end
23
+ end
24
+
25
+ def close
26
+ FileUtils.remove_entry_secure(@tmp_dir, true) if @tmp_dir
27
+ end
28
+
29
+ private
30
+
31
+ def initialize working_dir, tmp_dir
32
+ @working_dir = working_dir
33
+ @tmp_dir = tmp_dir
34
+ @sheets = []
35
+ end
36
+
37
+ end
38
+
39
+ def foreach(path, options = {}, &block)
40
+ convert(path, options) do |info|
41
+ CSV.foreach(info.sheets.first[:path], options, &block)
42
+ end
43
+ end
44
+
45
+ module_function :foreach
46
+
47
+ def read(path, options = {})
48
+ convert(path, options) do |info|
49
+ CSV.read(info.sheets.first[:path], options)
50
+ end
51
+ end
52
+
53
+ module_function :read
54
+
55
+ def convert(path, options = {})
56
+ begin
57
+ tmp_dir = Dir.mktmpdir
58
+ dest_folder = options[:dest_folder] || tmp_dir
59
+ java_options = options[:java_options] || "-Dfile.encoding=utf8 -Xms512m -Xmx512m -XX:MaxPermSize=256m"
60
+ `java #{java_options} -jar lib/excel2csv.jar #{path} #{dest_folder}`
61
+ info = Info.read dest_folder, tmp_dir
62
+ if block_given?
63
+ yield info
64
+ else
65
+ info
66
+ end
67
+ ensure
68
+ info.close if block_given?
69
+ end
70
+ end
71
+
72
+ module_function :convert
73
+
74
+ end
@@ -0,0 +1,41 @@
1
+ #encoding: utf-8
2
+ require 'excel2csv'
3
+
4
+ describe Excel2CSV do
5
+
6
+ let(:excel) {Excel2CSV}
7
+
8
+ it "reads xls files" do
9
+ data = excel.read "spec/fixtures/basic_types.xls"
10
+ data[0].should == ["1.00", "2011-12-23 21:00:00 UTC(+0000)", "Hello"]
11
+ data[1].should == ["2.00", "2011-12-24 21:00:00 UTC(+0000)", "Привет"]
12
+ data[2].should == ["3.00", "2011-12-25 21:00:00 UTC(+0000)", 'Привет, "я excel!"']
13
+ end
14
+
15
+ it "reads xlsx files" do
16
+ data = excel.read "spec/fixtures/basic_types.xlsx"
17
+ data[0].should == ["1.00", "2011-12-23 21:00:00 UTC(+0000)", "Hello"]
18
+ data[1].should == ["2.00", "2011-12-24 21:00:00 UTC(+0000)", "Привет"]
19
+ data[2].should == ["3.00", "2011-12-25 21:00:00 UTC(+0000)", 'Привет, "я excel!"']
20
+ end
21
+
22
+ it "iterates rows" do
23
+ count = 0
24
+ excel.foreach "spec/fixtures/basic_types.xls" do |row|
25
+ row.length.should == 3
26
+ count += 1
27
+ end
28
+ count.should == 3
29
+ end
30
+
31
+ it "removes tmp dir after work" do
32
+ tmp_dir = nil
33
+ excel.convert "spec/fixtures/basic_types.xlsx" do |info|
34
+ # puts IO.read(info.sheets.first[:path])
35
+ tmp_dir = info.tmp_dir
36
+ end
37
+ tmp_dir.should_not be_nil
38
+ Dir.exists?(tmp_dir).should == false
39
+ end
40
+
41
+ end
Binary file
Binary file
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'bundler'
5
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excel2csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Yury Korolev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70324209188260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - <=
20
+ - !ruby/object:Gem::Version
21
+ version: '2.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70324209188260
25
+ description: gem for converting Excel files to csv
26
+ email:
27
+ - yury.korolev@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.md
35
+ - Rakefile
36
+ - excel2csv.gemspec
37
+ - lib/excel2csv.jar
38
+ - lib/excel2csv.rb
39
+ - lib/excel2csv/version.rb
40
+ - spec/excel2csv_spec.rb
41
+ - spec/fixtures/basic_types.xls
42
+ - spec/fixtures/basic_types.xlsx
43
+ - spec/spec_helper.rb
44
+ homepage: https://github.com/anjlab/excel2csv-ruby
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.9
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: extract excel worksheets to csv files
68
+ test_files:
69
+ - spec/excel2csv_spec.rb
70
+ - spec/fixtures/basic_types.xls
71
+ - spec/fixtures/basic_types.xlsx
72
+ - spec/spec_helper.rb