如何在Excel中使用Perl和OLE打开制表符分隔的文本文件?

本来我试图用Excel打开一个XML文件。 但是,由于这需要很长时间,我自己将XMLparsing为制表符分隔的文本文件。 我以为我在互联网上find了正确的语法。 我使用我用Excelmacroslogging的值来尝试下面的代码。 我有一个18列制表符分隔的文件。 调用“工作表”方法时,下面代码的最后一行出现错误。

错误消息: 不能调用方法“Worksheets”没有包或对象引用在./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); 

你真的需要使用ExcelparsingTSV吗? 如果你的文件中没有什么特别的,你可以使用这样的东西:

 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) 

尝试使用Text :: CSV 。 它支持用户定义的分隔符。 请参阅模块说明。

谢谢你的回答。 这解决了我的问题。 我想用OpenText方法做的是将TSV文件转换为Excel,因为这是我需要的结果格式。 因为在我喜欢提供完整的代码之前,我无法在networking上find解决scheme:

 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;