friend 们,我编写了一个 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();
}
最佳答案
I wrote a Perl script to convert a set of CSV files into spreadsheet format using Spreadsheet::WriteExcel. After some research I came to conclusion that there is no option to fix column width as Auto-fit option.
自动调整是 Excel 中的运行时选项,因此无法使用 Spreadsheet::WriteExcel 通过文件格式创建它。
但是,Spreadsheet::WriteExcel 文档包含 an example of how to simulate autofit并对所涉及的一些问题进行了解释。
关于perl - 如何使用OLE模块处理Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15732650/