If you've had formatting issues, read on.
What? Isn't this an RPG column? It sure is. But copying data from the iSeries
to CSV format for reading in MS Excel is extremely widespread throughout the
iSeries community.
So let's start out with some basics. First, the most often used method of
copying DB2/400 data to CSV format is the OS/400 CPYTOIMPF command. This
command--although slow, narrow in scope, and not very versatile--does convert
data into CSV format so that it can be opened in MS Excel.
The reason CPYTOIMPF is limited is that it outputs fixed-length field values
(it does not trim off trailing blanks), it does not provide a method to format
numeric or date output (however, it does support converting dates to one
consistent format), and it does not have an option to generate a row of column
headings based on the field names or the DDS COLHDG keyword.
And perhaps the worst thing of all about CPYTOIMPF is that as the number of
records in the file that are to be converted grows, performance of the command
seems to fall off a cliff. Certainly, more records means more time to convert,
but the curious design of CPYTOIMPF obviously wasn't meant for a large-volume
conversion.
The bottom line: Use CPYTOIMPF for quick copies to CSV format, when you just
need the raw data without column headings and totals and when you don't mind
trailing blanks in the output fields.
Once you have your data in CSV format, you can edit it so that it displays
better in MS Excel. For example, adding a header row, perhaps reformatting a
date column, and of course adding column totals are often necessary. In
addition, avoiding zero suppression is always fun.
In MS Excel, functions can be used to format individual cells, and adding
column headings is only a matter of inserting an empty row and then typing in
the column headings.
Two key areas have been problems for iSeries data moved to CSV format and
opened in MS Excel: date values and numeric values.
Date values in Excel are somewhat odd anyway, but there is a function that
allows you to embed a date value in a cell and keep it in date format. Normally,
when CPYTOIMPF copies a date value, it converts that value to text in *ISO
format (yyyy-mm-dd) and encloses it in quotes.
This isn't bad, and Excel displays the date value correctly. But that's not
really what you want because you're not telling Excel that the field is a date
data type. Fortunately, Excel has a DATE function that can be used to format a
set of data so that it is not only displayed as a date, but interpreted as a
date by Excel. This allows data sets to be sorted correctly from within
Excel.
To use the DATE function, you need to write the date value to the CSV file as
follows, where yyyy is the two- or four-digit year, mm is the two-digit month,
and dd is the two-digit day of the month::
=DATE(yyyy,mm,dd)
Yes, adding MS Excel functions into a CSV works fine. Excel parses functions
it detects in CSV files as if they had come from an actual Excel file. So if a
CSV file contained this data...
02345,"XYZ Company","2005-07-31"
...it could be changed to this...
02345,"XYZ Company",=DATE(2005,07,31)
...and the date would now be identified as a date.
The next thing we want to do is correct the problem with the number 02345. In
Excel, this numeric value is normally displayed as 2345. But what if we don't
want to suppress leading or trailing zeroes in the value? Enter the "quote"
function, which looks like this:
=("02345")
Whatever value is enclosed in the quote function is displayed. So 034.50,
which would normally be displayed as 34.5, is displayed as 034.50.
There are several other options for converting DB2 database files to Excel
besides CPYTOIMPF. In fact, the POI interface (class library) for Java is pretty
robust and allows you write directly to an Excel file. I don't particularly like
POI on the iSeries simply because (a) it requires Java and consequently requires
that I load the JVM, and (b) it's overkill for most of the conversion I've ever
come across.
In addition to POI, there are several third-party and free solutions out
there. Some perform well; some do not. Most of the non-free solutions work
effectively, but many of the "free" solutions work only in very limited scope
(although the authors believe they work in all situations, I have yet to find
one that works on my test files).
Of course, CPYTOCSV (Copy to CSV File) in the RPG xTools is available free,
but requires the RPG xTools runtime.
Of the chargeable third-party solutions, some allow you to output column
headings, and CPYTOCSV also allows you to convert dates to the =DATE function,
convert numbers to the =("...") function, and insert column totals (=SUM
function).
So fear not! Dates can be converted to Excel properly.
Bob Cozzi is a programmer/consultant,
writer/author, and software developer of the RPG xTools, a popular
add-on subprocedure library for RPG IV. His book The Modern RPG
Language has been the most widely used RPG programming
book for nearly two decades. He, along with others, speaks at and runs the
highly-popular RPG
World conference for RPG
programmers.
|