By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,326 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

how to output excel to textfile?

100+
P: 170
hi guys,
i've got the code below that extract cell value from an excel sheet
but any idea how i continue to output it to a text file?
i'm not too sure how to do it, thanks

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Win32::OLE qw(in with);
  3. use Win32::OLE::Const 'Microsoft Excel';
  4. $Win32::OLE::Warn = 3;                                # die on errors...
  5. my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
  6.     || Win32::OLE->new('Excel.Application', 'Quit');  # get already active Excel
  7.                                                       # application or open new
  8. my $Book = $Excel->Workbooks->Open("C:\\DOCUMENTS\\test.xls"); # open Excel file
  9. my $Sheet = $Book->Worksheets(1);                     # select worksheet number 1
  10. $last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
  11. my $array = $Sheet->Range("A4:B$last_row")->{'Value'};        # get the contents
  12. $Book->Close;
  13.  
  14.  
Mar 13 '08 #1
Share this Question
Share on Google+
16 Replies


P: 23
It has returned two dimensional array i.e an array with references to arrays.

so the value to be get from the array reference
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Win32::OLE qw(in with);
  3. use Win32::OLE::Const 'Microsoft Excel';
  4. $Win32::OLE::Warn = 3;                                # die on errors...
  5. my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); 
  6.  # get already active Excel
  7. my $Book = $Excel->Workbooks->Open("E:\\perl-scripts\\thescripts\\test.xls"); # open Excel file
  8. my $Sheet = $Book->Worksheets(1);                     # select worksheet number 1
  9. my $last_row = $Sheet->UsedRange->Find({What => "*", SearchDirection=>xlPrevious, SearchOrder=>xlByRows})->{Row};
  10. my $array = $Sheet->Range("A4:B$last_row")->{'Value'};        # get the contents
  11. $Book->Close;
  12.  
  13. foreach my $cell_ref (@$array) {
  14.   foreach my $value (@$cell_ref) {
  15.     print "$value\t";
  16.   }
  17.   print "\n";
  18. }
  19.  
-Manimaran
Mar 13 '08 #2

100+
P: 170
hm..i think there's a problem with the code
u can pass it into an array and print out the values,
but it's not printing to a text file
any suggestion how i can print the values to a text file?
Mar 14 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
hm..i think there's a problem with the code
u can pass it into an array and print out the values,
but it's not printing to a text file
any suggestion how i can print the values to a text file?
If you want to output to a file, then you have to first open a file and get a file handle assigned, then use the file handle to point the print statement to the file for output.

Here is an example:

Expand|Select|Wrap|Line Numbers
  1. # open a file for writing to and assign a file handle
  2. open(FILE, ">file.txt");
  3.  
  4.  
  5. # Print out to the newly opened file
  6. print FILE ("Some text\n");
  7.  
Regards,

Jeff
Mar 14 '08 #4

100+
P: 170
hey guys thanks for the help got it printed out already but i need to do a few more things, tried a lot ways still can't get it

my excel file for example has data in its respective cell like:

1 4
2 5
3 6

when i print it to a text file it becomes:

1
4
2
5
3
6

any idea if i can change the print out format in text file to:

1 4
2 5
3 6

i tried putting 123 in one array and 456 in another array but could not work it out
below's my most recent code

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.     use Win32::OLE;
  5.     use Win32::OLE::Const 'Microsoft Excel';    
  6.  
  7.         # use existing instance if Excel is already running
  8.         eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
  9.         die "Excel not installed" if $@;
  10.         unless (defined $ex) {
  11.             $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
  12.                     or die "Oops, cannot start Excel";
  13.         }
  14.  
  15.     ####deducing if $save_file_name exist or not######    
  16.  
  17.  
  18.     $book = $ex->Workbooks->Open("C:\\Documents and Settings\\clong\\Desktop\\Convert to Text file\\Quarantined_IMSIHLR2_20070611_Completed1.xls");
  19.     $sheet = $book->Worksheets(1);
  20.  
  21.     $data = $sheet -> Range("A4:B2873") -> {Value};
  22.  
  23.  
  24.     open(FILE, ">file.txt");
  25.     foreach my $cell_ref (@$data) {
  26.           foreach my $value (@$cell_ref){     
  27.  
  28.     print FILE ("$value    \n");
  29. }
  30. }    
  31.  
Mar 14 '08 #5

nithinpes
Expert 100+
P: 410
Change this block of code:

Expand|Select|Wrap|Line Numbers
  1. open(FILE, ">file.txt");
  2.     foreach my $cell_ref (@$data) {
  3.           foreach my $value (@$cell_ref){     
  4.       print FILE ("$value    \n");
  5. }
  6. }    
  7.  
to:
Expand|Select|Wrap|Line Numbers
  1. open(FILE, ">file.txt");
  2.     foreach my $cell_ref (@$data) {
  3.           foreach my $value (@$cell_ref){     
  4.       print FILE ("$value \t");
  5. }
  6. print FILE "\n";
  7. }    
  8.  
Mar 14 '08 #6

100+
P: 170
thanks for the codes

ok tried soemthing too..

i wanted another variable added to the text file:

1 4 DELETED
2 5 DELETED
3 6 DELETED

i amended my code to :
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.     open(FILE, ">file.txt");
  4.     foreach my $cell_ref (@$data) {
  5.           foreach my $value (@$cell_ref){     
  6.  
  7.         print FILE ("$value \t","DELETED");
  8.  
  9.  
  10.         }
  11.  
  12.     print FILE "\n";
  13. }    
  14.  
and it became :

1 DELETED4 DELETED
2 DELETED5 DELETED
3 DELETED 6 DELETED

any idea where i went wrong?
Mar 14 '08 #7

100+
P: 170
opps
ok found it out

just replaced it as below

Expand|Select|Wrap|Line Numbers
  1.  
  2.     open(FILE, ">file.txt");
  3.     foreach my $cell_ref (@$data) {
  4.           foreach my $value (@$cell_ref){     
  5.  
  6.         print FILE ("$value");
  7.         print FILE"    \t";
  8.  
  9.  
  10.         }
  11.     print FILE ("DELETED");
  12.     print FILE "\n";
  13. }    
  14.  
  15.  
Mar 14 '08 #8

P: 12
Hate to be redundant or insignificant in my response, yet here is my take.

Based on my limited understanding and personal roadblocks.

1) Reading/Looping Through an XLS file to extract data with perl is a $bear = killer koala

2) My path of least resistance to overcome said $bear, has been to require the XLS file to be converted (via save as) to a CSV format.

O.K -- I too am almost sick at the lame a$$ed effort to be cute with $variables

couldn't resist...

knowing enoughing to be dangerous and not much more

Regards,

hutch

:-)
Mar 14 '08 #9

100+
P: 170
hi guys, i made some modification to my script above but quite puzzled again

well this time i got 2 sets of array
but i need to print them out in the same manner again like from excel it has:

1 4 DELETED
2 5 DELETED
3 6 DELETED

right the problem is the number are defined as cells from A1 to B3 as one array
and the deleted are taken from another cell say T1 to T3 in another array
so i got print out 2 arrays of info into text file in the manner:

1 4 DELETED
2 5 DELETED
3 6 DELETED

below is my code which i think is wrong when i tried to attempt to print the 2nd array out

Expand|Select|Wrap|Line Numbers
  1. use Win32::OLE;
  2.     use Win32::OLE::Const 'Microsoft Excel';    
  3.  
  4.         # use existing instance if Excel is already running
  5.         eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
  6.         die "Excel not installed" if $@;
  7.         unless (defined $ex) {
  8.             $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
  9.                     or die "Oops, cannot start Excel";
  10.         }
  11.  
  12.  
  13.     $book = $ex->Workbooks->Open("C:\\Documents and Settings\\clong\\Desktop\\Convert to Text file\\@xlsFiles");
  14.     $sheet = $book->Worksheets(1);
  15.  
  16.  
  17.     $last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
  18.     $last_row = $last_row-14;    
  19.     $total_run = $sheet -> Range("Z$last_row") -> {Value};
  20.     $last_row = $last_row-1;
  21.     $data = $sheet -> Range("A4:B$last_row") -> {Value};
  22.     $last_row = $last_row+4;
  23.     $deleted = $sheet -> Range("Z$last_row") -> {Value};
  24.     $last_row = $last_row-4;
  25.     $output = $sheet -> Range("T4:T$last_row") -> {Value};
  26.  
  27.  
  28.     open(FILE, ">file.txt");
  29.     foreach my $cell_ref (@$data) {
  30.           foreach my $value (@$cell_ref){     
  31.  
  32.         print FILE ("$value \t");
  33.  
  34.         }
  35.         foreach my $cell_ref (@$output)    {
  36.                 foreach my $value (@$cell_ref){ 
  37.                     print FILE ("$value");}
  38.                         }
  39.         print FILE ("\n");
  40.  
  41. }        
  42.         print FILE ("TOTAL RUN: $total_run    \n");
  43.         print FILE ("TOTAL DELETED: $deleted\n");
  44.         print FILE ("ROWS SELECTED: $total_run\n");
  45.         print FILE ("$last_row");
  46.  
  47.  
Mar 26 '08 #10

100+
P: 170
alright found out something...if i change the code as shown
i just need to know how to put $output into an array so it prints out but it must be the same format as i've describe as above, thanks
any suggestion?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. ##start of conversion of excel to text file
  4.  
  5.  
  6.     use Win32::OLE;
  7.     use Win32::OLE::Const 'Microsoft Excel';    
  8.  
  9.         # use existing instance if Excel is already running
  10.         eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')};
  11.         die "Excel not installed" if $@;
  12.         unless (defined $ex) {
  13.             $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;})
  14.                     or die "Oops, cannot start Excel";
  15.         }
  16.  
  17.  
  18.     $book = $ex->Workbooks->Open("C:\\Documents and Settings\\clong\\Desktop\\Convert to Text file\\@xlsFiles");
  19.     $sheet = $book->Worksheets(1);
  20.  
  21.  
  22.     $last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
  23.     $last_row = $last_row-14;    
  24.     $total_run = $sheet -> Range("Z$last_row") -> {Value};
  25.     $last_row = $last_row-1;
  26.     $data = $sheet -> Range("A4:B$last_row") -> {Value};
  27.     $output = $sheet -> Range("T4:T$last_row") -> {Value};
  28.     $last_row = $last_row+14;
  29.     $deleted = $sheet -> Range("Z$last_row") -> {Value};
  30.  
  31.  
  32.  
  33.     open(FILE, ">file.txt");
  34.  
  35.     foreach my $cell_ref (@$data) {
  36.           foreach my $value (@$cell_ref){     
  37.  
  38.  
  39.         print FILE ("$value \t");
  40.         }
  41.             print FILE ("$output");
  42.             print FILE ("\n");
  43.  
  44. }    
  45.  
  46.  
  47.  
  48.  
  49.         print FILE ("TOTAL RUN: $total_run    \n");
  50.         print FILE ("TOTAL DELETED: $deleted\n");
  51.         print FILE ("ROWS SELECTED: $total_run\n");
  52.  
Mar 26 '08 #11

100+
P: 170
hm...any one has suggestions how i can print the 2nd array out properly?
i tried using push @array, value
and i got things like

1 4 deleted deleted deleted
2 5 deleted deleted deleted
3 6 deleted deleted deleted

but i just need one deleted

1 4 deleted
2 5 deleted
3 6 deleted

i tried accomplishing this using a counter below:

[code=perl]
Mar 28 '08 #12

100+
P: 170
hm...any one has suggestions how i can print the 2nd array out properly?
i tried using push @array, value
and i got things like

1 4 deleted deleted deleted
2 5 deleted deleted deleted
3 6 deleted deleted deleted

but i just need one deleted

1 4 deleted
2 5 deleted
3 6 deleted

i tried accomplishing this using a counter below:
Expand|Select|Wrap|Line Numbers
  1.     $last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
  2.     $last_row = $last_row-14;    
  3.     $total_run = $sheet -> Range("Z$last_row") -> {Value};
  4.     $last_row = $last_row-1;
  5.     $data = $sheet -> Range("A4:B$last_row") -> {Value};
  6.     $last_row = $last_row+14;
  7.     $deleted = $sheet -> Range("Z$last_row") -> {Value};
  8.  
  9.  
  10.  
  11.     open(FILE, ">file.txt");
  12.  
  13.     foreach my $cell_ref (@$data) {
  14.           foreach my $value (@$cell_ref){     
  15.  
  16.  
  17.         print FILE ("$value \t");
  18.         }
  19.             $output = $sheet -> Range("T$row") -> {Value};
  20.             print FILE ("$output");
  21.             print FILE ("\n");
  22.             $row++;    
  23. }    
  24.  
but the algorithm is a little slow..so i'm thinking of using arrays
but how can i get the 2nd array printed out correctly
pls help..thanks
Mar 28 '08 #13

100+
P: 170
i hope u all can see this thread...seems like it's not posted under new
well take yr time
thanks for the help in advance
Mar 28 '08 #14

100+
P: 170
ok i figured out found it somewhere
basically it's to print arrays into columns

so say @array1, @array2, @array3 into column
the below code works,
but can someone explain to me the principle behind this code how it works?

Expand|Select|Wrap|Line Numbers
  1.  
  2.             while(@array1 || @array2 || @array3) {
  3.                 printf FILE ("%-10s        %-10s        %-10s\n",
  4.                     shift(@array1) || '    ',
  5.                 shift(@array2) || '    ',
  6.                 shift(@array3) || '    ',
  7.     );
  8. }    
Mar 28 '08 #15

nithinpes
Expert 100+
P: 410
ok i figured out found it somewhere
basically it's to print arrays into columns

so say @array1, @array2, @array3 into column
the below code works,
but can someone explain to me the principle behind this code how it works?

Expand|Select|Wrap|Line Numbers
  1.  
  2.             while(@array1 || @array2 || @array3) {
  3.                 printf FILE ("%-10s        %-10s        %-10s\n",
  4.                     shift(@array1) || '    ',
  5.                 shift(@array2) || '    ',
  6.                 shift(@array3) || '    ',
  7.     );
  8. }    

The logic of this is code is like this:
- The code block in while loop as long as any of the three arrays exist i.e. if the arrays are of unequal size, the loop iterates until the last element in the largest array is processed.
- The printf() statement will format the output. In this case, a ten characters' space is left between the first characters of consecutive words.
- The three parameters to be printed are the elements of the 3 arrays.
- shift() statement keeps on removing the first element from the array. If any of the arrays is emptied, the shift() fails and the space character after OR(||) will be printed.
Mar 28 '08 #16

100+
P: 170
i see..ok
thanks for the enlightenment
cheers, :)
Mar 31 '08 #17

Post your reply

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