2011-09-15 83 views
1

我很難找到一種方法來獲取Spreadsheet :: WriteExcel的write_row/write_col來打印Excel表格中的行。幫助電子表格:: WriteExcel perl

我有這種工作方法將它們打印爲文本文件。但是將它們作爲行/列寫入Excel表是我失敗的地方。 我的$ com_sam是這樣的。它是一個多級散列,其中包含所有匹配的樣本以及它們的百分比作爲關鍵字,以及它們與每個樣本匹配的所有其他詳細信息(X3,X32,E32_P,E32_PL是樣本)



    'X3' => { 
           '100.00' => [ 
               { 
               'NoofCalls' => 30, 
               'percent' => '100.00', 
               'NoofNs' => 0, 
               'match' => 30, 
               'sample' => 'X3' 
               }, 
               { 
               'NoofCalls' => 30, 
               'percent' => '100.00', 
               'NoofNs' => 0, 
               'match' => 30, 
               'sample' => 'X32' 
               }, 
               { 
               'NoofCalls' => 30, 
               'percent' => '100.00', 
               'NoofNs' => 0, 
               'match' => 30, 
               'sample' => 'E32_P' 
               }, 
               { 
               'NoofCalls' => 30, 
               'percent' => '100.00', 
               'NoofNs' => 0, 
               'match' => 30, 
               'sample' => 'E32_PL' 

               }, 

這裏是我的代碼,試圖使用WriteExcel將它們打印到Excel中。


    foreach my $percent (sort { $b $a } keys %{ $com_sam->{ $s1 } }){ 
    249 
    250  my $match_samples = $com_sam->{ $s1 }->{ $percent }; 
    251 
    252  foreach my $matSam(@ { $match_samples }){ 
    253  if(($s1 ne $matSam->{ sample }) and ($matSam->{ percent } >= $top_percent)) { 
    254 
    255   next if compare($s1, $matSam->{sample}); 
    256 #   print "****,$s1,$matSam->{ sample },$matSam->{ percent },$top,$top_percent,$matSam->{ NoofCalls },$matSam->{ match }, $matSam->{ NoofNs },****\n" ; 
    257   push @$log_array,($s1,$matSam->{ sample },$matSam->{ percent },$top,$top_percent,$matSam->{ NoofCalls },$matSam->{ match }, $matSam->{ NoofNs }); 
    258   push @$array_ref_log, @$log_array; 
    259   for my $col (0 ..7) { 
    260 
    261    for my $row (1 .. scalar(@sam2com)) { 
    262 
    263     $worksheet->write_row($row,$col,$array_ref_log);#Wat I get with this is just a single sample compared rather than all the sample. 

    264    } 
    265 
    266 
    267   } 
    268  }else{ 
    269 
    270   my $total_calls = $matSam->{ NoofCalls } + $matSam->{ NoofNs }; 
    271 
    272   my $l = sprintf "%s, %s, %0.2f, %s, %0.2f,%s,%s,%s", $s1, $matSam->{ sample }, $matSam->{ percent }, $top, $top_percent,$matSam->{ NoofCalls },$matSam->{ match }, $matSam->{ NoofNs } ; 
    273 
    274   if($total_calls == 97) { 
    275 
    276 #   print "$l\n" if ($matSam->{ NoofNs } { NoofNs } sample 
    287 }#end of percentage foreach loop 

我希望看到這樣的


    X3,X3,100.00,X3,100.00,30,30, 0 
    X3,X32_P,100.00,X3,100.00,30,30, 0 
    X3,E32,100.00,X3,100.00,30,30, 0 

的結果,而是我明白了這樣的在Excel中。 (當然不是逗號分隔:))


    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 
    X3,X32,100.00,X3,100.00,30,30, 0,X3,X32_P,100.00,X3,100.00,30,30, 0,X3,E32,100.00,X3,100.00,30,30, 0 

應該如何處理array_ref for write_row/write_col? 在此先感謝

+0

'$ array_ref_log'似乎沒有被設置 - 這不能是您運行得到該輸出的實際代碼...? – plusplus

+0

對不起,我評論過它。但是我沒有注意到我運行腳本。問題在於這一點。這就是爲什麼我發佈整個腳本。 – kutipi

回答

1

很難說只是這段代碼的問題,但我可以提出一些建議。

第一種是不使用write_row(),因爲它只是語法糖,並且使用自己的for()循環和write()來代替。

第二個是,如果你使用write_row(),確保你清楚它的嵌套數組引用的行爲方式。研究write_row documentation中的示例,看看它是否符合您的期望。可能是因爲您需要write_col()

最後,這不是一個真正的Spreadsheet::WriteExcel問題,並且更多地涉及數據結構的處理。您可以用簡單的print()語句替換write *函數,直到您確定已捕獲需要捕獲的數據,然後添加所需的WriteExcel函數。

0

要在XLSX文件中寫入大量數據,可以使用Excel_Writer_XLSX perl mudule。這是非常簡單的XLSX迭代和打印數據如下

 [email protected] = ([ "10001", "1/5/2011", "Jan", "Midwest", "Ami", "Binder", "94", "20" ], 
        [ "10002", "1/13/2011", "May", "West Coast", "Stevenson", "Pencil", "3", "275" ], 
        [ "10051", "2/24/2011", "Nov", "Midwest", "Jones", "Desk", "35", "4.99" ], 
        [ "10086", "3/7/2011", "Apr", "New England", "Andrews", "Pen Set", "16", "20" ], 
        [ "10450", "4/10/2011", "Dec", "Midwest", "Adams", "Ball", "20", "57.2" ], 
        [ "16001", "7/19/2011", "Mar", "West Coast", "Thompson", "Note Book", "28", "33.5" ], 
        [ "19565", "6/23/2011", "Jul", "Midwest", "Dwyer", "Scale", "15", "16" ], 
        [ "20048", "6/15/2011", "Oct", "Midwest", "Morgan", "Stickers", "96", "12" ], 
        [ "50962", "2/7/2011", "Feb", "New England", "Howard", "Clips", "52", "5.5" ],); 

my $workbook  = Excel::Writer::XLSX->new($outputFile); 
$workbook->set_optimization(); 
my $worksheet = $workbook->add_worksheet('Test'); 
my $row   = 0; 
my $col   = 0; 
foreach my $record (@records){ 
     $row++; 
     $col = 0; 

     foreach my $data (@{$record}){ 
      $worksheet->write($row,$col,$data);  
      $col++; 
     } 
    } 

從這個鏈接https://github.com/AarthiRT/Excel_Writer_XLSX請參閱「Write_largeData_XLSX.pl」的更多細節。