Overwriting the existing Worksheet of a Excel File in Perl - linux

With the reference to the question I had asked before is:
My Code is as below:
if ( !-e $xls_file ) #Checking whether to create a new Excel file or not
{
#if yes create a new excel file and 4 worksheets
print OUTFILE "Creatin a new sheet.\n";
$excelFileHandle= Spreadsheet::WriteExcel->new($xls_file) ;
&InitFormatting ($excelFileHandle);#For format of each cell.
($handle, $row, $col) = &AddSheet("$monthYearStr", "$SHEET_TITLE", #ReportColLabels);
($handle1, $row1, $col1) = &AddSheet("$STATUS_CODE_SHEET_NAME", "$STATUS_CODE_SHEET_TITLE", #Status_Code_labels);
($handle2, $row2, $col2) = &AddSheet("$SUCCESS_COUNT_SHEET_NAME", "$SUCCESS_SHEET_TITLE", #Success_count_labels);
($handle3, $row3, $col3) = &AddSheet("$FAILURE_COUNT_SHEET_NAME", "$FAILURE_SHEET_TITLE", #Failure_count_labels);
$new = 1;
}
else #File exists needs to parsed and append the data.
{
print OUTFILE "Opening the existing sheet.\n";
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($xls_file);
if ( !defined $workbook )
{
die $parser->error(), ".\n";
}
my $worksheet = $workbook->worksheet($monthYearStr);
( $row_min, $row_max ) = $worksheet->row_range();
( $col_min, $col_max ) = $worksheet->col_range();
my $worksheet1 = $workbook->worksheet($monthYearStr);
( $row_min1, $row_max1 ) = $worksheet1->row_range();
( $col_min1, $col_max1 ) = $worksheet1->col_range();
$new = 0;
}
if ( $new = 1 )
{
$handle->write($row, $col++, "$present_date", $num_field_format);
------Contiuned for remainin row and col for $monthYearStr $STATUS_CODE_SHEET_NAME $SUCCESS_COUNT_SHEET_NAME $FAILURE_COUNT_SHEET_NAME-------
}
elsif ( $new == 0)
{
###Appending the data####
$parser = Spreadsheet::ParseExcel::SaveParser->new();
$template = $parser->Parse($xls_file);
if ( ! defined $template )
{
die $parser->error(), ".\n";
}
my $worksheet = $template->worksheet($monthYearStr);
my $row = $row_max + 1;
my $cell = $worksheet->get_cell( $row_max, $row_max );
my $format_number = $cell->{FormatNo};
$worksheet->AddCell($row, $col++, "$present_date", $format_number);
---------Continued for remaining row and col only for $monthYearStr $STATUS_CODE_SHEET_NAME -----------------------------
}
Now, how do I over-write the worksheet sheet success_count and failure_count each time when the excel file exists.
Please suggest me how to do this.

Its difficult to tell what to do without the AddSheet subroutine, but you should be able to overwrite the cells in a worksheet using something like this, per the POD:
foreach $worksheet ($workbook->sheets()) {
if( $worksheet->get_name() eq $SUCCESS_COUNT_SHEET_NAME ) {
$worksheet->write('A1', $success_count);
}
elsif( $worksheet->get_name() eq $FAILURE_COUNT_SHEET_NAME ) {
$worksheet->write('A1', $failure_count);
}
}

Related

Converting XLSM file to CSV format in Perl using "$csv->print" function?

Does function CSV->PRINT only accept array reference? I am pushing $cell2_value in an array and then printing the array (i.e rows), it would be nice if I can directly write $cell2_value into an opened CSV file.
Things to take care of -
Excel cell having commas in their value would print in double-quotes.
Excel cells having "keyword" in their value would print the whole-cell value in double-quotes and inner quotes will change to ""keyword"".
 
I can write a CSV file with a few undesired outputs of excel cells. its inserting double quotes whenever it sees special characters like / or *.
CSV FILE from the below code:-
"CLASS_A,,x,Singapore,,0xABCF00C4,"/* x2-4Rw */",-,,,,,,,Yes,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"CHECK- ""CLASS_B""- WORKED",1,2,3"
"CLASS_A,,,malyaisa,," 3:0","/* ABCVF */",E,,,,,,,Yes,,,,,,,,,,,,,Yes,,,,,,Yes,Yes,Yes,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"
Desired output
CLASS_A,,x,malyaisa,,0xABCF00C4,/* x2-4Rw */,-,,,,,,,Yes,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,"CHECK- ""CLASS_B""- WORKED",1,2,3
CLASS_A,,,malyaisa,, 3:0,/* ABCVF */,E,,,,,,,Yes,,,,,,,,,,,,,Yes,,,,,,Yes,Yes,Yes,,,,,,,,,,,,,,,,,,,
Why it's inserting quotes around each line? Is there any way to remove it?
sub Excel_to_CSV
{
($student_excel_file) = #_;
if($student_excel_file ne "")
{
$student_excel_out_csv_file = $student_excel_file;
$student_excel_out_csv_file =~ s/.xlsm$/_new.csv/;
my $parser_1 = Spreadsheet::ParseXLSX->new();
my $workbook = $parser_1->parse($student_excel_file);
my $csv_1 = Text::CSV->new ({ binary => 1, auto_diag => 1, sep_char => ',' });
open my $fh, ">:encoding(utf-8)", $student_excel_out_csv_file or die "failed to create $student_excel_out_csv_file: $!";
if ( !defined $workbook )
{
die $parser_1->error(), ".\n";
}
my $worksheet=$workbook_->worksheet(0);
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
printf("Copyig Sheet: %s from the provided student \n", $worksheet->{Name});
my $concurentEmptyLineCount = 0;
for my $row_1 ( $row_min .. $row_max )
{
my #row_elements_array;
for my $col_1 ( $col_min .. $col_max )
{
my $cell_1 = $worksheet->get_cell( $row_1, 0 );
next unless $cell_1;
$concurentEmptyLineCount=0;
my $cell_2 = $worksheet->get_cell( $row_1, $col_1);
my $cell2_value =$cell_2 -> {Val};
if(defined $cell2_value)
{
push(#row_elements_array, $cell2_value);
}
else
{
my $blank="";
push(#row_elements_array, $blank);
}
}
my $next_line="\n";
push(#row_elements_array, $next_line);
my #temp_row_elements_array= #row_elements_array;
$csv_1->print($fh, \#temp_row_elements_array);
}
close $fh;
}
return $student_excel_out_csv_file;
}
Based on my understanding of your requirements, one solution is to remove the extraneous double-quotes from $cell2_value before pushing it into #row_elements_array. For example:
$cell2_value =~ s/"(")*/$1/g;
push(#row_elements_array, $cell2_value);

Getting the background color of a cell in an existing Excel spreadsheet

I am trying to read an existing Excel spreadsheet and write it into a new Excel document with all the same format options.
I am able to read the cell contents and write them into the new Excel sheet, but I cannot get the background color of the cell.
If I use the $cell->get_format->{Fill}[2] method it gives me numbers like either 64 or 65 if the cell has a background color.
How can I get the actual color of a cell and apply the same background color to the cell in new Excel sheet?
I am doing all these things as there is no method available in the Spreadsheet::ParseExcel module to set the background color for a cell after appending data to an existing Excel sheet.
Here is my code
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook_parse = $parser->Parse( 'Report.xls' );
my $worksheet_parse = $workbook_parse->Worksheet( "Health_Report" );
my ( $col_min, $col_max ) = $worksheet_parse->col_range();
my ( $row_min, $row_max ) = $worksheet_parse->row_range();
my $workbook = Spreadsheet::WriteExcel->new( "Report_new.xls" );
my $worksheet = $workbook->addworksheet( "Health_Report" );
my $bkgd_color = $workbook->addformat();
for my $col ( $col_min .. $col_max ) {
for my $row ( $row_min .. $row_max ) {
# Return the cell object at $row and $col
my $cell = $worksheet_parse->get_cell( $row, $col );
next unless $cell;
my $value = $cell->value();
my $format = $cell->get_format();
my $backgroundcolor = $format->{Fill}->[2];
print "Row, Col = ($row, $col) ";
print "Value = $value\n";
print "Format = $backgroundcolor\n";
$bkgd_color->set_bg_color( $backgroundcolor );
### Here trying to rewrite into Excel and apply the
### same background color which the cell had previously
$worksheet->write( $row, $col, $value, $bkgd_color );
}
}
Part of my output from print statements:
Format = 65
Row, Col = (25, 4) Value = -115966
Format = 65
Row, Col = (10, 5) Value = 20170417
Format = 65
Row, Col = (11, 5) Value = 0
Format = 64
Row, Col = (16, 5) Value = 0
Format = 64
Update
It appears that the documentation is wrong. The name of the workbook object method is different from that of the class method, and you need to call
$workbook->color_idx_to_rgb($color_index)
instead
The documentation for Spreadsheet::ParseExcel says this
$font->{Color}
Returns the color index for the font. The mapping to an RGB color is defined by each workbook.
The index can be converted to a RGB string using the $workbook->ColorIdxToRGB() Parser method.
(Older versions of Spreadsheet::ParseExcel provided the ColorIdxToRGB class method, which is deprecated.)
I expect that the colour indexes in the $format->{Fill} array can be translated similarly
Hello Venkatesh k,
Try the set_color() method from (Spreadsheet::WriteExcel).
From the documentation:
Default state: Excels default color, usually black
Default action: Set the default color
Valid args: Integers from 8..63 or the following strings:
'black'
'blue'
'brown'
'cyan'
'gray'
'green'
'lime'
'magenta'
'navy'
'orange'
'pink'
'purple'
'red'
'silver'
'white'
'yellow'
Update: try this, it seems to be working for my example spread sheet.
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
sub create_new_worksheet {
my ($row, $col, $value, $backgroundcolor) = #_;
# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new('perl.xls');
# Add a worksheet
my $worksheet = $workbook->add_worksheet();
# Add and define a format
my $format = $workbook->add_format(bg_color => $backgroundcolor);
$format->set_align('center');
# Write a formatted and unformatted string, row and column notation.
$worksheet->write($row, $col, $value, $format);
}
my $parser = Spreadsheet::ParseExcel->new();
my $workbook_parse = $parser->Parse( 'Report.xls' );
my $worksheet_parse = $workbook_parse->Worksheet("Sheet1");
my ( $col_min, $col_max ) = $worksheet_parse->col_range();
my ( $row_min, $row_max ) = $worksheet_parse->row_range();
for my $col ( $col_min .. $col_max ) {
for my $row ( $row_min .. $row_max ) {
# Return the cell object at $row and $col
my $cell = $worksheet_parse->get_cell( $row, $col );
next unless $cell;
my $value = $cell->value();
my $format = $cell->get_format();
my $backgroundcolor = $format->{Fill}->[2];
print "Row, Col = ($row, $col) ";
print "Value = $value\n";
print "Format = $backgroundcolor\n";
create_new_worksheet($row, $col, $value, $backgroundcolor);
}
}

add chart to an existing excel using perl

I am new to perl.
i have an excel sheet with lot of data.. I need to update it and create a graph based on the data..using perl.
i am succeded in updating an existing excel..
now adding chart to it is not happening
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
use Spreadsheet::WriteExcel;
# Open an existing file with SaveParser
my $parser = Spreadsheet::ParseExcel::SaveParser->new();
my $template = $parser->Parse('MyExcel.xls');
my $worksheet = $template->worksheet('Firstsheet');
my $chart = $template->add_chart( type => 'line' );
$chart->add_series(
categories => '=URV!$A$17:$A$442',
values => '=URV!$D$17:$D$442',
name => 'pended graph',
);
This is not working.
Can't call method "add_chart" on an undefined value at charts4.ps line 20
Please help me with a sample working code..
Want to know whats the problem here.
add_chart() is one of the WORKBOOK METHODS. Try code like this:
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new('perl.xls');
$worksheet = $workbook->add_worksheet();
$worksheet->write('A1', 'Hi Chart!');
my $chart = $workbook->add_chart( type => 'line', embedded => 1, name => 'pended graph' );
# Insert the chart into the a worksheet.
$worksheet->insert_chart( 'E2', $chart );
Update
The problem is that excel is very hard to update with perl.
An Excel file is a binary file within a binary file. It contains
several interlinked checksums and changing even one byte can cause it
to become corrupted.
As such you cannot simply append or update an Excel file. The only way
to achieve this is to read the entire file into memory, make the
required changes or additions and then write the file out again.
Spreadsheet::ParseExcel will read in existing excel files:
my $parser = Spreadsheet::ParseExcel->new();
# $workbook is a Spreadsheet::ParseExcel::Workbook object
my $workbook = $parser->Parse('blablabla.xls');
What you really want is Spreadsheet::ParseExcel::SaveParser, which is a combination of Spreadsheet::ParseExcel and Spreadsheet::WriteExcel.
Here is an example.
Summing it up, I would suggest you to read the excel data in and then try either of the following:
Create another xls file and use the Spreadsheet::WriteExcel::Chart
library.
Create a xlsx file and use the Excel::Writer::XLSX::Chart library.
Another close option would be to read the excel in with
Spreadsheet::ParseExcel::SaveParser and then add the chart and save
it, but with this module all original charts are lost.
If you are on a Windows machine you may try to use Win32::OLE.
Here is the example from Win32::OLE's own documentation:
use Win32::OLE;
# use existing instance if Excel is already running
eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
die "Excel not installed" if $#;
unless (defined $ex) {
$ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
or die "Oops, cannot start Excel";
}
# get a new workbook
$book = $ex->Workbooks->Add;
# write to a particular cell
$sheet = $book->Worksheets(1);
$sheet->Cells(1,1)->{Value} = "foo";
# write a 2 rows by 3 columns range
$sheet->Range("A8:C9")->{Value} = [[ undef, 'Xyzzy', 'Plugh' ],
[ 42, 'Perl', 3.1415 ]];
# print "XyzzyPerl"
$array = $sheet->Range("A8:C9")->{Value};
for (#$array) {
for (#$_) {
print defined($_) ? "$_|" : "<undef>|";
}
print "\n";
}
# save and exit
$book->SaveAs( 'test.xls' );
undef $book;
undef $ex;
UPDATE#2
Here is an example code:
use strict;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new( 'chart_column.xls' );
my $worksheet = $workbook->add_worksheet();
my $bold = $workbook->add_format( bold => 1 );
# Add the worksheet data that the charts will refer to.
my $headings = [ 'Category', 'Values 1', 'Values 2' ];
my $data = [
[ 2, 3, 4, 5, 6, 7 ],
[ 1, 4, 5, 2, 1, 5 ],
[ 3, 6, 7, 5, 4, 3 ],
];
$worksheet->write( 'A1', $headings, $bold );
$worksheet->write( 'A2', $data );
###############################################################################
#
# Example 1. A minimal chart.
#
my $chart1 = $workbook->add_chart( type => 'column', embedded => 1 );
# Add values only. Use the default categories.
$chart1->add_series( values => '=Sheet1!$B$2:$B$7' );
# Insert the chart into the main worksheet.
$worksheet->insert_chart( 'E2', $chart1 );
###############################################################################
#
# Example 2. One more chart
#
my $chart2 = $workbook->add_chart( type => 'column', embedded => 1 );
# Configure the chart. # change the categories if required change the values as required
$chart2->add_series(
categories => '=Sheet1!$A$4:$A$7',
values => '=Sheet1!$B$4:$B$7',
);
$worksheet->insert_chart( 'N1', $chart2, 3, 3 );
Also,
If you don't mind xlsx over xls, you may use Excel::Writer::XLSX. It is more actively maintained.
The trick to be able to parse and use at the same time the functions inside the WriteExcel module is to use the the use Spreadsheet::ParseExcel::SaveParser; module.
Below i have an example. The example will not use the chart functions but the problem you have is not on how to use the chart functions of WriteExcel module but on how to parse an existing excel file and then use that parsed information with the WriteExcel modul (which is originally thought only for NEW excel files).
if ( ( -f $excel_file_name ) && ( ( stat $excel_file_name )[7] > 0 ) ) {
#PARSE EXCEL
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::SaveParser;
# Open the template with SaveParser
my $parser = new Spreadsheet::ParseExcel::SaveParser;
my $template = $parser->Parse("$excel_file_name");
my $sheet = 0;
my $row = 0;
my $col = 0;
if ( !defined $template ) {
die $parser->error(), " Perlline:", __LINE__, " \n "; #probably the file is already open by your GUI
}
# Get the format from specific cell
my $format = $template->{Worksheet}[$sheet]->{Cells}[$row][$col]->{FormatNo};
# Add a new worksheet
#for my $worksheet ( $template->worksheets() ) {
my $worksheet_parser = $template->worksheet("$metrict_data_worksheet_name");
my ( $row_min, $row_max ) = $worksheet_parser->row_range();
my ( $col_min, $col_max ) = $worksheet_parser->col_range();
my #row_array_value;
for my $row ( 1 .. $row_max ) { #avoid header start from 1
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet_parser->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";
push( #row_array_value, $cell->value() );
} #end header column loops for one regression
} #end row loop all lines
#}
# The SaveParser SaveAs() method returns a reference to a
# Spreadsheet::WriteExcel object. If you wish you can then
# use this to access any of the methods that aren't
# available from the SaveParser object. If you don't need
# to do this just use SaveAs().
#
my $workbook;
{
# SaveAs generates a lot of harmless warnings about unset
# Worksheet properties. You can ignore them if you wish.
local $^W = 0;
# Rewrite the file or save as a new file
my $check_if_possible2write = Spreadsheet::WriteExcel->new($excel_file_name);
if ( defined $check_if_possible2write ) { #if not possible it will be undef
$workbook = $template->SaveAs("$excel_file_name");#IMPORTANT this is of type WriteExcel and not ParseExcel
}
else {
print "Not possible to write the Excel file :$excel_file_name, another user may have the file open. Aborting... ", __LINE__, " \n ";
exit;
}
}
#####################FROM HERE YOU CAN USE AGAIN use Spreadsheet::WriteExcel; ####################
use Spreadsheet::WriteExcel;
my $worksheet = $workbook->sheets("$metrict_data_worksheet_name");
my $column_header_count = 0;
foreach my $name ( sort { lc $a cmp lc $b } keys %merged_all_metrics ) {
$worksheet->write( $row_max + 1, $column_header_count, "$merged_all_metrics{$name}" ); #row,col start
$column_header_count++;
}
$worksheet->set_column( 'A:L', 50, undef, 0, 1, 0 ); #grouping #comp_src group
$worksheet->set_column( 'N:R', 50, undef, 0, 1, 0 ); #grouping
$workbook->close() or die "Error closing file: $!"; #CLOSE
}
The important part of the code is what happens after the comment line:
#####################FROM HERE YOU CAN USE AGAIN use Spreadsheet::WriteExcel; ####################
After that point you will see that you have a $workbook handler. This variable has all the information parsed and more important is that it is from type WriteExcel Object so you will have all the methods of this module available.
Important Notice. The parser is not able to parse charts and formulas (only values), therefore you will have to write then again on each parse->write loop.

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

Error when using Spreadsheet::ParseExcel

I get 2 errors when compiling the following code:
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::ParseExcel;
my $xlsparser = Spreadsheet::ParseExcel->new();
my $xlsbook = $parser->parse('xsl_test.xls');
my $xls = $xls->worksheet(0);
my ( $row_first, $row_last ) = $xls->row_range();
my ( $col_first, $col_last ) = $xls->col_range();
my $csv = '';
for my $row ( $row_first .. $row_last ) { #Step through each row
for my $col ( $col_first .. $col_last ) { #Step through each column
my $cell = $xls->get_cell( $row, $col ); #Get the current cell
next unless $cell;
$csv .= $cell->unformatted(); #Get the cell's raw data -- no border colors or anything like that
if ( $col == $col_last ) {
$csv .= "\n"; #Make a new line at the end of the row
} else {
$csv .= ",";
}
}
}
Errors:
global symbol "$parser" requires explicit package name at line 8
global symbol "$xls" requires explicit package name at line 9
I get the above code from http://www.ehow.com/how_7352636_convert-xls-csv-perl.html, and installed the excel module using: cpan Spreadsheet::ParseExcel Spreadsheet::XLSX Spreadsheet::Read
What's causing the error?
Those errors mean that you are using strict, but you didn't declare some variables with my. For example, you declared $xmlprser, but then you tried to use $parser, which was not declared. The code you copied has errors.
A better place to get code is the source itself: Spreadsheet::ParseExcel
Try:
my $parser = Spreadsheet::ParseExcel->new();
my $xlsbook = $parser->parse('xsl_test.xls');
my $xls = $xlsbook->worksheet(0);

Resources