Shark Shark - 3 months ago 11x
Ruby Question

Using file.readlines.size to get the total number of lines of xlsx files in Ruby, but it returned the wrong number of rows

In ruby, I tried to use file.readlines.size to get the total number of rows in xlsx files, but there are 1200 rows in my files, file.readlines.size return me 322 insdead of 1200. I want to know why it's wrong and what's is the correct way to get the number of rows in a xlsx file. Thank you


It returns the wrong size because file.readlines looks for line breaks to separate lines from one another. This only works with text files and xlsx is either binary or xml-based file. You need to parse the file using a specialized parser to find out how many spreadsheet rows are in it. One gem which does it is Roo.