简体   繁体   中英

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

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

        }
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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