Search code examples
informix4gl

How to convert .txt files to .xls files using informix 4GL codes


I got a question to be disscuss.I am working on INFORMIX 4GL programs. That programs produce output text files.This is an example of the output:

 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|        

From that output text files(.txt) files, we can open it manually from the excel(.xls) files.From this case, is that any 4gl codes or any commands that we can use it for open the text files in microsoft excell automatically right after we run the program?If there any ideas,please share with me... Thank You


Solution

  • The output shown is in the normal Informix UNLOAD format, using the pipe as a delimiter between fields. The nearest approach to this for Excel is a CSV file with comma-separated values. Generating one of those from that output is a little fiddly. You need to enclose fields containing a comma inside double quotes. You need to use commas in place of pipes. And you might have to worry about backslashes too.

    It is a moot point whether it is easier to do the conversion in I4GL or whether to use a program to do the conversion. I think the latter, so I wrote this script a couple of years ago:

    #!/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