簡體   English   中英

無法使用Spreadsheet :: Read解析xlsx

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

我試圖用解析一個.xlsx文件電子表格::閱讀相同的代碼工作的.xls文件,但下面拋出一個錯誤的.xlsx是程序如下:

  1. 我們正在.cgi文件中創建一個名為“ csv_path”的文件上傳(瀏覽)按鈕。
  2. 用戶提交表單時(當然是表單的multipart / form-data
  3. 下一頁上的代碼

     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 

請建議是否需要任何更改或缺少任何內容。

在這里,我們不是直接傳遞流數據,而是將文件存儲在臨時位置,然后將具有位置的文件名傳遞給ReadData()函數。
請參考以下代碼:

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);

當代碼讀取完成時,我們可以取消鏈接文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM