如何处理Excel使用OLE模块?
朋友们,我写了一个Perl脚本,使用Spreadsheet::WriteExcel
将一组CSV文件转换为电子表格格式。 经过一番研究后,我得出结论:没有select将列宽固定为“自适应”选项。
所以我在做的是在我使用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(); }
我写了一个Perl脚本,使用Spreadsheet :: WriteExcel将一组CSV文件转换为电子表格格式。 经过一番研究后,我得出结论:没有select将列宽固定为“自适应”选项。
Autofit是Excel中的一个运行时选项,因此无法使用Spreadsheet :: WriteExcel通过文件格式创build。
但是,Spreadsheet :: WriteExcel文档包含一个如何模拟autofit的例子,并解释了一些涉及的问题。