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