Copy the cell format from .xlsx file using Spreadsheett::ParseXLSX to write another cell using EXCEL:WRITER::XLSX?

ぃ、小莉子 提交于 2020-03-25 16:57:27

问题


Please check the code below. if I remove the $format from the last line to $sheet_write->write($row, $col,$cell -> value) then it works fine.

I am declaring to add the format in excel_2 spreadsheet which would be written by "Excel::Writer::XLSX;" package. And I am coping the cell format($format= $cell->get_format();) from another reader parser "use Spreadsheet::ParseXLSX". Any help would be appreciated. I am expecting there is some mismatch between these 2 different methods.

#!/home/utils/perl-5.24/5.24.2-058/bin/perl -w
use strict;
use warnings;


use Excel::Writer::XLSX;
use Spreadsheet::ParseXLSX;


my $parser   = Spreadsheet::ParseXLSX->new();
my $workbook = $parser->parse('abc.xlsx');
my $excel_2 = Excel::Writer::XLSX -> new ('abc_copied.xlsx');

my $format = $excel_2->add_format();

if ( !defined $workbook ) {
    die $parser->error(), ".\n";
}


for my $worksheet ( $workbook->worksheets() ) {

    my ( $row_min, $row_max ) = $worksheet->row_range();
    my ( $col_min, $col_max ) = $worksheet->col_range();
    printf("Sheet: %s\n", $worksheet->{Name});

    my $sheet_write = $excel_2->add_worksheet($worksheet->{Name});

    for my $row ( $row_min .. $row_max ) {
    for my $col ( $col_min .. $col_max ) {

            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            print "Row, Col    = ($row, $col)\n";
            #print "Value       = ", $cell->value(),       "\n";
            #print "Unformatted = ", $cell->unformatted(), "\n";
            #print "\n";
            $format= $cell->get_format();
            $sheet_write->write($row, $col,$cell -> value,$format);

        }
    }
}

回答1:


Spreadsheet::ParseXLSX's cell->get_format yields a different object than what Excel::Writer::XLSX is expecting as the format object in its write method. Even though they both deal with spreadsheets, they are different modules and will very unlikely share a class in this manner.

The properties of Excel::Writer::XLSX's format object are well documented:

https://metacpan.org/pod/Excel::Writer::XLSX#CELL-FORMATTING

I see there is a clone format module:

https://metacpan.org/pod/Excel::CloneXLSX::Format

No personal experience with it, but it looks promising... Otherwise you will probably have to dump the contents of the Parse module, figure out what's most important to you, and then do the translations yourself. But seriously, try the module.

Alternatively, if this is on a Windows machine, Win32::OLE may be better to handle this type of task for you if you are tethered to Perl (which needless to say would not be my first choice if your sole focus is Excel spreadsheet operations).



来源:https://stackoverflow.com/questions/60645343/copy-the-cell-format-from-xlsx-file-using-spreadsheettparsexlsx-to-write-anot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!