简体   繁体   English

电子表格帮助:: WriteExcel perl

[英]Help with Spreadsheet::WriteExcel perl

I am having a hard time in working a way to get the write_row/write_col of Spreadsheet::WriteExcel to print the rows in the excel sheet. 我很难找到一种方法来获取Spreadsheet :: WriteExcel的write_row / write_col以打印excel工作表中的行。

I have this working method to print them as a text file. 我有这种工作方法将它们打印为文本文件。 But writing them to the excel sheet as a row/column is where I am failing. 但是我将它们作为行/列写入excel表是我失败的地方。 My $com_sam is like this. 我的$ com_sam就是这样。 Is it is a multi-level hash which holds all the samples that matches and their percentage as a key and all the other details of their match with each sample.(X3, X32,E32_P,E32_PL are the samples) 它是一个多级哈希,包含所有匹配的样本及其百分比作为键,以及与每个样本匹配的所有其他详细信息。(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'

                                               },

Here is my code trying to print them to excel using WriteExcel. 这是我的代码,试图使用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

I would like to see the result like this 我想看到这样的结果


    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

but rather I get it like this in excel. 但是我在excel中却是这样。 (ofcourse not comma separated :) ) (当然不要用逗号分隔:))


    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

How should be working on the array_ref for write_row/write_col? 在write_row / write_col的array_ref上应该如何工作? Thanks in advance 提前致谢

It is hard to tell what the issue is from just this snippet of code but I can make a few suggestions. 仅从这段代码很难说出问题是什么,但是我可以提出一些建议。

The first is to not use write_row() , since it is merely syntactic sugar, and use your own for() loop and write() instead. 首先是不要使用write_row() ,因为它只是语法糖,而是使用自己的for()循环和write()

The second is that, if you do you use write_row() , make sure that you are clear about the way it behaves with nested array refs. 第二个问题是,如果您使用write_row() ,请确保您清楚它与嵌套数组引用的行为方式。 Study the example in the write_row documentation to see that it matches your expectations. 研究write_row文档中的示例,以确保它符合您的期望。 It may be that you need write_col() instead. 可能是您需要write_col()代替。

Finally, this isn't really a Spreadsheet::WriteExcel issue and relates more to the handling of the data structure. 最后,这实际上不是Spreadsheet :: WriteExcel问题,它更多地涉及数据结构的处理。 You could replace the write* function with a simple print() statement until you are sure that you have captured the data you need to capture and then add the required WriteExcel function. 您可以用简单的print()语句替换write *函数,直到确定已捕获需要捕获的数据,然后添加所需的WriteExcel函数。

To write the large set of data in XLSX file Excel_Writer_XLSX perl mudule can be used. 要在XLSX文件中写入大量数据,可以使用Excel_Writer_XLSX perl模子。 And it is very simple to iterate and print the data in XLSX as follows 如下所示,在XLSX中迭代和打印数据非常简单

     my@records = ( [ "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++;
        }
    }   

Refer "Write_largeData_XLSX.pl" from this link https://github.com/AarthiRT/Excel_Writer_XLSX for more details. 请从此链接https://github.com/AarthiRT/Excel_Writer_XLSX参阅“ Write_largeData_XLSX.pl”以了解更多详细信息。

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

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