最初我尝试用Excel打开XML文件。但是,由于这需要很长时间,我将自己的XML解析为制表符分隔的文本文件。我以为我在互联网上找到了正确的语法。我使用我用Excel宏记录的值尝试下面的代码。我有一个18列制表符分隔文件。调用“工作表”方法时,在代码的最后一行出现错误。
错误讯息: 在./PostProcessingDev.pl第70行没有包或对象引用时,无法调用方法“工作表”。
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
Win32::OLE->Option(Warn => 3);
use strict;
my $Excel;
my $Sheet;
my $Workbook;
$Excel = CreateObject OLE "Excel.Application";
$Workbook = $Excel->Workbooks->OpenText({Filename =>"$inSelf->{'TXT'}",
Origin => xlMSDOS,
StartRow => 1,
DataType => xlDelimited,
TextQualifier => xlDoubleQuote,
ConsecutiveDelimiter => "False",
Tab => "True",
Semicolon => "False",
Comma => "False",
Space => "False",
Other => "False",
FieldInfo => [[1, xlTextFormat],
[2, xlTextFormat],
[3, xlTextFormat],
[4, xlTextFormat],
[5, xlTextFormat],
[6, xlTextFormat],
[7, xlTextFormat],
[8, xlTextFormat],
[9, xlTextFormat],
[10, xlTextFormat],
[11, xlTextFormat],
[12, xlTextFormat],
[13, xlTextFormat],
[14, xlTextFormat],
[15, xlTextFormat],
[16, xlTextFormat],
[17, xlTextFormat],
[18, xlTextFormat]],
TrailingMinusNumbers => "True"});
$Sheet = $Workbook->Worksheets(1);
答案 0 :(得分:2)
你真的要用Excel解析TSV吗?如果您的文件中没有任何花哨的内容,您可能会使用以下内容:
my $file = 'some tsv file';
{
open my $in,"<",$file or die "$file: $!";
while(<$in>) {
chomp;
my @cols = split /\t/;
# do something with columns in @cols array
}
}
如果由于我无法看到的原因需要Excel,则问题是OpenText
方法返回True / False而不是Workbook
对象。这有效:
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
use Data::Dump; # for dd
Win32::OLE->Option(Warn => 3);
my $Excel = Win32::OLE->new("Excel.Application");
$Excel->Workbooks->OpenText({
Filename => 'enter the full path to file',
Origin => xlMSDOS,
StartRow => 1,
DataType => xlDelimited,
TextQualifier => xlDoubleQuote,
Tab => "True",
TrailingMinusNumbers => "True"
});
my $Sheet = $Excel->ActiveWorkbook->Worksheets(1);
my $Data = $Sheet->UsedRange()->{Value};
dd $Data; # arrayref (rows) of arrayrefs (columns)
答案 1 :(得分:0)
尝试使用Text::CSV。 它支持用户定义的分隔符。请参阅模块说明。
答案 2 :(得分:0)
感谢您的回答。这解决了我的问题。我想用OpenText方法做的就是将TSV文件转换为Excel,因为这是我需要的结果格式。在我想提供完整的代码之前,我无法在网上找到解决方案:
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Excel';
use strict;
Win32::OLE->Option(Warn => 3);
my $FileName = "Complete Path of TSV File";
my $Excel = Win32::OLE->new("Excel.Application");
# Open Tab separated Text file in Excel, all 18 columns are "Text" formated
$Excel->Workbooks->OpenText({
Filename => $FileName,
Origin => xlMSDOS,
StartRow => 1,
DataType => xlDelimited,
TextQualifier => xlDoubleQuote,
Tab => "True",
FieldInfo => [[1, xlTextFormat],
[2, xlTextFormat],
[3, xlTextFormat],
[4, xlTextFormat],
[5, xlTextFormat],
[6, xlTextFormat],
[7, xlTextFormat],
[8, xlTextFormat],
[9, xlTextFormat],
[10, xlTextFormat],
[11, xlTextFormat],
[12, xlTextFormat],
[13, xlTextFormat],
[14, xlTextFormat],
[15, xlTextFormat],
[16, xlTextFormat],
[17, xlTextFormat],
[18, xlTextFormat]],
TrailingMinusNumbers => "True"
});
my $Sheet = $Excel->ActiveWorkbook->Worksheets(1);
$Sheet->Activate;
my $Workbook = $Excel->ActiveWorkbook;
# Replace the "*.txt" file extension by "*.xls" for Excel
$FileName =~ s/txt$/xls/;
# Turn off the "This file already exists" message.
$Excel->{DisplayAlerts} = 0;
# Save file as Excel 2000-2003 Workbook (command for Excel 2007)
$Workbook->SaveAs({Filename => $FileName, FileFormat => xlExcel8});
$Excel->Quit;