【问题标题】:How to convert .txt files to .xls files using informix 4GL codes如何使用 informix 4GL 代码将 .txt 文件转换为 .xls 文件
【发布时间】:2013-10-29 03:54:30
【问题描述】:

我有一个问题要讨论。我正在研究 INFORMIX 4GL 程序。该程序生成输出文本文件。这是输出示例:

 Lot No|Purchaser name|Billing|Payment|Deposit|Balance|                
 J1006|JAUHARI BIN HAMIDI|5285.05|4923.25|0.00|361.80|                 
 J1007|LEE, CHIA-JUI AKA LEE, ANDREW J. R.|5366.15|5313.70|0.00|52.45| 
 J1008|NAZRIN ANEEZA BINTI NAZARUDDIN|5669.55|5365.30|0.00|304.25|     
 J1009|YAZID LUTFI BIN AHMAD LUTFI|3180.05|3022.30|0.00|157.75|        

从输出的文本文件(.txt)文件中,我们可以从 excel(.xls)文件中手动打开它。在这种情况下,我们可以使用任何 4gl 代码或任何命令来打开文本文件在我们运行程序后自动在microsoft excel中?如果有任何想法,请与我分享...谢谢

【问题讨论】:

    标签: informix 4gl


    【解决方案1】:

    显示的输出采用正常的 Informix UNLOAD 格式,使用管道作为字段之间的分隔符。 Excel 最接近的方法是使用逗号分隔值的 CSV 文件。从该输出中生成其中一个有点繁琐。您需要将包含逗号的字段括在双引号内。您需要使用逗号代替管道。而且您可能还需要担心反斜杠。

    在 I4GL 中更容易进行转换还是使用程序进行转换,这是一个有争议的问题。我认为是后者,所以几年前我写了这个脚本:

    #!/usr/bin/env perl
    #
    # @(#)$Id: unl2csv.pl,v 1.1 2011/05/17 10:20:09 jleffler Exp $
    #
    # Convert Informix UNLOAD format to CSV
    
    use strict;
    use warnings;
    use Text::CSV;
    use IO::Wrap;
    
    my $csv = new Text::CSV({ binary => 1 }) or die "Failed to create CSV handle ($!)";
    my $dlm = defined $ENV{DBDELIMITER} ? $ENV{DBDELIMITER} : "|";
    my $out = wraphandle(\*STDOUT);
    my $rgx = qr/((?:[^$dlm]|(?:\\.))*)$dlm/sm;
    
    # $csv->eol("\r\n");
    
    while (my $line = <>)
    {
        print "1: $line";
        MultiLine:
        while ($line eq "\\\n" || $line =~ m/[^\\](?:\\\\)*\\$/)
        {
            my $extra = <>;
            last MultiLine unless defined $extra;
            $line .= $extra;
        }
        my @fields = split_unload($line);
        $csv->print($out, \@fields);
    }
    
    sub split_unload
    {
        my($line) = @_;
        my @fields;
        print "$line";
    
        while ($line =~ $rgx)
        {
            printf "%d: %s\n", scalar(@fields), $1;
            push @fields, $1;
        }
        return @fields;
    }
    
    __END__
    
    =head1 NAME
    
    unl2csv - Convert Informix UNLOAD to CSV format
    
    =head1 SYNOPSIS
    
    unl2csv [file ...]
    
    =head1 DESCRIPTION
    
    The unl2csv program converts a file from Informix UNLOAD file format to
    the corresponding CSV (comma separated values) format.
    
    The input delimiter is determined by the environment variable
    DBDELIMITER, and defaults to the pipe symbol "|".
    It is not assumed that each input line is terminated with a delimiter
    (there are two variants of the UNLOAD format, one with and one without
    the final delimiter).
    
    =head1 EXAMPLES
    
    Input:
    
      10|12|excessive|cost \|of, living|
      20|40|bou\\ncing tigger|grrrrrrrr|
    
    Output:
    
      10,12,"excessive","cost |of, living"
      20,40,"bou\ncing tigger",grrrrrrrr
    
    =head1 RESTRICTIONS
    
    Since the csv2unl program does not know about binary blob data, it
    cannot convert such data into the hex-encoded format that Informix
    requires.
    It can and does handle text blob data.
    
    =head1 PRE-REQUISITES
    
    Text::CSV_XS
    
    =head1 AUTHOR
    
    Jonathan Leffler <jleffler@us.ibm.com>
    
    =cut
    

    【讨论】:

    • 感谢您的回答,但我仍然不确定如何将值直接从文本文件执行到 (.xls)。我使用 TinyTerm 作为 informix 的终端。上面的代码看起来不同。我不确定我是否可以申请我的计划......
    • 如果您知道如何将文本转换为.xls 文件,那就太好了。 Perl 有支持它的模块(Spreadsheet::Write,可能未维护,或Spreadsheet::Wright,Spreadsheet::Write 的一个分支)。 Excel 接受 CSV 文件;将您拥有的内容转换为 CSV 并不太难(看看我提供的 Perl 脚本——它确实使用 Text::CSV 来处理输出的大部分繁重工作。
    • IMO,生成 CSV 可能是将数据获取到 Excel 的最简单方法。 OTOH,我不是 Excel 的超级用户,我不知道将基于文本的数据导入 Excel 的替代方法是什么。
    • 脚本是 Perl 脚本。它可以放在 $HOME/bin、$INFORMIXDIR/bin、/usr/local/bin 或您选择的其他目录中。我称之为unl2csv(我也有相反的csv2unl 脚​​本)。你现在用你的 I4GL 程序创建文件;然后,在将其导入 Excel 之前,使用以下脚本将其转换为 CSV 文件:unl2csv i4gl.output &gt; excel.csv。如果您愿意,您可以在您的 I4GL 代码中自动执行此操作(RUN 语句)。
    【解决方案2】:

    我通过使用 Excel progid ("?mso-application progid=\"Excel.Sheet\"?) 编写 XML 从 4GL 代码生成 Excel 文件,因此 Excel 会照此打开它。

    这就像从 4GL 编写 HTML,您只需将 HTML 代码写入文件。但是使用 Excel,您可以编写 XML。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      相关资源
      最近更新 更多