在Perl自动化Excel文件的处理,避免对话框/ UI交互 [英] automate excel file processing in perl and avoid dialog/UI interactions

查看:160
本文介绍了在Perl自动化Excel文件的处理,避免对话框/ UI交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何能保证我没有弹出对话框会当我通过OLE自动化Microsoft Excel中出现?我使用一个Perl模块(的Win32 :: OLE)。我可以用下面的code避免大多数对话框弹出窗口:

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.


      
  • 如果您知道该文件是从另一个程序这是与不兼容
      Microsoft Excel中,单击取消,然后
      在其原来的打开这个文件
      应用。如果你想打开
      更新的文件在Microsoft Excel,保存
      的格式是兼容的,这样
      为文本格式。

  •   
  • 如果您怀疑文件已损坏,请单击有关的更多信息
      解决问题。

  •   
  • 如果您仍想看看文本文件中包含的,单击确定。
      然后单击Finish在文本导入
      向导。

  •   

确定取消

有时会出现类似的对话框,其中包含确定,取消和帮助按钮。

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.

推荐答案

我重新审视这个问题,并找到解决方案。

I revisited this issue and found a solution.

加工前将文件复制到一个临时位置。该Excel文件在关闭前,然后保存:

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();

为什么这个作品:

Excel 2003中自动重新计算在一个旧版本的Excel中创建的文档的公式。此外,在打开文档时,宏可以调用。所有这一切都意味着,有可能是对文档所做的更改,即使您的脚本不执行任何此类操作。

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.

这篇关于在Perl自动化Excel文件的处理,避免对话框/ UI交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆