2011-02-15 61 views
0

本來我試圖用Excel打開一個XML文件。但是,由於這需要很長時間,我自己將XML解析爲一個製表符分隔的文本文件。我以爲我在互聯網上找到了正確的語法。我使用Excel宏記錄的值嘗試了下面的代碼。我有一個18列製表符分隔文件。調用「工作表」方法時,下面代碼的最後一行出現錯誤。如何使用Perl和OLE在Excel中打開製表符分隔的文本文件?

錯誤消息: 無法調用「工作單」,而不在./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); 

回答

2

你真的有使用解析TSV Excel中?如果沒有在你的文件看中,你可能會使用這樣的:

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和not 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) 
+0

您不應該使用split/regexes分析xSV文件。與CSV相同,使用適當的模塊(文本:CSV_CS) – DVK 2011-02-15 13:34:34

0

嘗試使用Text::CSV。它支持用戶定義的分隔符。請參閱模塊說明。

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; 
相關問題