[英]Why does Perl's Win32::OLE complain about “Invalid index” for an Excel worksheet?
[英]Perl+Excel: “use Win32::OLE::Const” not having an effect
我一直在使用StrawberryPerl v 5.014和Win32 :: OLE在Windows 7系統上進行項目。
我正在嘗試使用另一台運行Windows XP SP3的PC,以並行開發同一項目。 在新系統上,我已經安裝了Strawberry Perl v 5.016,並使用cpanp安裝了Win32 :: OLE和Win32 :: OLE :: Const。
我在新系統中使用了相同的腳本源。
事實證明,在新系統中,
use Win32::OLE::Const 'Microsoft Excel';
沒有效果。 我收到錯誤消息:
Bareword "xlExcel8" not allowed while "strict subs" in use in StatementExcel.pm line 159.
我提到的第一個原始系統中未顯示此錯誤。
該怎么辦?
TIA,海倫
以下是該程序的摘錄:
package StatementExcel;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(LoadExcel ProcessPreparedSheet);
use strict;
use warnings;
use Encode;
use 5.016;
use utf8;
use Win32::Console;
use autodie;
use warnings qw< FATAL utf8 >;
use StateConversion;
use Carp;
use Win32::OLE qw(CP_UTF8);
use Win32::OLE::Const 'Microsoft Excel';
use Cwd;
use Text::CSV::Unicode;
use File::BOM qw( :all );
use List::MoreUtils 'first_index';
...
$xlBook -> SaveAs( $xlFile, xlExcel8);
$i = Win32::OLE->LastError();
if ($i) {
PrintT $parms{LogStructRef}{HANDLE}, "Error trying to save Excel file:\n", $i;
} # end if ($i)
PrintT $parms{LogStructRef}{HANDLE}, 'Press <return> to continue...'; # Wait for user input...
$j = <STDIN>;
# Clean up
$xlBook->{Saved} = 1;
$xlApp->Quit;
$xlBook = 0;
$xlApp = 0;
...
注意:在PerlMonks上交叉發布: http ://www.perlmonks.org/?node_id = 985596
事實證明,發生此問題是由於Excel 2010和Excel 2003之間的OLE對象庫存在差異。在Win XP系統上,已安裝Excel 2003。 在Windows 7系統上,已安裝Excel 2010。 保存Excel文件時,我指示它使用“ xlExcel8” OLE常量將其保存為Excel 2003“ xls”格式(而不是Excel 2010的“ xlsx”格式)。
事實證明,該常量在OLE 2003對象庫中不存在。
解決方案很簡單:保存文件時不指定格式-它使用默認格式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.