473,698 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Spreadsheet::Wr iteExcel Question

57 New Member
I am using Win32. I have created an excel spreadsheet, formatted the columns and rows, and would like to write to the cells…all of this using Spreadsheet::Wr iteExcel. My issue is, my script is very vast with a lot of subroutines, but I need the excel spreadsheet created in the main subroutine becasue this is where the data is that i want to capture. So if I create and format the spreadsheet within the main subroutine, and as it loops through, a new spreadsheet is being created every time, so that when I move to the next row, it writes to it fine, but essentially it is writing over the previous row making it blank. If I create and format the spreadsheet outside of the main subroutine, then when I get to the part where it writes to the excel spreadsheet, it gives me an error that says, “Can’t call method ‘write’ on an undefined value…” I even tried to put the spreadsheet lines in its own subroutine and have the main subrouting call it, but i still run into the same problem when trying to write to it.

My question is how do I initially create and format the spreadsheet at the beginning and then as the subroutine loops, write to the cells, incrementing the rows in my loop? Any insight would be greatly appreciated.

Here is an example of the script format with the excel creation inside the main subroutine. Basically, the way it's set up, the main subroutine will continue looping until it goes through all of the declared %testcase_corre lation variables:

[HTML]#!/usr/bin/perl

use Spreadsheet::Wr iteExcel;
$report_file_pa th_name = "C:\\results.tx t";

my %testcase_corre lation1 = (
name => "Name1",
test_case => "NM204",
Code No. => "Z-204.5 CHEM",
);

my %testcase_corre lation2 = (
name => "Name2",
test_case => "NM366",
Code No. => "Z-366 CHEM",
);

my %testcase_corre lation3 = (
name => "Name3",
test_case => "NM876",
Code No. => "Z-876 CHEM",
);


my $row_count = 1;

my other declared variables;

execute_main_su broutine (\%testcase_cor relation1, $report_file_pa th_name, 'CHEM');
execute_main_su broutine (\%testcase_cor relation2, $report_file_pa th_name, 'CHEM');
execute_main_su broutine (\%testcase_cor relation3, $report_file_pa th_name, 'CHEM');

sub defined_subrout ine1{
.
.
}
sub defined_subrout ine2{
.
.
}

sub execute_main_su broutine{

my $params = shift;
my %testcase = %$params;
my $report_file_pa th_name = shift;
my $NBC_type = shift;

my $workbook = Spreadsheet::Wr iteExcel->new("c:\\perl. xls");

# Add worksheet
my $sheet1 = $workbook->add_worksheet( "Results Overview");

$sheet1->set_margins_LR (.3);
$sheet1->set_margin_top (.75);
$sheet1->set_margin_bot tom(.75);
$sheet1->center_horizon tally();


# Add Formats
my $format = $workbook->add_format() ;
$format->set_align('lef t');
$format->set_align('top ');
$format->set_border() ;
$format->set_text_wrap( );

my $format_fail = $workbook->add_format() ;
$format_fail->set_align('lef t');
$format_fail->set_align('top ');
$format_fail->set_color('red ');
$format_fail->set_border() ;
$format_fail->set_text_wrap( );

my $format_header = $workbook->add_format() ;
$format_header->set_align('cen ter');
$format_header->set_align('vce nter');
$format_header->set_bold();
$format_header->set_text_wrap( );
$format_header->set_border() ;

# Set the height of the rows
$sheet1->set_row(0, 60,$format_head er );


# Set the width of the columns
$sheet1->set_column('A: A', 15);
$sheet1->set_column('B: B', 25);
$sheet1->set_column('C: C', 15);
$sheet1->set_column('D: D', 41);

$sheet1->write(0, 0, "Test Case -\nMessage Group", undef,$format_h eader );
$sheet1->write(0, 1, "AEP-45\nParagraph\n Tested", undef,$format_h eader);
$sheet1->write(0, 2, "Completion\nSt atus", undef,$format_h eader);
$sheet1->write(0, 3, "Reason for Failure", undef,$format_h eader);

defined_subrout ine1();
.
.
defined_subrout ine2();
.
.
$sheet1->write($row_cou nt, 0, "$testcase{test _case}", $format);
$sheet1->write($row_cou nt, 1, "$testcase{AEP_ 45}", $format);

if (xyz == 0)
{
$sheet1->write($row_cou nt, 2, "PASS", $format);
}
else{
$sheet1->write($row_cou nt, 2, "FAIL", $format_fail);
}

$row_count++;

return 0;
} #end main subroutine[/HTML]
Feb 27 '08 #1
8 9413
nithinpes
410 Recognized Expert Contributor
If I create and format the spreadsheet outside of the main subroutine, then when I get to the part where it writes to the excel spreadsheet, it gives me an error that says, “Can’t call method ‘write’ on an undefined value…” I even tried to put the spreadsheet lines in its own subroutine and have the main subrouting call it, but i still run into the same problem when trying to write to it.

My question is how do I initially create and format the spreadsheet at the beginning and then as the subroutine loops, write to the cells, incrementing the rows in my loop? Any insight would be greatly appreciated.
This reply is to the emphasised portion of your quote.If you are creating spreadsheet outside the main subroutine, then you need to pass $workbook and $sheet1 as parameters to main subroutine. Otherwise, $workbook and $sheet1 would be undefined inside main subroutine.

Expand|Select|Wrap|Line Numbers
  1. my $workbook = Spreadsheet::WriteExcel->new("c:\\perl.xls");
  2. my $sheet1 =  $workbook->add_worksheet("Results Overview");
  3. .
  4. .
  5. .
  6. execute_main_subroutine (\%testcase_correlation1, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  7. execute_main_subroutine (\%testcase_correlation2, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  8. execute_main_subroutine (\%testcase_correlation3, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  9. .
  10. .
  11. sub execute_main_subroutine{
  12.  
  13. my $params = shift;
  14. my %testcase = %$params;
  15. my $report_file_path_name = shift;
  16. my $NBC_type = shift;
  17. my ($workbook,$sheet1) = @_;
  18.  
  19.  
  20.  
Feb 28 '08 #2
Perl Beginner
57 New Member
This reply is to the emphasised portion of your quote.If you are creating spreadsheet outside the main subroutine, then you need to pass $workbook and $sheet1 as parameters to main subroutine. Otherwise, $workbook and $sheet1 would be undefined inside main subroutine.

Expand|Select|Wrap|Line Numbers
  1. my $workbook = Spreadsheet::WriteExcel->new("c:\\perl.xls");
  2. my $sheet1 =  $workbook->add_worksheet("Results Overview");
  3. .
  4. .
  5. .
  6. execute_main_subroutine (\%testcase_correlation1, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  7. execute_main_subroutine (\%testcase_correlation2, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  8. execute_main_subroutine (\%testcase_correlation3, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  9. .
  10. .
  11. sub execute_main_subroutine{
  12.  
  13. my $params = shift;
  14. my %testcase = %$params;
  15. my $report_file_path_name = shift;
  16. my $NBC_type = shift;
  17. my ($workbook,$sheet1) = @_;
  18.  
  19.  
  20.  
Thank you for your reply Nithinpes. However, it’s still not working. When I did as you suggested and pass them as parameters, I still get the error, “Can’t call method ‘write’ on an undefined value…”.
So then, instead of putting:

Expand|Select|Wrap|Line Numbers
  1. my ($workbook,$sheet1) = @_;
I separated them and put:

Expand|Select|Wrap|Line Numbers
  1. my $workbook = @_;
  2. my $sheet1 = @_;
When I did that, then I get a different error that says, “Can’t call method ‘write’ without a package or object reference…”.

I will continue to research this avenue. i think I'm really close, but just missing something. if you have any more suggestions that would be great.
Feb 28 '08 #3
Perl Beginner
57 New Member
Well, i was right...i did miss something! i didn't notice that you put $workbook and $sheet1 in the following lines:

Expand|Select|Wrap|Line Numbers
  1. execute_main_subroutine (\%testcase_correlation1, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  2. execute_main_subroutine (\%testcase_correlation2, $report_file_path_name, 'CHEM',$workbook,$sheet1);
  3. execute_main_subroutine (\%testcase_correlation3, $report_file_path_name, 'CHEM',$workbook,$sheet1);
When i put them there, the script runs to completion without errors. but now, when i look at the excel spreadsheet, the lines are not populated. so still the 'write' is not working for some reason inside the main subroutine.

But, i'm getting that much closer!
Feb 28 '08 #4
nithinpes
410 Recognized Expert Contributor
Thank you for your reply Nithinpes. However, it’s still not working. When I did as you suggested and pass them as parameters, I still get the error, “Can’t call method ‘write’ on an undefined value…”.
So then, instead of putting:

Expand|Select|Wrap|Line Numbers
  1. my ($workbook,$sheet1) = @_;
I separated them and put:

Expand|Select|Wrap|Line Numbers
  1. my $workbook = @_;
  2. my $sheet1 = @_;
When I did that, then I get a different error that says, “Can’t call method ‘write’ without a package or object reference…”.

I will continue to research this avenue. i think I'm really close, but just missing something. if you have any more suggestions that would be great.
The way you have assigned is not right:
Expand|Select|Wrap|Line Numbers
  1. my $workbook = @_;
  2. my $sheet1 = @_;
For both variables, you are assigning entire array containing both $workbook and $sheet1 variables. When I group them together, both variables will be assigned two remaining elements from the array(after shift). If you want to assign them separately, use shift
Expand|Select|Wrap|Line Numbers
  1. my $workbook = shift; ### equivalent to my $workbook = shift @_;
  2. my $sheet1 = shift;
Feb 29 '08 #5
Perl Beginner
57 New Member
I don't know what it is, it's running to completion but it still isn't populating the excel cells. The formating part is working since it's at the beginning of the script, outside of the main subroutine. but inside the subroutine, it just won't 'write' to the cells.
Feb 29 '08 #6
Perl Beginner
57 New Member
It's working! everything you said to do was correct. but i had to move all of the excel fomatting lines inside the main subroutine. I don't know why, but once i did that it worked. the only lines outside of the subroutine were:

Expand|Select|Wrap|Line Numbers
  1. my $workbook = Spreadsheet::WriteExcel->new("c:\\perl.xls");
  2.  
  3. my $sheet1 = $workbook->add_worksheet("Results Overview");
I am using the following line instead of the shift:
Expand|Select|Wrap|Line Numbers
  1. my ($workbook,$sheet1)= @_;
The only thing now is now i'm getting an excel warning when i open the spreadsheet. it says, 'File Error: data may have been lost'. But when i look at my data, everything is there.

At any rate, thank you for your knowledge!
Feb 29 '08 #7
nithinpes
410 Recognized Expert Contributor
It's working! everything you said to do was correct. but i had to move all of the excel fomatting lines inside the main subroutine. I don't know why, but once i did that it worked. the only lines outside of the subroutine were:
Offcourse, that is what I had done in my initial reply. There were only two lines outside subroutine, one for $workbook and other for $sheet1. When you did formatting outside the subroutine, it did not work because inside subroutine you are using $format and $format_header variables while writing to excel sheet which are undefined. If you were to do so, then you need to pass $format and $format_header also as parameters to subroutine adding complexity to calling subroutine.
Well, I'm not sure why you got that error while opening excel sheet!
Mar 2 '08 #8
Perl Beginner
57 New Member
I've been researching the 'File Error: data may have been lost', and it looks as though this is a known issue with Spreadsheet::Wr iteExcel that happens on certain versions of Microsoft Excel. From what i read it's on the excel end not the Perl end. Don't know what the fix is yet.
Mar 3 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2912
by: Dorthe Luebbert | last post by:
Hi, there is a new(er) version of John McNammara's Excel package for Perl: http://homepage.eircom.net/~jmcnamara/perl/prerel/Spreadsheet-WriteExcel-0.49.7.tar.gz The package writes Excel97 instead of Excel5, it has e.g. no longer the 255 char's limit for one cell etc. See:
2
7373
by: Fabio | last post by:
Hello, there's a module called Spreadsheet::WriteExcel. It allows me to create a new worksheet and edit its cells: my $workbook = Spreadsheet::WriteExcel->new('my.xls'); my $worksheet = $workbook->addworksheet(); $worksheet -> write('A2', 222); But how can I put some values into the cells of an existing worksheet?
4
14692
by: tito | last post by:
I am using Active Perl for running perl programs.I am finding difficulty in installing Spreadsheet::WriteExcel,Spreadsheet:ParseExcel to the machine for writing a program which will retrieve data from an Excel Sheet. Can anyone explain me the steps to install Spreadsheet::WriteExcel,Spreadsheet:ParseExcel in Windows machine.
1
13683
by: tito | last post by:
I would like to write a program which will retrieve the data from the EXCEL file and write into an SQL Server using Perl script.Spreadsheet::WriteExcel programs are working fine.But while executing Spreadsheet::ParseExcel i am getting the error like Can't locate Spreadsheet/ParseExcel.pm in @INC (@INC contains: D:/Perl/lib D:/Pe rl/site/lib .) at parse.pl line 5. I used the following program.Can anyone tell me whether the problem arise...
5
11060
by: rasmitasah25 | last post by:
hi, I am very new to perl.I have written a perl script which is writing data into an excel file.The problem is that it is creating one new excel file while executing but my need id to write into an existing .xls file. currently I am using: my $workbook = Spreadsheet::WriteExcel::Big->new("file.xls"); for that which method Should I use?? can anyone please help me... Thanks
5
2829
by: bvithya | last post by:
Hi Gurus, I am in need of your guidance. I have installed Perl5.8.8 on my server running with SunOS 5.8 and then I installed Spreadsheet::WriteExcel perl module. It has been installed successfully. Howver when I try to run a command to check whether it emails an excel sheet or not, it showed me the below. # /home/oracle8/oradba/admin/utils/sqlplus2xls/sqlplus2xls.pl ERROR (Missing Parameter): <config_file> not specified!
2
3196
by: lavsaxena | last post by:
Can we edit a worksheet of excel through spreadsheet::writeExcel. Because when I am using my $workbook = Spreadsheet::WriteExcel->new('D:\test_result.xls'); this statement is creating the excel file again. I want to update an excel sheet means if the some data already exist, the new data should be append to it... Thanks in Advance -Lav
7
3773
by: somsub | last post by:
Hi, When I tried to open excel sheet created by below code with MSexcel it showing "Unable to read file " error . use Spreadsheet::WriteExcel; $workbook = Spreadsheet::WriteExcel->new("sm2.xls"); I am using winxp OS and MS excel 2003 ....
1
1802
by: Irakli Lekishvi | last post by:
Hello guys I have 1675 line Code. Want to when user click button textbox.text insert into this xml spreadsheet and then create on desktop. does it possible? how can i insert text in current xml spreadsheet row? For Example : <Cell ss:StyleID="s38"/> <Cell ss:StyleID="s39"> <Data ss:Type="String">ვაშლიჯვარი</Data> </Cell> <Cell ss:StyleID="s36"/> Please help me, advice something its very important...
0
8601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9021
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7716
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.