问题
How can a I guarantee that no pop-up dialogs will appear when I automate Microsoft Excel through OLE? I'm using a Perl module (Win32::OLE). I can avoid most dialog pop-ups using the following code:
use Win32::OLE;
use Win32::OLE::Variant;
use Win32::OLE::Const;
my $excel_symbols = Win32::OLE::Const->Load('Microsoft Excel');
my $excel = Win32::OLE->new('Excel.Application', sub { $_[0]->Quit();} );
$excel->{'Visible'} = 0;
$excel->{'DisplayAlerts'} = 0;
$excel->Workbooks->Open('c:\some_excel_file.xls',
{ 'UpdateLinks' => $excel_symbols->{'xlUpdateLinksNever'},
'ReadOnly' => 1,
'IgnoreReadOnlyRecommended' => 1
});
However for some files, I continue to get a dialog with the following text:
This file is not a recognizable format.
- If you know the file is from another program which is incompatible with Microsoft Excel, click Cancel, then open this file in its original application. If you want to open the file later in Microsoft Excel, save it in a format that is compatible, such as text format.
- If you suspect the file is damaged, click Help for more information about solving the problem.
- If you still want to see what text is contained in the file, Click OK. Then click Finish in the Text Import Wizard.
OK Cancel
Sometimes a similar dialog appears that contains 'OK', 'Cancel' and 'Help' buttons.
I cannot control the quality of files that are provided to the scripts.
回答1:
You could consider using Spreadsheet::ParseExcel (albeit it may lack features you need) or Apache POI (albeit it will need some wrapping to use in a Perl script) instead of calling the Excel engine over OLE. That way you won't get any Excel-generated dialogs.
回答2:
I revisited this issue and found a solution.
Copy the file before processing to a temporary location. Then save the file before closing it in Excel:
File::Copy::copy('c:\some_excel_file.xls', 'c:\temp\SFO3jfd.xls');
my $book = $excel->Workbooks->Open('c:\temp\SFO3jfd.xls',
{ 'UpdateLinks' => $excel_symbols->{'xlUpdateLinksNever'},
'ReadOnly' => 1,
'IgnoreReadOnlyRecommended' => 1
});
$book->Save();
$book->Close();
Why this works:
Excel 2003 automatically recalculates the formulas in documents that were created in an older version of Excel. Furthermore, macros could be invoked when the document is opened. All of this means that there could be changes made on a document, even though your script doesn't perform any such operations.
By saving the document before closing, you avoid the dialog requesting that you save the file. Using a temporary file ensures that the original file does not get changed during the validation operation. If you aren't concerned about this, you might consider validating in-place.
回答3:
Here is full documentation for Open method. I wonder if CorruptLoad parameter is what you need.
回答4:
If you are trying of process all xl files in the tree, some of them may be open by other users and have the ~ prefix.
来源:https://stackoverflow.com/questions/461869/automate-excel-file-processing-in-perl-and-avoid-dialog-ui-interactions