简体   繁体   中英

how to read sheet no 2 from csv file in ruby on rails and nodejs

I wrote a CSV file in excel with multiple sheets like sheet1 , sheet2 , sheet3 having different information and now I want to read CSV file. How can I access information in sheet2, sheet3 ... , using ruby on rails and Nodejs .

Perhaps using a more appropriate tool for the job might help you, there are numerous tools available, my favourite is RubyXL it will simply allow you to iterate through work sheets or work with specific sheet.

eg Gemfile

gem 'rubyXL'

then from the command line install the gem

bundle install

To open a spreadsheet

xlfile = RubyXL::Parser.parse('my_xl_file')

Obviously replacing 'my_xl_file' with whatever path your xl file lives in plus the file name

And to read a sheet by name

xlsheet = xlfile['WorkSheetName']

Or to iterate through the sheets you can use, by sheet position

xlsheet = xlfile[0]

or xlsheet = workbook.first xlsheet = workbook.next

use .count to get the number of sheets if you want to loop then to access cells you just need the row, col (row,col) eg the first cell

row = 1
col = 1
cell_value = xlsheet[row][col].value

read more here https://github.com/weshatheleopard/rubyXL

Similarly there is also the spreadsheet gem if the above doesn't suit. This will work with propritary xls files in a similar way

https://github.com/zdavatz/spreadsheet

so to answer your question

how to read sheet no 2 from csv file in ruby on rails and nodejs

My answer is "don't", Please use a better tool for the job

But if you really need to use nodeJS then the following should help

By default it reads the first sheet in the document. If you have multiple sheets in your spreadsheet then pass either sheet: number (sheet index, starting from 1) or sheet: string (sheet name) as part of the options argument (options.sheet is 1 by default):

readXlsxFile(file, { sheet: 2 }).then((data) => {
  ...
})

The above taken from https://www.npmjs.com/package/read-excel-file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM