【发布时间】:2016-10-15 15:39:24
【问题描述】:
这类似于A copy of Excel Addin is created in My Documents after saving,只是我使用的是Perl而不是VBA,使用xls文件而不是xlsm,并且行为的负面影响是不同的。
我继承了一个在 Windows 2003 Server 上作为 SYSTEM 运行的 Perl 脚本 (Perl 5.8.8)。将 Excel 2003 模板文件复制到唯一的、完全定义的路径位置后,它会使用 OLE 在 Excel 中打开唯一文件,编辑文件,保存文件,然后关闭文件。结果是编辑后的文件既保存在正确的、完全定义的路径位置,也保存在默认用户配置文件的 Documents 文件夹中。
这会导致数千个此类文件堆积在 C: 驱动器上,因为每位新入职的管理员都会在其 Documents 文件夹中获得一份副本。
添加设置 $OUT 值的代码:
if (!$db->Sql("EXEC GetDetails 'name'"))
{
while ($db->FetchRow()>0)
{
@DataIn = $db->Data();
$name = $DataIn[0];
$IN = $DataIn[1];
$OUT = $DataIn[2];
opendir(DIR,"$OUT") || die "$OUT directory does not exist $!\n";
#... loop of proprietary code
#...
@Completed = $db1->Data();
#...
&formatExcelReport #The code that I previously posted
#...
# more proprietary code
# end of loop
} #end of while
}#end of if
我最初发布的代码:
# Initialize Excel object
eval {Win32::OLE->new('Excel.Application', 'Quit')};
eval {$Excel = Win32::OLE->GetActiveObject('Excel.Application')};
unless (defined $Excel)
{
$Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit');
}
$infiles = "Report_Template.xls";
$infiles = $OUT."/".$infiles;
$db6->Sql("EXEC FormatResults '".$Completed[0]."','".$Completed[1]."'");
$row = 2;
$fileName = $Completed[0]."_".$Completed[1];
$uniquefile = $fileName.$printdate.".xls";
# $OUT is a fully defined path on the E: drive
$reportfile = "$OUT"."\\".$uniquefile;
copy($infiles,$reportfile);
$Book = $Excel->Workbooks->Open("$reportfile");
$sheetnum = 1;
my $Sheet = $Book->Worksheets($sheetnum);
# Set Headers
$Header = $Sheet->PageSetup->{'CenterHeader'};
$Header = $Header." Results Test Code: ".$Completed[0]." Worksheet: ".$Completed[1]." Date: ".$headerdate;
$Sheet->PageSetup->{'CenterHeader'}= $Header;
# More file editing
# ...
$Book->Save();
$Book->Close(0);
Win32::OLE->new('Excel.Application', 'Quit');
这个问题的根源是 Save() 命令吗?我应该改用 SaveAs() 吗?
也欢迎就 Excel 的使用方式提供任何其他反馈。
谢谢!
【问题讨论】:
标签: excel perl save windows-server-2003 ole