use strict; use warnings; use Win32::OLE; use File::Spec; my $ksFactor = 0.177346958265686; my $knMaxWidth = 100; my $xl = Win32::OLE->new('Excel.Application'); $xl->{EnableEvents} = 0; $xl->{ScreenUpdating} = 1; $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; my $sht = $wb->Sheets(1); my $dialogue = $xl->FileDialog(4); #msoFileDialogFolderPicker = 4 $dialogue->{Title} = "Choose a directory"; $dialogue->Show; if ($dialogue->SelectedItems->Count == 0) {die "No directory selected";} my $dirName = $dialogue->SelectedItems(1); my $filemask = File::Spec->catfile($dirName, '*.jpg'); my @sFiles = sort(glob($filemask)); my $nWidth = int(sqrt(scalar @sFiles)) - 1; my $nHeight = int(scalar @sFiles / $nWidth); if ($nWidth * $nHeight < scalar @sFiles) {$nHeight++}; my $nCount = 0; for my $row (1..$nHeight) { my $nMaxHeight = 0; for my $col (1..$nWidth) { if ($nCount < scalar @sFiles) { my $picCurrent = $sht->Pictures->Insert($sFiles[$nCount]); $picCurrent->{Top} = $sht->Cells($row, $col)->{Top}; $picCurrent->{Left} = $sht->Cells($row, $col)->{Left}; my $nPicWidth = $picCurrent->{Width}; my $nPicHeight = $picCurrent->{Height}; if ($nPicWidth > $knMaxWidth) { $picCurrent->{Width} = $knMaxWidth; $picCurrent->{Height} = $knMaxWidth / $nPicWidth * $nPicHeight; } $nMaxHeight = $xl->Max($picCurrent->{Height}, $nMaxHeight); my ($vol, $path, $file) = File::Spec->splitpath($sFiles[$nCount]); $sht->Cells($row, $col)->{Value} = $file; if ($row == 1 || $sht->Columns($col)->{ColumnWidth} < $picCurrent->{Width} * $ksFactor) { $sht->Columns($col)->{ColumnWidth} = $picCurrent->{Width} * $ksFactor; } $nCount++; } } $sht->Rows($row)->{RowHeight} = $nMaxHeight + 11.75; } for my $col (1..$nWidth) { $sht->Columns($col)->{ColumnWidth}++; } my ($vol, $path, $file) = File::Spec->splitpath($sFiles[0]); my $setup = $sht->PageSetup; $setup->{LeftHeader} = $vol . $path; $setup->{Orientation} = 1; #xlPortrait $setup->{Zoom} = 0; #False $setup->{FitToPagesWide} = 1; $setup->{FitToPagesTall} = 1;