perl - unable to parse xlsx with Spreadsheet::Read -
i trying parse .xlsx file using spreadsheet::read same code works .xls files throws error .xlsx below procedure followed:
- we creating file upload(browse) button in .cgi file name "csv_path".
- when user submits form (of course multipart/form-data)
code on next page
use cgi; use spreadsheet::read; use spreadsheet::parseexcel; use data::dumper qw(dumper); #will catch uploaded file. $fname = $query->param("csv_path"); print dumper($fname); #just confirmation printed variable $fname #when .xls uploaded $fname contains: #$var1 = bless( \*{'fh::fh00001308_4_template.xls'}, 'fh' ); #when .xlsx uploaded $fname contains: #$var1 = bless( \*{'fh::fh00001308_4_template.xlsx'}, 'fh' ); #now read file "spreadsheet::read" $data_xls = readdata ( $fname, "strip"=>3, "dtfmt" => "mm/dd/yyyy");#here if pass .xlsx file name(stored on server) readdata() works properly. #print out of spreadsheet::read print "<pre>"; print dumper($data_xls); #when .xls uploaded $data_xls comes required data #when .xlsx uploaded below error occurs # xlsx parser cannot parse data: undefined subroutine fh::opened
please suggest if changes required or thing missing.
here instead of passing stream data directly suppose store file @ temporary location, , pass file name location readdata() function.
please refer below code:
use cgi; use spreadsheet::read; use spreadsheet::parseexcel; use data::dumper qw(dumper); $corrected_filename = $query->param('csv_path'); $corrected_filename =~ s/ /_/g; # $corrected_filename .= "$username"; #store file locally local $| = 1; ($bytesread,$buffer,$file); $fh = $query->upload('csv_path'); open(outf, '>' . "/tmp/upload-".$corrected_filename); while ($bytesread = read($fh, $buffer, 1024)) { print(outf $buffer); } close(outf); $data_xls = readdata ("/tmp/upload-".$corrected_filename, "strip"=>3, "dtfmt" => "mm/dd/yyyy");#here if pass .xlsx file name(stored on server) readdata() works properly. #print out of spreadsheet::read print "<pre>"; print dumper($data_xls);
and when code read finish can unlink file.
Comments
Post a Comment