简体   繁体   English

无法使用Spreadsheet :: Read解析xlsx

[英]unable to parse xlsx with Spreadsheet::Read

I am Trying to parse a .xlsx file using Spreadsheet::Read same code works for .xls files but throws an error .xlsx below is procedure followed: 我试图用解析一个.xlsx文件电子表格::阅读相同的代码工作的.xls文件,但下面抛出一个错误的.xlsx是程序如下:

  1. We are creating a file upload(browse) button in .cgi file with name "csv_path" . 我们正在.cgi文件中创建一个名为“ csv_path”的文件上传(浏览)按钮。
  2. when user submits the form (of course its multipart/form-data ) 用户提交表单时(当然是表单的multipart / form-data
  3. code on next page 下一页上的代码

     use CGI; use Spreadsheet::Read; use Spreadsheet::ParseExcel; use Data::Dumper qw(Dumper); #will catch uploaded file. my $fname = $query->param("csv_path"); print Dumper($fname); #just for confirmation we printed variable $fname #when .xls is uploaded $fname contains: #$VAR1 = bless( \\*{'Fh::fh00001308_4_template.xls'}, 'Fh' ); #when .xlsx is uploaded $fname contains: #$VAR1 = bless( \\*{'Fh::fh00001308_4_template.xlsx'}, 'Fh' ); #now read the file with "Spreadsheet::Read" my $data_xls = ReadData ( $fname, "strip"=>3, "dtfmt" => "mm/dd/yyyy");#here if we pass .xlsx file name(stored on server) to ReadData() it works properly. #print out of Spreadsheet::Read print "<pre>"; print Dumper($data_xls); #when .xls is uploaded $data_xls comes up with all required data #when .xlsx is uploaded below error occurs # XLSX parser cannot parse data: Undefined subroutine Fh::opened 

Please suggest if any changes required or any thing missing. 请建议是否需要任何更改或缺少任何内容。

Here instead of passing stream data directly we are suppose to store the file at temporary location, and then pass the file name with location to the ReadData() function. 在这里,我们不是直接传递流数据,而是将文件存储在临时位置,然后将具有位置的文件名传递给ReadData()函数。
Please refer below code: 请参考以下代码:

use CGI;
use Spreadsheet::Read;
use Spreadsheet::ParseExcel;
use Data::Dumper qw(Dumper);

my $corrected_filename = $query->param('csv_path');
$corrected_filename =~ s/ /_/g;
# $corrected_filename .= "$username";
#store file locally
local $| = 1;
my ($bytesread,$buffer,$file);
my $fh = $query->upload('csv_path');
open(OUTF, '>' . "/tmp/upload-".$corrected_filename);
while ($bytesread = read($fh, $buffer, 1024)) {
    print(OUTF $buffer);
}
close(OUTF);
my $data_xls  = ReadData ("/tmp/upload-".$corrected_filename, "strip"=>3, "dtfmt" => "mm/dd/yyyy");#here if we pass .xlsx file name(stored on server) to ReadData() it works properly.


#print out of Spreadsheet::Read
print "<pre>";
print Dumper($data_xls);

And when code read finish we can unlink the file. 当代码读取完成时,我们可以取消链接文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM