my $oExcel = new Spreadsheet::XLSX ;
my $oBook = $oExcel->Parse( $filename) ;
####
my $oBook = Spreadsheet::XLSX->new($filename);
####
sub xls2csv {
my ($filename, $regions, $rotate) = @_ ;
my $sheet = 0 ;
my $output = "" ;
# extract any sheet number from the region string
$regions =~ m/^(\d+)-(.*)/ ;
if( $2) {
$sheet = $1 - 1 ;
$regions = $2 ;
}
# now extract the start and end regions
$regions =~ m/(.*):(.*)/ ;
if( !$1 || !$2) {
print STDERR "Bad Params";
return "" ;
}
my @start = sheetRef( $1) ;
my @end = sheetRef( $2) ;
if( !@start) {
print STDERR "Bad coordinates - $1";
return "" ;
}
if( !@end) {
print STDERR "Bad coordinates - $2";
return "" ;
}
if( $start[1] > $end[1]) {
print STDERR "Bad COLUMN ordering\n";
print STDERR "Start column " . int2col($start[1]);
print STDERR " after end column " . int2col($end[1]) . "\n";
return "" ;
}
if( $start[0] > $end[0]) {
print STDERR "Bad ROW ordering\n";
print STDERR "Start row " . ($start[0] + 1);
print STDERR " after end row " . ($end[0] + 1) . "\n";
exit ;
}
# start the excel object now
my $oBook = Spreadsheet::XLSX -> new($filename);
# my $oExcel = new Spreadsheet::XLSX ;
# my $oBook = $oExcel->Parse( $filename) ;
# open the sheet
my $oWkS = $oBook->{Worksheet}[$sheet] ;
# now check that the region exists in the file
# if not trucate to the possible region
# output a warning msg
if( $start[1] < 0) {
print STDERR int2col( $start[1]) . " < min col " . int2col(0) . " Resetting\n";
$start[1] = 0 ;
}
if( $end[1] > 701) {
print STDERR int2col( $end[1]) . " > max col " . int2col(701) . " Resetting\n" ;
$end[1] = 701 ;
}
if( $start[0] < 0) {
print STDERR "" . ($start[0] + 1) . " < min row " . (0 + 1) . " Resetting\n";
$start[0] = 0 ;
}
if( $end[0] > 1048575) {
print STDERR "" . ($end[0] + 1) . " > max row " . (1048575 + 1) . " Resetting\n";
$end[0] = 1048575 ;
}
my $x1 = $start[1] ;
my $y1 = $start[0] ;
my $x2 = $end[1] ;
my $y2 = $end[0] ;
if( !$rotate) {
for( my $y = $y1 ; $y <= $y2 ; $y++) {
for( my $x = $x1 ; $x <= $x2 ; $x++) {
my $cell = $oWkS->{Cells}[$y][$x] ;
$output .= $cell->Value if(defined $cell);
$output .= "," if( $x != $x2) ;
}
$output .= "\n" ;
}
} else {
for( my $x = $x1 ; $x <= $x2 ; $x++) {
for( my $y = $y1 ; $y <= $y2 ; $y++) {
my $cell = $oWkS->{Cells}[$y][$x] ;
$output .= $cell->Value if(defined $cell);
$output .= "," if( $y != $y2) ;
}
$output .= "\n" ;
}
}
return $output ;
}