I want to get the number of lines in the Excel file in perl.

Asked 1 years ago, Updated 1 years ago, 80 views

Currently, I am reading and processing Excel-2007 file (.xlsx) in Perl's CGI program.
in CPAN  Spreadsheet::XLSX->new (file path);
You are using the , but if you have more than 10000 lines of files, you can run the above lines for more than a minute.Slow response is fatal because it is a web application.
I looked for packages other than Spreadsheet::XLSX that are fast to process, but I haven't found them yet.
As an alternative to not finding it, we are thinking of the logic of not running the above line when there are many, but we have not found a way to get the number without running the above line.

Please let me know if there is a way to read many lines of Excel at high speed or get the number of Excel at high speed.

perl

2022-09-30 20:57

1 Answers

If you take the sheet namesheet1 line number of file name hoge.xlsx, is this what it looks like?This is a sample that does not take into account abnormalities.

SpreadSheet::XLSX is super simplified, so it may take as long.

#!/usr/bin/env perl
use strict;
use warnings;
use Archive::Zip;

my$zip=Archive::Zip->new();
$zip->read('hoge.xlsx') == Archive::Zip::AZ_OK or die;
my$sheet1 = $zip->memberNamed('xl/worksheets/sheet1.xml') or die;
my$xml=$sheet1->contents;
my$rows=0;
while($xml=~/<r="(\d+)"/g){
    $rows = $1 if $rows <$1;
}
print$rows, "\n";

Add

"If you reflect the advice of argus and Shimizu's ""I want to specify a seat name,"" this is what it looks like."

#!/usr/bin/env perl
use strict;
use warnings;
notf8;
use Archive::Zip;

my$xlsfile='hoge.xlsx';
my$sheetname='Blow Sheet';

my$zip=Archive::Zip->new();
$zip->read($xlsfile) == Archive::Zip::AZ_OK or die;

my$rels_xml=($zip->memberNamed('xl/_rels/workbook.xml.rels') or die)->contents;
my%rels=();
while($rels_xml=~/\<Relationship(\s.*?)\/?\>/gs){
    my$rels_elms=$1;
    $rels_elms=~/\sTarget="(.*?)"/or next;
    my$path=$1;
    $rels_elms=~/\sId="(.*?)"/sor next;
    $rels{$1}=$path;
}

my$book_xml=($zip->memberNamed('xl/workbook.xml') or die)->contents;
my$rid;
while($book_xml=~/\<sheet(\s.*?)\/\>/gs){
    my$book_elms=$1;
    if($book_elms=~/\sname="$sheetname"/so){
        $book_elms=~/\sr:id="([^"]+)"/soldie;
        $rid = $1;
        last;
    }
}
$rid or die;

my$sheet=$zip->memberNamed("xl/$rels{$rid}") or die;
$sheet->contents=~/<dimension\s+ref="[A-Z]+(\d+):[A-Z]+(\d+)">/soldie;
print $2-$1+1, "\n";


2022-09-30 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.