朋友们,我写了一个 Perl 脚本来将一组 CSV 文件转换成电子表格格式,使用Spreadsheet::WriteExcel
. 经过一些研究,我得出结论,没有将列宽固定为自动调整选项的选项。
所以我正在做的是在同一个脚本中,我使用Win32::OLE 模块打开了那个 XLS 文件,在执行此操作时我收到了一条错误消息
Can't use an undefined value as a HASH reference
对应的代码是:
# spread sheet creation
my $workbook = Spreadsheet::WriteExcel->new($file_name);
# ...
my $worksheet = $workbook->add_worksheet($work_sheet_name);
# ...
$worksheet->write($rowNum, $j,$_,$default_format);
在这些步骤之后,我在同一个脚本中有更多的行:
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application');
$Excel->{'Visible'} = 0; #0 is hidden, 1 is visible
$Excel->{DisplayAlerts}=1; #0 is hide alerts
# Open File and Worksheet
my $return_file_name="C:\\Users\\admin\\Desktop\\Report_Gen\\$file_name";
print ">>$return_file_name<<";
my $Book = $Excel->Workbooks->Open($return_file_name); # open Excel file
foreach my $Sheet (in $Book->Sheets) {
my $LastCol = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByColumns})->{Column}; # mentioned error is from this line
my $mylastcol = 'A';
for (my $m=1;$m<$LastCol;$m++) {$mylastcol++;}
my @columnheaders = ('A:'.$mylastcol);
foreach my $range (@columnheaders){
$Sheet->Columns($range)->AutoFit();
}