保存Excel电子表格文件,无需密码

时间:2016-03-21 16:00:13

标签: excel perl win32ole

我正在尝试打开受密码保护的Excel,并在没有密码的情况下保存它。 我知道密码。当我尝试运行下面的代码时,文件将使用密码保存。

#!/usr/bin/perl

use strict;
use warnings,

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel *';

my $file = "in.xls";
my $outFile = "out.xls";

my $Excel = Win32::OLE->new ('Excel.Application', 'Quit');
$Excel->{'Visible'} = 0;    #0 is hidden, 1 is visible
$Excel->{'DisplayAlerts'} = 0;  #0 is hide alerts

my $Book = $Excel->Workbooks->Open({FileName => "$file", Password => 'test'});
my $Sheet = $Book->Worksheets('test');
$Sheet->Activate();
$Book->SaveAs({Filename=>"$outFile",FileFormat=>xlWorkbookNormal});
$Excel->Quit();

请告知。

谢谢你, -Andrey

2 个答案:

答案 0 :(得分:1)

尝试:

$Book->Unprotect('test'); # Where 'test' is your password

参考:Workbook.Unprotect Method (Excel)

答案 1 :(得分:1)

我想出了答案:

$Book->SaveAs({Filename=>"$outFile",FileFormat=>xlWorkbookNormal, Password => undef});

谢谢你们。