|
1 # Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 # All rights reserved. |
|
3 # This component and the accompanying materials are made available |
|
4 # under the terms of the License "Eclipse Public License v1.0" |
|
5 # which accompanies this distribution, and is available |
|
6 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 # |
|
8 # Initial Contributors: |
|
9 # Nokia Corporation - initial contribution. |
|
10 # |
|
11 # Contributors: |
|
12 # |
|
13 # Description: |
|
14 # |
|
15 # |
|
16 # Description: |
|
17 # TableFormatter/Excel.pm |
|
18 # |
|
19 |
|
20 package TableFormatter::Excel; |
|
21 |
|
22 use Utils; |
|
23 use TableFormatter; |
|
24 use vars qw/@ISA/; |
|
25 @ISA = qw(TableFormatter); |
|
26 |
|
27 use strict; |
|
28 use Win32::OLE; |
|
29 use Cwd; |
|
30 |
|
31 sub New { |
|
32 my $class = shift; |
|
33 my $iniData = shift; |
|
34 |
|
35 if ($iniData->Win32ExtensionsDisabled()) { |
|
36 print "Cannot use Excel table format with win32 extensions disabled. It relies on Win32::OLE module to communicate with Excel. If anybody really cares about this, please rewrite the module to use Spreadsheet::WriteExcel... but you'll have to write your own auto-fit engine. Meanwhile, using Text formatting instead."; |
|
37 require TableFormatter::Text; |
|
38 return TableFormatter::Text->New($iniData); |
|
39 } |
|
40 |
|
41 return bless {}, (ref $class || $class); |
|
42 } |
|
43 |
|
44 sub PrintTable { |
|
45 my $self = shift; |
|
46 my $data = shift; |
|
47 my $doHeading = shift; |
|
48 |
|
49 my $excel = new ExcelConnection; |
|
50 |
|
51 my $filename = Utils::PrependEpocRoot("\\epoc32\\relinfo\\temp-table.xls"); |
|
52 my $cwd = cwd; |
|
53 $cwd =~ m/^(\w\:)/; |
|
54 my $driveletter = $1 || ""; |
|
55 $filename = "$driveletter$filename"; |
|
56 unlink ($filename); |
|
57 |
|
58 my $wb = $excel->Workbooks->Add; |
|
59 |
|
60 my $ws = $wb->Worksheets(1); |
|
61 |
|
62 if ($doHeading) { |
|
63 my $style = $wb->Styles->Add("Headings"); |
|
64 $style->{Font}->{Bold} = 1; |
|
65 $ws->Rows(1)->{Style} = "Headings"; |
|
66 } |
|
67 |
|
68 for (my $row=0; $row<@$data; $row++) { |
|
69 my $rowdata = $data->[$row]; |
|
70 for (my $col=0; $col<@$rowdata; $col++) { |
|
71 my $cell = $ws->Cells($row+1, $col+1); |
|
72 my $value = $rowdata->[$col]; |
|
73 next if ($value eq ""); # otherwise Excel seems to think it's 0 |
|
74 $cell->{Value} = "'$value"; |
|
75 } |
|
76 } |
|
77 |
|
78 $self->DoFinalFormatting($ws); |
|
79 # We want to save, because otherwise Excel will prompt whether you |
|
80 # want to save when you close the workbook. But on the other hand |
|
81 # it prints horrible error messages if there is already a workbook |
|
82 # open with the same name. |
|
83 # So we need to implement a scheme to give each output workbook |
|
84 # a unique name, which means some sort of cleanup mechanism. TODO! |
|
85 #eval { |
|
86 #$wb->SaveAs($filename); |
|
87 #}; # ignore errors, we don't really care. |
|
88 $excel->{Visible} = (1); |
|
89 } |
|
90 |
|
91 ### Private |
|
92 |
|
93 sub FormatHeadingCell { |
|
94 my $self = shift; |
|
95 my $cell = shift; |
|
96 $cell->{Style}->{Font}->{Bold} = 1; |
|
97 } |
|
98 |
|
99 sub DoFinalFormatting { |
|
100 my $self = shift; |
|
101 my $ws = shift; |
|
102 $ws->Columns("A:J")->AutoFit(); |
|
103 } |
|
104 |
|
105 package ExcelConnection; |
|
106 |
|
107 sub new { |
|
108 my $excel; |
|
109 eval {$excel = Win32::OLE->GetActiveObject('Excel.Application')}; |
|
110 die "Excel not installed" if $@; |
|
111 unless (defined $excel) { |
|
112 $excel = Win32::OLE->new('Excel.Application', sub {}) or die "Oops, cannot start Excel"; |
|
113 } |
|
114 return $excel; |
|
115 } |
|
116 |
|
117 1; |
|
118 |
|
119 __END__ |
|
120 |
|
121 =head1 NAME |
|
122 |
|
123 TableFormatter/Excel.pm - Formats tables in Excel |
|
124 |
|
125 =head1 INTERFACE |
|
126 |
|
127 =head2 New |
|
128 |
|
129 Creates a formatter. |
|
130 |
|
131 =head2 PrintTable |
|
132 |
|
133 Prints the table. Two arguments: firstly, a 2D array of the data. Secondly, a Boolean specifying whether the first row is a header row. |
|
134 |
|
135 =head1 KNOWN BUGS |
|
136 |
|
137 None. |
|
138 |
|
139 =head1 COPYRIGHT |
|
140 |
|
141 Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
142 All rights reserved. |
|
143 This component and the accompanying materials are made available |
|
144 under the terms of the License "Eclipse Public License v1.0" |
|
145 which accompanies this distribution, and is available |
|
146 at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
147 |
|
148 Initial Contributors: |
|
149 Nokia Corporation - initial contribution. |
|
150 |
|
151 Contributors: |
|
152 |
|
153 Description: |
|
154 |
|
155 |
|
156 =cut |
|
157 |