0

我必须编写代码来密码保护工作表以及密码保护单元格。到目前为止,我已经尝试过以下代码

#!c:\perl\bin\
use strict;

use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
use Win32::OLE::Variant;
use Win32::OLE::NLS qw(:LOCALE :DATE);

$Win32::OLE::Warn = 3; # Die on Errors.
use Spreadsheet::WriteExcel;
my $excelfile = 'protect.xls';

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit'); 
$Excel->{DisplayAlerts}=0;   
my $Book = $Excel->Workbooks->Add();
$Book->SaveAs($excelfile); 
my $Sheet = $Book->Worksheets("Sheet1");
$Sheet->{Name} = "DidItInPerl";

my $vtfalse =  Variant(VT_BOOL, 0);
my $vttrue =  Variant(VT_BOOL, 1);
my $Range = $Sheet->Range("A1:c2");

$Sheet->Protection->AllowEditRanges->Add({Title=>"MyRange", Range=>$Range},'1222');


 $Sheet->Protect(                                         
 {DrawingObjects=>$vttrue, 
 Contents=>$vttrue, 
 Scenarios=>$vttrue,});

但这不起作用。

4

1 回答 1

0

您需要更改此行:

$Sheet->Protection->AllowEditRanges->Add({Title=>"MyRange", Range=>$Range},'1222');

至:

$Sheet->Protection->AllowEditRanges->Add(
    "MyRange", $Range, '1222' );
于 2013-04-07T00:36:04.950 回答