ravi45722 has asked for the wisdom of the Perl Monks concerning the following question:

I want to edit a excel file without losing previous data

For that I try a code

#!usr/bin/perl #use warnings; #use strict; use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; use Spreadsheet::XLSX; use Spreadsheet::Read; use DBI; #use POSIX qw(strftime); my $dbh = DBI->connect("DBI:mysql:database=MIS_Reports;host=172.16.15. +104;mysql_socket=/opt/lampstack-5.5.27-0/mysql/tmp/mysql.sock","root" +,"", {'RaiseError' => 1}); #Selecting the data to fetch my $sth = $dbh->prepare("SELECT * from MIS_P2P"); $sth->execute() or die $DBI::errstr; my $file_path= "/root/prac/packages/B_MUM_dashboard_month_latency_corr +ected_FDB.XLS"; my $workbook = ReadData($file_path,cells => 0 ); if(defined $workbook->[0]{'error'}) { print "Error occurred while processing $file_path:".$workbook->[0] +{'error'}."\n"; exit(-1); } my $worksheet = $workbook->[1]; my $max_rows = $worksheet->{'maxrow'}; my $max_cols = $worksheet->{'maxcol'}; print "Rows : $max_rows\n"; print "Colomns : $max_cols\n"; while (my @row = $sth->fetchrow_array()) { my $excel_col=0; foreach my $value (@row) { $worksheet->write($max_rows+1,$excel_col,$value); $excel_col++; } }

But I am getting an error

Error: Can't call method "write" on unblessed reference at excel_write.pl line 48.

I install that Spreadsheet::WriteExcel several times but still it showing error. I am not getting where I missed it

Replies are listed 'Best First'.
Re: Open a file in append mode
by Corion (Patriarch) on Aug 11, 2015 at 12:16 UTC

    What kind of object is stored in $worksheet and what does the documentation of the relevant module say about its ->write method?

    You cannot simply mix and match modules that contain "Spreadsheet" or "XLS" in their name and hope that Perl will figure out how you want to use them.

      Then is there any module to read & write an excel sheet without losing my previous data. (append mode)
Re: Open a file in append mode
by chacham (Prior) on Aug 11, 2015 at 13:25 UTC

    SELECT * from MIS_P2P

    Side note: A column list is better than *, as it is self-commenting, has no issues if columns are added or change order, and promotes keeping focus on only the columns you actually require. * is best left to exists(), aggregate functions, and ad hoc queries.