如何使用informix 4GL代码将.txt文件转换为.xls文件

时间:2013-10-29 03:54:30

标签: informix 4gl

我有一个问题要讨论。我正在研究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中自动生成?如果有任何想法,请与我分享...谢谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我通过使用Excel progid编写XML来生成4GL代码中的Excel文件(&#34;?mso-application progid = \&#34; Excel.Sheet \&#34;?)因此Excel将其打开。

就像从4GL编写HTML一样,你只需要将HTML代码存档即可。但是使用Excel编写XML。