Skip to content

Instantly share code, notes, and snippets.

@sironekotoro
Created January 27, 2022 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sironekotoro/b8fb6d4cd45974b4d374d8f3748fd127 to your computer and use it in GitHub Desktop.
Save sironekotoro/b8fb6d4cd45974b4d374d8f3748fd127 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Win32::OLE;
# https://perldoc.jp/docs/modules/libwin32-0.26/Win32/OLE.pod
# use existing instance if Excel is already running
my $excel;
eval { $excel = Win32::OLE->GetActiveObject('Excel.Application') };
die "Excel not installed" if $@;
unless ( defined $excel ) {
$excel = Win32::OLE->new( 'Excel.Application', sub { $_[0]->Quit; } )
or die "Oops, cannot start Excel";
}
# get a new workbook
my $book = $excel->Workbooks->Add;
# write to a particular cell
my $sheet = $book->Worksheets(1);
$sheet->Cells( 1, 1 )->{Value} = "foo";
# save and exit
# USERNAME は変更してね
$book->SaveAs('C:\Users\USERNAME\Desktop\test.xlsx') or die("$!");
undef $book;
undef $excel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment