1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
use warnings; use strict; use Cwd; use OLE; use Win32::OLE::Const "Microsoft Excel"; &init; sub init { my $directory = &getcwd; &edit_excel ( "$directory/TemplateAuswertung.xlsx" ); } sub edit_excel { my ( $xl_report ) = @_; my ( $excel, $workbook, $sheet, $row ); $excel = CreateObject OLE 'Excel.Application'; $excel -> {Visible} = 1; $workbook = $excel -> Workbooks -> Open ( "$xl_report" ) or &terminate ( "open $xl_report" ); #$sheet = $workbook -> Worksheets -> Add ( { After => $workbook -> Worksheets ( $workbook -> Worksheets -> {Count} ) } ); $sheet = $workbook -> Worksheets(1); $sheet -> Activate(); $sheet -> Range ( "A:B" ) -> Font -> {size} = 10; $sheet -> Range ( "C:D" ) -> Font -> {Size} = 10; $sheet -> Range ( "A:D" ) -> Font -> {Bold} = "True"; $sheet -> Range ( "A:D" ) -> {HorizontalAlignment} = xlHAlignLeft; $sheet -> Range ( "C11" ) -> {HorizontalAlignment} = xlHAlignCenter; $sheet -> Range ( "C13" ) -> {HorizontalAlignment} = xlHAlignCenter; $sheet -> Range ( "D11" ) -> {HorizontalAlignment} = xlHAlignCenter; $sheet -> Range ( "D13" ) -> {HorizontalAlignment} = xlHAlignCenter; #$sheet -> Range ( "A18" ) -> {Value} = "CAN Signal"; #$sheet -> Range ( "B18" ) -> {Value} = "PASSED"; #$sheet -> Range ( "C18" ) -> {Value} = "No Comment"; #$sheet -> Range ( "D18" ) -> {Value} = "RAS"; $sheet -> Range ( "A2" ) -> Activate; $excel -> ActiveWindow -> {FreezePanes} = "True"; #$sheet -> {Name} = "Test Report" . $workbook -> Worksheets -> {Count}; $excel -> {DisplayAlerts} = 0; # $workbook -> SaveAs ( "$xl_report" ); $workbook -> Save; $workbook -> Close; #$excel -> Quit; } sub terminate { print "\a\a\nCould not @_ for the following reason:\n\n$!\n"; print "This program will now terminate. Press <ENTER> to exit."; <STDIN>; exit; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
my %hash;
while( my $wert = <STDIN> ) {
if( ! $hash{$wert} ) {
$hash{$wert} = 1;
# Excel Zeile anhängen
}
}
# Oder
my @array;
while( my $wert = <STDIN> ) {
if( ! grep { $wert eq $_; } @array ) {
push( @array, $wert);
# Excel Zeile anhängen
}
}
QuoteDas Excel-Template ist manuell erstellt worden und alles, was oberhalb der Zeile 14 ist statisch und bleibt auch so. Nur ab Zeile 14 möchte ich nach unten automatisch ausfüllen.
1
2
3
4
5
6
7
8
9
my $row = 14; # die erste Zeile, die geschrieben werden soll
foreach my $dataset (@datasets) {
$sheet->Range( 'A' . $row )->{Value} = $dataset->{name};
$sheet->Range( 'B' . $row )->{Value} = $dataset->{testresult};
# Oder:
$sheet->Range( "C$row" )->{Value} = $dataset->{inca};
# usw ...
$row++; # oder $row += 1; oder $row = $row + 1;
}