0

我有一个问题要讨论。我正在研究 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 excell 中的文本文件在我们运行程序后自动运行?如果有任何想法,请与我分享...谢谢

4

2 回答 2

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
于 2013-10-29T04:13:15.637 回答
0

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

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

于 2015-01-02T17:14:19.570 回答