如何设置Excel公式与Win32 :: OLE?

有人可以告诉我,为什么...->{FormulaR1C1} = '=SUMME( "R[-3]C:R[-1]C" )'; 不起作用。 在结果应该出现的单元格中,我得到“#Wert!” (也许“价值”英文)。 随着WENN(IF)公式,我得到了我所期望的。

 #!C:\Perl\bin\perl.exe use warnings; use strict; use Win32::OLE qw; use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; my $xl = Win32::OLE::Const -> Load( 'Microsoft Excel' ); my $excelfile = 'win32_ole_excel.xls'; my $excel = Win32::OLE -> GetActiveObject( 'Excel.Application' ) || Win32::OLE -> new( 'Excel.Application', 'Quit' ) or die $!; my $workbook = $excel -> Workbooks -> Add(); my $sheet = $workbook -> Worksheets( 1 ); $sheet -> Activate; $sheet->Range( 'A3' )->{Value} = 10; $sheet->Range( 'B3' )->{FormulaR1C1} = '=WENN( "RC[-1]" > 5; "OK"; "Not OK")'; # IF(,,); workes fine $sheet->Range( 'G1' )->{Value} = 3; $sheet->Range( 'G2' )->{Value} = 7; $sheet->Range( 'G3' )->{Value} = 6; $sheet->Range( 'G4' )->{FormulaR1C1} = '=SUMME( "R[-3]C:R[-1]C" )'; # SUM(); doesn't work $workbook -> SaveAs( { Filename => $excelfile, FileFormat => xlWorkbookNormal } ); 

SUM范围周围不需要引号。 它应该是明确的:

 =SUMME(R[-3]C:R[-1]C) 

另外一点 – 你的IF / WENN公式是不正确的。 它试图将string“RC [-1]”与数字5进行比较,并提出YES! STRING是更大的。 这不是在做你认为正在做的事情……你应该在这里引用引用的引号。

编辑:这是我的你的代码版本,运行没有任何错误。 变更被评论。 必须对英文版的Excel进行一些更改。 对抗ActivePerl 5.10.1 Build 1006

 #!C:\Perl\bin\perl.exe use warnings; use strict; # CHANGE - empty qw caused compilation error use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; my $xl = Win32::OLE::Const -> Load( 'Microsoft Excel' ); # CHANGE - set path my $excelfile = 'C:\win32_ole_excel.xls'; my $excel = Win32::OLE -> GetActiveObject( 'Excel.Application' ) || Win32::OLE -> new( 'Excel.Application', 'Quit' ) or die $!; my $workbook = $excel -> Workbooks -> Add(); my $sheet = $workbook -> Worksheets( 1 ); $sheet -> Activate; $sheet->Range( 'A3' )->{Value} = 10; # CHANGE - Use IF, use commas, took quotes out around range $sheet->Range( 'B3' )->{FormulaR1C1} = '=IF( RC[-1] > 5, OK, Not OK)'; # IF(,,); workes fine $sheet->Range( 'G1' )->{Value} = 3; $sheet->Range( 'G2' )->{Value} = 7; $sheet->Range( 'G3' )->{Value} = 6; # CHANGE - Use SUM, took quotes out around range $sheet->Range( 'G4' )->{FormulaR1C1} = '=SUM(R[-3]C:R[-1]C)'; # SUM(); doesn't work $workbook -> SaveAs( { Filename => $excelfile, FileFormat => xlWorkbookNormal } ); 

在perl-community.de的帮助下,我现在有一个解决scheme:我必须设置

 $excel->{ReferenceStyle} = $xl->{xlR1C1}; 

并用Z1S1代替R1C1

 =SUMME(Z(-2)S:Z(-1)S) 

但看起来在德国版本中,我必须在A1Z1S1R1C1 )之间进行select。