blob: 5229d98e2c173a8d21a58681b7981b27806e7a5e [file] [log] [blame]
yu.dongc33b3072024-08-21 23:14:49 -07001#!/usr/bin/perl
2#
3# Filename:
4# ---------
5# Office.pm
6#
7# Description:
8# ------------
9# Microsoft Office access functions.
10#
11# Auther:
12# -------
13# mtk01542
14#
15# Note:
16# -----
17# Hints for Win32 Ole: for object, use 'abc->def'; for attribute, use 'abc->{def}'.
18#
19# Log:
20# -----
21# 2006/12/26 Create for Excel part.
22#
23
24package Office; # declare package library
25use strict;
26
27# for use of Excel
28use Win32::OLE qw(in with);
29use Win32::OLE::Const 'Microsoft Excel';
30$Win32::OLE::Warn = 3;
31
32#******************************************************************************
33# Global Data
34#******************************************************************************
35my $g_excel_name = "";
36my $g_excel;
37my $g_book;
38my $g_sheet;
39
40my %g_fontAttr = ('Name' => 'Arial', 'Size' => 11, 'Bold' => 0, 'ColorIndex' => 1);
41my %g_fontColorHash = ('black' => 1, 'white' => 2, 'red' => 3, 'blue' => 5);
42
43my $curr_obj;
44
45#******************************************************************************
46# Export Function
47#******************************************************************************
48
49
50#******************************************************************************
51# FUNCTION
52# excelGetColIndex
53# DESCRIPTION
54# get column index by given cell names and desired row
55# PARAMETERS
56# para 1 - reference to cell name array
57# para 2 - reference to index hash
58# para 3 - desired row number
59# RETURNS
60# none
61#******************************************************************************
62sub excelGetColIndex()
63{
64 my $indexStrArray_ref;
65 my $indexHash_ref;
66 my $row;
67
68 ($indexStrArray_ref, $indexHash_ref, $row) = @_;
69
70 foreach my $indexStr (@{$indexStrArray_ref})
71 {
72 ${$indexHash_ref}{$indexStr} = 0;
73 my $col = 1;
74 my $str = trim(excelRead($row, $col));
75 while ($str ne "")
76 {
77 if ($str =~ /^$indexStr$/i)
78 {
79 ${$indexHash_ref}{$indexStr} = $col;
80 #print "$indexStr = $col\n";
81 }
82 $col++;
83 $str = trim(excelRead($row, $col));
84 }
85 }
86}
87
88
89#******************************************************************************
90# FUNCTION
91# excelNew
92# DESCRIPTION
93# new an excel file
94# PARAMETERS
95# para 1 - filename
96# RETURNS
97# none
98#******************************************************************************
99sub excelNew()
100{
101 $g_excel_name = $_[0];
102
103 $g_excel = Win32::OLE->new('Excel.Application', 'Quit') or die "Oops, cannot start Excel";
104 #Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);
105
106 $curr_obj = $g_excel;
107
108 $g_book = $g_excel->Workbooks->Add;
109 $g_sheet = $g_book->Worksheets(1);
110}
111
112
113#******************************************************************************
114# FUNCTION
115# excelOpen
116# DESCRIPTION
117# open an excel file
118# PARAMETERS
119# para 1 - filename
120# RETURNS
121# none
122#******************************************************************************
123sub excelOpen()
124{
125 my $file = $_[0];
126
127 $g_excel = Win32::OLE->new('Excel.Application', 'Quit') or die "Oops, cannot start Excel";
128 #Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);
129
130 $curr_obj = $g_excel;
131
132 die "\n[ERROR] $file doesn't exist!\n" if (!(-e $file));
133 $g_book = $g_excel->Workbooks->Open("$file");
134 $g_sheet = $g_book->Worksheets(1);
135}
136
137
138#******************************************************************************
139# FUNCTION
140# excelSetWorksheet
141# DESCRIPTION
142# set worksheet of excel file
143# PARAMETERS
144# para 1 - worksheet name
145# RETURNS
146# none
147#******************************************************************************
148sub excelSetWorksheet()
149{
150 my $worksheet;
151
152 ($worksheet) = @_;
153
154 $g_sheet = $g_book->Worksheets($worksheet);
155}
156
157
158#******************************************************************************
159# FUNCTION
160# excelAddWorksheet
161# DESCRIPTION
162# add worksheet to excel file
163# PARAMETERS
164# para 1 - worksheet name
165# para 2 - how to add? to right/left or rightest
166# para 3 - name of reference worksheet
167# RETURNS
168# none
169#******************************************************************************
170sub excelAddWorksheet()
171{
172 my $name;
173 my $cmd;
174 my $arg;
175
176 ($name, $cmd, $arg) = @_;
177
178 $arg = $g_book->ActiveSheet->{Name} if ($arg eq ''); # set to current if empty
179
180 if ($cmd eq 'rightest')
181 {
182 # add worksheet as rightest one
183 $g_book->Worksheets->add(
184 {After=>$g_book->Worksheets($g_book->Worksheets->{Count})})->{Name} = $name;
185 }
186 elsif ($cmd eq 'right')
187 {
188 # add worksheet at the right of given worksheet
189 $g_book->Worksheets->add(
190 {After=>$g_book->Worksheets($arg)})->{Name} = $name;
191 }
192 else
193 {
194 # add worksheet at the left of given worksheet
195 $g_book->Worksheets->add(
196 {Before=>$g_book->Worksheets($arg)})->{Name} = $name;
197 }
198 $g_sheet = $g_book->Worksheets($name);
199}
200
201#******************************************************************************
202# FUNCTION
203# excelOpenText
204# DESCRIPTION
205# open a text file to excel
206# PARAMETERS
207# para 1 - text filename
208# para 2 - delimiter character
209# RETURNS
210# none
211#******************************************************************************
212sub excelOpenText()
213{
214 my $file = $_[0];
215 my $delimiter = $_[1];
216
217 $g_excel = Win32::OLE->new('Excel.Application', 'Quit') or die "Oops, cannot start Excel";
218 #Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);
219
220 $curr_obj = \$g_excel;
221
222 $g_book = $g_excel->Workbooks->Open($file, 0, 0, 6, 0, 0, 0, 2, $delimiter);
223 $g_sheet = $g_book->Worksheets(1);
224}
225
226
227#******************************************************************************
228# FUNCTION
229# excelSaveAs
230# DESCRIPTION
231# save as another excel file
232# PARAMETERS
233# para 1 - filename
234# RETURNS
235# none
236#******************************************************************************
237sub excelSaveAs()
238{
239 $g_book->SaveAs("$_[0]");
240}
241
242
243#******************************************************************************
244# FUNCTION
245# excelClose
246# DESCRIPTION
247# close excel file
248# PARAMETERS
249# none
250# RETURNS
251# none
252#******************************************************************************
253sub excelClose()
254{
255 if ($g_excel_name ne "")
256 {
257 $g_book->SaveAs("$g_excel_name");
258 }
259 $g_book->Close();
260}
261
262
263#******************************************************************************
264# FUNCTION
265# excelWriteRow
266# DESCRIPTION
267# write data items to row
268# PARAMETERS
269# para 1 - row
270# para 2 - start of col
271# para 3 - reference to data array (e.g. \@abc)
272# RETURNS
273# none
274#******************************************************************************
275sub excelWriteRow()
276{
277 my $row = $_[0];
278 my $col = $_[1];
279 my $data_ref = $_[2];
280
281 foreach my $item (@$data_ref)
282 {
283 excelSetFontAttr($g_sheet->Cells($row,$col));
284 $g_sheet->Cells($row,$col)->{Value} = "$item";
285 $col++;
286 }
287}
288
289
290#******************************************************************************
291# FUNCTION
292# excelWriteCol
293# DESCRIPTION
294# write data items to column
295# PARAMETERS
296# para 1 - start of row
297# para 2 - column
298# para 3 - reference to data array (e.g. \@abc)
299# RETURNS
300# none
301#******************************************************************************
302sub excelWriteCol()
303{
304 my $row = $_[0];
305 my $col = $_[1];
306 my $data_ref = $_[2];
307
308 foreach my $item (@$data_ref)
309 {
310 excelSetFontAttr($g_sheet->Cells($row,$col));
311 $g_sheet->Cells($row,$col)->{Value} = "$item";
312 $row++;
313 }
314}
315
316
317#******************************************************************************
318# FUNCTION
319# excelWrite
320# DESCRIPTION
321# write a cell
322# PARAMETERS
323# para 1 - row
324# para 2 - col
325# para 3 - data
326# RETURNS
327# none
328#******************************************************************************
329sub excelWrite()
330{
331 my $row = $_[0];
332 my $col = $_[1];
333 my $data = $_[2];
334
335 excelSetFontAttr($g_sheet->Cells($row,$col));
336 $g_sheet->Cells($row,$col)->{Value} = "$data";
337}
338
339
340#******************************************************************************
341# FUNCTION
342# excelRead
343# DESCRIPTION
344# read a cell
345# PARAMETERS
346# para 1 - row
347# para 2 - col
348# RETURNS
349# data read
350#******************************************************************************
351sub excelRead()
352{
353 my $row = $_[0];
354 my $col = $_[1];
355
356 return $g_sheet->Cells($row,$col)->{Value};
357}
358
359
360#******************************************************************************
361# FUNCTION
362# excelDelCol
363# DESCRIPTION
364# delete a column
365# PARAMETERS
366# para 1 - column to delete
367# RETURNS
368# none
369#******************************************************************************
370sub excelDelCol()
371{
372 $g_sheet->Columns($_[0])->Delete;
373}
374
375
376#******************************************************************************
377# FUNCTION
378# excelDelRow
379# DESCRIPTION
380# delete a row
381# PARAMETERS
382# para 1 - row to delete
383# RETURNS
384# none
385#******************************************************************************
386sub excelDelRow()
387{
388 $g_sheet->Rows($_[0])->Delete;
389}
390
391
392#******************************************************************************
393# FUNCTION
394# excelColAlignment
395# DESCRIPTION
396# set alignment to specified column
397# PARAMETERS
398# para 1 - column to set
399# para 2 - alignment type (center, right, left)
400# RETURNS
401# none
402#******************************************************************************
403sub excelColAlignment()
404{
405 my $col;
406 my $align;
407 my %alignHash = ('center' => xlCenter, 'right' => xlRight, 'left' => xlLeft);
408
409 ($col, $align) = @_;
410
411 $g_sheet->Columns($col)->{HorizontalAlignment} = $alignHash{$align};
412}
413
414
415#******************************************************************************
416# FUNCTION
417# excelColAlignmentAll
418# DESCRIPTION
419# set alignment to all columns
420# PARAMETERS
421# para 1 - alignment type (center, right, left)
422# RETURNS
423# none
424#******************************************************************************
425sub excelColAlignmentAll()
426{
427 my $col;
428 my $align;
429 my %alignHash = ('center' => xlCenter, 'right' => xlRight, 'left' => xlLeft);
430
431 ($align) = @_;
432
433 $g_sheet->Cells->EntireColumn->{HorizontalAlignment} = $alignHash{$align};
434}
435
436
437#******************************************************************************
438# FUNCTION
439# excelSetTextOrientation
440# DESCRIPTION
441# set text orientation to specified cell
442# PARAMETERS
443# para 1 - row to set
444# para 2 - column to set
445# para 3 - degree
446# RETURNS
447# none
448#******************************************************************************
449sub excelSetTextOrientation()
450{
451 my $row;
452 my $col;
453 my $degree;
454
455 ($row, $col, $degree) = @_;
456
457 $g_sheet->Cells($row,$col)->{'Orientation'} = $degree; # background color
458}
459
460
461#******************************************************************************
462# FUNCTION
463# excelAutoFilter
464# DESCRIPTION
465# enable auto-filter for all data (usually used when all data are ready)
466# PARAMETERS
467# none
468# RETURNS
469# none
470#******************************************************************************
471sub excelAutoFilter()
472{
473 $g_sheet->Cells->AutoFilter();
474}
475
476
477#******************************************************************************
478# FUNCTION
479# excelAutoFit
480# DESCRIPTION
481# set auto-fit to specified column
482# PARAMETERS
483# para 1 - column to set
484# RETURNS
485# none
486#******************************************************************************
487sub excelAutoFit()
488{
489 $g_sheet->Columns($_[0])->AutoFit;
490}
491
492
493#******************************************************************************
494# FUNCTION
495# excelAutoFitAll
496# DESCRIPTION
497# set auto-fit to all.
498# note that this only works when there's already data in cells.
499# PARAMETERS
500# none
501# RETURNS
502# none
503#******************************************************************************
504sub excelAutoFitAll()
505{
506 $g_sheet->Cells->EntireColumn->AutoFit;
507}
508
509
510#******************************************************************************
511# FUNCTION
512# excelSetFontName
513# DESCRIPTION
514# set cell font name (e.g. Arial)
515# PARAMETERS
516# para 1 - font name
517# RETURNS
518# none
519#******************************************************************************
520sub excelSetFontName()
521{
522 my $fontName;
523
524 ($fontName) = @_;
525
526 $g_fontAttr{'Name'} = $fontName if ($fontName ne "");
527}
528
529
530#******************************************************************************
531# FUNCTION
532# excelSetFontSize
533# DESCRIPTION
534# set cell font size
535# PARAMETERS
536# para 1 - font size
537# RETURNS
538# none
539#******************************************************************************
540sub excelSetFontSize()
541{
542 my $fontSize;
543
544 ($fontSize) = @_;
545
546 $g_fontAttr{'Size'} = $fontSize if ($fontSize > 0);
547}
548
549
550#******************************************************************************
551# FUNCTION
552# excelSetFontColor
553# DESCRIPTION
554# set cell font color
555# PARAMETERS
556# para 1 - font color name
557# RETURNS
558# none
559#******************************************************************************
560sub excelSetFontColor()
561{
562 my $fontColor;
563
564 ($fontColor) = @_;
565
566 $g_fontAttr{'ColorIndex'} = $g_fontColorHash{$fontColor} if (defined $g_fontColorHash{$fontColor});
567}
568
569
570#******************************************************************************
571# FUNCTION
572# excelSetFontBold
573# DESCRIPTION
574# set cell font bold
575# PARAMETERS
576# para 1 - font bold
577# RETURNS
578# none
579#******************************************************************************
580sub excelSetFontBold()
581{
582 my $fontBold;
583
584 ($fontBold) = @_;
585
586 $g_fontAttr{'Bold'} = $fontBold if ($fontBold =~ /[0-1]/);
587}
588
589
590#******************************************************************************
591# FUNCTION
592# setVisible
593# DESCRIPTION
594# write excel file
595# PARAMETERS
596# para 1 - visible (1) or not (0)
597# RETURNS
598# none
599#******************************************************************************
600sub setVisible()
601{
602 $$curr_obj->{Visible} = 1;
603}
604
605
606#******************************************************************************
607# FUNCTION
608# excelWrapText
609# DESCRIPTION
610# set wrap-text to specified column
611# (use this when there's data in specified column)
612# PARAMETERS
613# para 1 - column to set
614# RETURNS
615# none
616#******************************************************************************
617sub excelWrapText()
618{
619 $g_sheet->Columns($_[0])->{WrapText} = 1;
620}
621
622
623#******************************************************************************
624# FUNCTION
625# excelWrapTextAll
626# DESCRIPTION
627# set wrap-text to all columns
628# PARAMETERS
629# none
630# RETURNS
631# none
632#******************************************************************************
633sub excelWrapTextAll()
634{
635 $g_sheet->Cells->EntireColumn->{WrapText} = 1;
636}
637
638
639#******************************************************************************
640# FUNCTION
641# excelSetColumnWidth
642# DESCRIPTION
643# set width of specified column
644# PARAMETERS
645# para 1 - column to set
646# para 2 - width
647# RETURNS
648# none
649#******************************************************************************
650sub excelSetColumnWidth()
651{
652 $g_sheet->Columns($_[0])->{ColumnWidth} = $_[1];
653}
654
655
656#******************************************************************************
657# FUNCTION
658# excelSetColumnWidthAll
659# DESCRIPTION
660# set width of all columns
661# PARAMETERS
662# para 1 - width
663# RETURNS
664# none
665#******************************************************************************
666sub excelSetColumnWidthAll()
667{
668 $g_sheet->Cells->EntireColumn->{ColumnWidth} = $_[0];
669}
670
671
672#******************************************************************************
673# FUNCTION
674# excelShrinkToFit
675# DESCRIPTION
676# set shrink-to-fit to specified column
677# PARAMETERS
678# para 1 - column to set
679# RETURNS
680# none
681#******************************************************************************
682sub excelShrinkToFit()
683{
684 $g_sheet->Columns($_[0])->{ShrinkToFit} = 1;
685}
686
687
688#******************************************************************************
689# FUNCTION
690# excelShrinkToFitAll
691# DESCRIPTION
692# set shrink-to-fit to all columns
693# PARAMETERS
694# noneß
695# RETURNS
696# none
697#******************************************************************************
698sub excelShrinkToFitAll()
699{
700 $g_sheet->Cells->EntireColumn->{ShrinkToFit} = 1;
701}
702
703#******************************************************************************
704# Internal Data
705#******************************************************************************
706
707#******************************************************************************
708# Program Start
709#******************************************************************************
710
7111; # return success
712
713#******************************************************************************
714# Internal Function
715#******************************************************************************
716
717#******************************************************************************
718# FUNCTION
719# excelSetFontAttr
720# DESCRIPTION
721# set cell font attributes
722# PARAMETERS
723# $oleObject_ref - reference to ole object, may be cells, range, rows, columns, etc.
724# RETURNS
725# none
726#******************************************************************************
727sub excelSetFontAttr()
728{
729 my $oleObject_ref;
730
731 ($oleObject_ref) = @_;
732
733 #$g_sheet->Cells($row,$col)->Interior->{'ColorIndex'} # background color
734
735 while (my ($key, $val) = each(%g_fontAttr))
736 {
737 $oleObject_ref->Font->{$key} = $val if ($val ne "");
738 }
739}
740
741
742#******************************************************************************
743# FUNCTION
744# trim
745# DESCRIPTION
746# trim left and right spaces
747# PARAMETERS
748# $str [IN] - string to process
749# RETURNS
750# processed string
751#******************************************************************************
752sub trim()
753{
754 my $str;
755 ($str) = @_;
756
757 $str =~ s/(^\s*)|(\s*$)//g;
758 return $str;
759}