Tips and Techniques: Displaying Dates in Excel PDF Print E-mail
Written by Robert Cozzi   
Tuesday, 06 September 2005

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.


Last Updated ( Tuesday, 06 September 2005 )
 
Discuss (10 posts)
neerajain
Tips and Techniques: Displaying Dates in Excel
Jan 17 2006 16:46:00
I am trying to upload an excel spreadsheet to i-series. The spreadsheet has few date fields. When I transfer this spreadsheet to i-series it does not transfer the correct dates. It shows some funky numbers insted. I think it converts the date into some numeric value that is different then date. Any resolution?
#116080
David Abramowitz
Tips and Techniques: Displaying Dates in Excel
Jan 17 2006 12:43:00
As far as I know, Excel stores dates as a <u>lillian</u> number. That is the number of days from a fixed date. <p>If you know what the date is, it is possible to calculate the field back to a true date by using CEExxxx date routines, or your own calculation. <p>Dave
#116079
neerajain
Tips and Techniques: Displaying Dates in Excel
Jan 17 2006 11:56:00
I am trying to upload an excel spreadsheet to i-series. The spreadsheet has few date fields. When I transfer this spreadsheet to i-series it does not transfer the correct dates. It shows some funky numbers insted. I think it converts the date into some numeric value that is different then date. Any resolution?
#116078
Guest.Visitor
Tips and Techniques: Displaying Dates in Excel
Sep 09 2005 12:49:00
Try performing a date related calculation on any of the 0001-01-01 dates. =Month(D2) for example returns #VALUE.
#116077
R.Cozzi
Tips and Techniques: Displaying Dates in Excel
Sep 09 2005 09:15:00
I don't think I'm in disagreement with you. I do want an option and there should be one there (on CPYTOIMPF) and on my own CPYTOCSV command. Today neither has the option. <p>Fixed-length fields stem back to 80-column cards, not System/38 (which supported VARLEN fields). But I get your point. <p>This "blank truncation" feature is similar to the "Blank Transparency" option in RPG IV that we debated before IBM introduced RPG IV. They did not want to modify data, and therefore programs would blow up with decimal data errors. I advocated a Blank Transparency option that would simply pass numeric fields containing X'40' through without a DDS, effectively turning them into zero values.
#116076
Guest.Visitor
Tips and Techniques: Displaying Dates in Excel
Sep 07 2005 16:19:00
I'm always amazed at statements such as <p>"The reason CPYTOIMPF is limited is that it outputs fixed-length field values (it does not trim off trailing blanks)..." <p>CPYTOIMPF outputs fixed-length fields as fixed-length values, and variable-length fields as variable-length values. It would be nice to have an optional feature to trim trailing blanks from fixed-length fields, but you have to remember that the full fixed-length value, complete with trailing blanks is the ACTUAL VALUE OF THE DATA IN THE FIELD. Truncating the data means altering the data. Granted, in very many cases altering the data may produce the most convenient output format. Still, the CPYTOIMPF command should not as a default alter the value of any data -- fixed-length or otherwise. <p>I think the problem stems from the fact that there's so much System-38, AS/400, iSeries legacy data and legacy mindset from when variable-length fields were not an option. Fixed-length fields are taken for granted. The more you work with folks from Oracle, SQL, etc. background, the more you'll see that fixed-length character data has a more limited role in most databases because fixed length actual values have a much more specific purpose. <p>In other words, don't blame the utility for being accurate.
#116075
bwierman@spi-ind.com
Tips and Techniques: Displaying Dates in Excel
Sep 07 2005 14:53:00
I've pasted a small sample directly from an excel spreadsheet that was downloaded from a file on our iSeries. The dates both appear and function as they should. <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6b27f966/3' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#116074
Guest.Visitor
Tips and Techniques: Displaying Dates in Excel
Sep 07 2005 14:08:00
Not exactly. If you use this with "DATE" type fields, it doesn't import them correctly. Also "null" dates (01-01-0001 on the iSeries) aren't recognized by Excel at ALL.
#116073
bwierman@spi-ind.com
Tips and Techniques: Displaying Dates in Excel
Sep 07 2005 12:44:00
Client Access has a built-in "Data Transfer from iSeries" function ('Actions', 'Receive File From Host...') which has numerous formats for output, including native excel formats (through MS Excel 8). It's VERY fast, has considerable query capabilities, and easily handles everything that this column notes as a shortcoming of CPYTOIMPF.
#116072
MC Press Web Site Staff
Tips and Techniques: Displaying Dates in Excel
Jan 17 2006 16:46:00
This is a discussion about <B>Tips and Techniques: Displaying Dates in Excel</b>.<p align='center'><a href=http://www.mcpressonline.com/mc?1@232.1KNKfHX1eQT.17@.6b27ccdc>Click here for the article</a>.</p>
#116071


Discuss...
User Rating: / 0
PoorBest 
Related Articles
< Prev   Next >

   MC-STORE.COM