简体   繁体   中英

Merged cells are unmerging on Save, using Spreadsheet::ParseExcel

I am writing a program to parse an .xls file. For that I have a template which contains five merged cells (B1,C1,D1,E1,F1) and written "User-Dependent errors" in that. In B2,C2,D2,E2,F2 I have written the error names and want to save their count every day. The code is working properly but after parsing and saving the merged cell (B1,C1,D1,E1,F1) is getting unmerged and the text is presented in B1. I need the merged cells as is (merged), even after parsing.

What do I have to do?

#!/usr/bin/perl
use strict;
use warnings;

use DBI;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;

my $date=$ARGV[1]; #yymmdd
my $hour=$ARGV[0]; #06

$date or $date=`date --date='1 day ago' +%Y%m%d`;
chomp $date;
chomp $hour;

my $db_name   = "ravi";
my $table     = "CDR";
my $sub_table = "Submission_Failures";
my $del_table = "Delivery_Failures";
my $host      = "xxx.xx.x.xxx";

my $command = "cp /root/prac/CDR/CDR.xls /root/prac/CDR/CDR_Report_20$date$hour.xls";
print $command;
`$command`;

sub NULL_count
{
    my $type = $_[0];
    my @temp_array;
    my $error_db = DBI->connect("DBI:mysql:database=$db_name;host=$host;mysql_socket=/opt/lampstack-5.5.27-0/mysql/tmp/mysql.sock","root","", {'RaiseError' => 1});
    my $error_sth = $error_db->prepare("SELECT Error_list from error_potrait WHERE Date='$date' and Type='$type'");
    $error_sth->execute() or die $DBI::errstr;
    while (my $temp = $error_sth->fetchrow_array())
    {
            push(@temp_array, $temp);
    }
    my $temp = @temp_array;
    foreach my $i ($temp .. 4)
    {
            $temp_array[$i] = "NULL";
    }
    $error_sth->finish();
    return @temp_array;
}

my @db_system_errors   = NULL_count ("Submission_user_error");
my @db_network_errors  = NULL_count ("Submission_ESME_error");
my @db_ESME_errors     = NULL_count ("Submission_system_error");
my @db_user_errors     = NULL_count ("Submission_network_error");
my @del_user_errors    = NULL_count ("Delivery_user_error");
my @del_network_errors = NULL_count ("Delivery_network_error");
my @del_system_errors  = NULL_count ("Delivery_system_error");
my @submission_errors  = (@db_network_errors,@db_system_errors,@db_ESME_errors,@db_user_errors);
my @delivery_errors    = (@del_user_errors,@del_network_errors,@del_system_errors);

sub error_headers
{
    my $sheet_no = shift;
    my @array    = @_;
    my $row      = 1;
    my $col      = 1;

    # Open an existing file with SaveParser
    my $parser   = Spreadsheet::ParseExcel::SaveParser->new();
    my $template = $parser->Parse("CDR_Report_20$date$hour.xls") or die "Cant open xls";

    # Get the first worksheet.
    my $sheet = $template->worksheet($sheet_no);
    $sheet->AddCell( 1, 0, $date );

    foreach my $value (@array)
    {
            $sheet->AddCell( $row, $col, $value );
            ++$col;
    }
    $template->SaveAs("CDR_Report_20$date$hour.xls");
}

error_headers (3,@submission_errors);
error_headers (4,@delivery_errors);

sub parser_excel
{
    my $sql_comm = $_[0];
    my $sheet_no = $_[1];
    my $row      = $_[2];
    my $col      = $_[3];

    my $dbh = DBI->connect("DBI:mysql:database=$db_name;host=$host;mysql_socket=/opt/lampstack-5.5.27-0/mysql/tmp/mysql.sock","root","",      {'RaiseError' => 1});
    #Selecting the data to fetch
    my $sth = $dbh->prepare("$sql_comm");
    $sth->execute() or die $DBI::errstr;
    # Open an existing file with SaveParser
    my $parser   = Spreadsheet::ParseExcel::SaveParser->new();
    my $template = $parser->Parse("CDR_Report_20$date$hour.xls") or die "Cant open xls";
   # Get the first worksheet.
    my $sheet = $template->worksheet($sheet_no);
    $sheet->AddCell( $_[4], 0, $date );

    while (my @row = $sth->fetchrow_array())
    {
            my $Date_db = shift @row;
            foreach my $value (@row)
            {
                    $sheet->AddCell( $row, $col, $value );
                    ++$col;
            }
            $row++;
            $col=0;
    }
    $template->SaveAs("CDR_Report_20$date$hour.xls");
    $sth->finish();
}

parser_excel("Select * from $table where Date = $date and Hour = $hour",2,1,0,0);
parser_excel("Select * from $sub_table where Date = $date and Hour = $hour",3,2,0,1);
parser_excel("Select * from $del_table where Date = $date and Hour = $hour",4,2,0,1);`

The docs for Spreadsheet::ParseExcel::SaveParser state that the module works "by reading it with Spreadsheet::ParseExcel and rewriting it with Spreadsheet::WriteExcel". So any merged cells will be lost when re-writing. You will need to use the WriteExcel module to re-create the merged cells, which means you will have to separate the reading and writing in your own script.

To merge the cells you use the "merge_range" method with a Format:

my $format = $workbook->add_format( align => 'left' );
$worksheet->merge_range('B1:F1', 'User-Dependent errors', $format);

See the docs for Spreadsheet::WriteExcel

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