Originally I tried to open a XML file with Excel. But as this takes ages I parsed the XML myself into a Tab delimited text file. I thought I found the correct syntax in the internet. I tried below code using the values I recorded with an Excel Macro. I have an 18 column Tab delimited file. The error occures in the last line of below code when calling "Worksheets" Method.
Error message: Can't call method "Worksheets" without a package or object reference at ./PostProcessingD开发者_StackOverflow社区ev.pl line 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);
Do you really have to parse TSV using Excel? If there is nothing fancy in your files, you might well use something like this:
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
}
}
If Excel is needed for a reason I cannot see, the problem is that OpenText
method returns True/False and not Workbook
object. This works:
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)
Thanks for at all for your answers. This resolved my issue. All I wanted to do with the OpenText method was to convert a TSV file to Excel as this is the result format I require. As I couldn't find a solution on the web before I like to provide the complete code:
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;
try using Text::CSV. it supports user defined delimiters. See module description.
精彩评论