【问题标题】:How do I open a Tab delimited Text file in Excel using Perl and OLE?如何使用 Perl 和 OLE 在 Excel 中打开制表符分隔的文本文件?
【发布时间】:2011-02-15 09:17:02
【问题描述】:

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

错误信息: 无法在 ./PostProcessingDev.pl 第 70 行调用没有包或对象引用的方法“Worksheets”。

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);

【问题讨论】:

    标签: excel perl text ole


    【解决方案1】:

    您真的必须使用 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)
    

    【讨论】:

    • 您不应通过拆分/正则表达式解析 xSV 文件。与 CSV 相同,使用适当的模块 (Text:CSV_CS)
    • 感谢您的快速回答。不,我不使用 Excel 作为解析器。我自己编写的解析部分,因为我既不能使用也不能安装所需的 mudules。我在珍珠阵列中做所有事情。 TSV 以及 Excel 文件是输出。与打开 TSV 相比,逐个单元格地编写 Excel 单元格要慢(数万行),我只想打开 TSV 并将其存储为 Excel。在 4 行文本文件上尝试您的代码,它卡在 OpenText 方法中。我怎样才能看到会发生什么?我可以调试 OLE 方法吗?
    • 我没有正确理解你想要什么。如果您写出带有 .CSV 扩展名的 CSV,则 Excel 会关联并能够直接打开它。按OLE调试,有点棘手,不过你可以在初始化后把$Excel-&gt;{Visible} = 1;放到Excel里面就可以看到了。
    • @DVK 如果你知道你的数据是什么,为什么不拆分呢? TSV 具有制表符在类数据库数据中很少见的优点。自然不应该投入生产来处理任意数据。
    • @bvr - 相信我,当你的脚本第一次因为数据包含古怪的东西而失败时(它会),你会开始希望你没有重新发明自行车(很糟糕)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      非常感谢您的回答。这解决了我的问题。我想要对 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; 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-07
        • 1970-01-01
        • 2013-10-16
        相关资源
        最近更新 更多