File Coverage

blib/lib/Spreadsheet/Excel2Text.pm
Criterion Covered Total %
statement 14 16 87.5
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 21 90.4


line stmt bran cond sub pod time code
1             package Spreadsheet::Excel2Text;
2            
3 1     1   26406 use warnings;
  1         3  
  1         43  
4 1     1   8 use strict;
  1         2  
  1         62  
5            
6             our $VERSION = '0.04';
7            
8             BEGIN {
9 1     1   7 use Exporter;
  1         7  
  1         114  
10 1     1   17 our @ISA = qw( Exporter );
11 1         10 our @EXPORT = qw( );
12 1         3 our %EXPORT_TAGS = ( );
13 1         71 our @EXPORT_OK = qw( &XlsSaveToText );
14             }
15            
16             sub XlsSaveToText($$) {
17            
18             my $xlsFile = shift;
19             my $txtFile = shift;
20             my $excelTab = "";
21            
22 1     1   496 use Win32::OLE qw(in with);
  0            
  0            
23             use Win32::OLE::Const 'Microsoft Excel';
24            
25             $Win32::OLE::Warn = 3;
26            
27             my $excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
28            
29             my $book = $excel->Workbooks->Open("$xlsFile");
30             my $sheet = $book->Worksheets(1);
31            
32             $excel->ActiveCell->SpecialCells(xlLastCell)->Select;
33             my $lastLine = $excel->ActiveCell->Row;
34             my $lastRow = $excel->ActiveCell->Column;
35            
36             my $array = $sheet->Range($sheet->Cells(1,1),$sheet->Cells($lastLine,$lastRow))->{'Value'};
37            
38             $book->Close;
39            
40             open(my $fhText, ">$txtFile") or die $!;
41            
42             my $line = "";
43             my $field = "";
44             foreach my $refArray (@$array) {
45             $line = "";
46             foreach my $excelTab (@$refArray) {
47             $field = $excelTab;
48             no warnings;
49             $line .= $field."\t";
50             use warnings;
51             } # foreach
52             $line =~ s%\r?\n?%%g;
53             print $fhText $line, "\n";
54             } # foreach
55            
56             $excel->Quit();
57            
58             close($fhText) or die $!;
59            
60             $txtFile;
61            
62             } # XlsSaveToText
63            
64             1;
65             __END__