简体   繁体   中英

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 .

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