Git Product home page Git Product logo

excel-esv's Introduction

ESV

Ruby library/gem for Excel parsing and generation with the ease of CSV.

Exporting CSVs because Excel generation is too complex? No more! CSVs can also be difficult to open correctly, e.g. in Excel on Mac.

ESV will read and generate XLS files. There is currently no XLSX support, but Pull Requests are welcome.

By design, ESV only reads and writes simple values like integers, floats, strings and dates/datetimes. When parsing a spreadsheet with formulas, you will get their last value, if known.

Usage

Generate data

require "esv"

data = ESV.generate do |esv|
  esv << [ "Name", "Dogs", "Cats" ]
  esv << [ "Victor", 1, 4 ]
end

File.write("/tmp/test.xls", data)

Generate file

require "esv"

ESV.generate_file("/tmp/test.xls") do |esv|
  esv << [ "Name", "Dogs", "Cats" ]
  esv << [ "Victor", 1, 4 ]
end

Parse data

require "esv"

data = File.read("/tmp/test.xls")
output = ESV.parse(data)
# => [ [ "Name", "Dogs", … ], … ]

This assumes a file with a single worksheet and will raise otherwise.

Parse file

require "esv"

output = ESV.parse_file("/tmp/test.xls")
# => [ [ "Name", "Dogs", … ], … ]

This assumes a file with a single worksheet and will raise otherwise.

Generate in Ruby on Rails

In config/initializers/mime_types.rb:

Mime::Type.register ESV::MIME_TYPE, "xls"

As a model or whatever you prefer:

class MyExcelDocument
  def self.generate(name)
    ESV.generate { |esv| esv << [ "Hello #{name}" ] }
  end
end

Controller:

class MyController < ApplicationController
  include ESV::RailsController  # for send_excel

  def show
    data = MyExcelDocument.generate("Rails")
    send_excel(data)
    # or: send_excel(data, filename: "myfilename.xls")
  end

  def another_example
    respond_to do |format|
      format.html {  }
      format.xls { send_excel() }
    end
  end
end

Installation

Add this line to your application's Gemfile:

gem 'excel-esv'

And then execute:

$ bundle

Or install it yourself as:

$ gem install excel-esv

Credits and license

By Henrik Nyh for Auctionet.com under the MIT license.

Using a lot of code from LivingSocial's Excelinator, also under the MIT license.

This library is a thin wrapper around Spreadsheet which does the heavy lifting.

excel-esv's People

Contributors

henrik avatar olleolleolle avatar p-wall avatar joakimk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.