TechTip: Excel Flies Higher with JExcelApi PDF Print E-mail
Written by Giuseppe Costagliola   
Thursday, 23 September 2004

You can create your Excel spreadsheets faster than ever with JExcelApi. In "Tech Tip: Excel on the Fly," I presented SQL2XLS, a utility to create Excel workbooks directly from a database query. Written in RPG, it relies on an open-source set of Java APIs written by the POI-Jakarta team. Because RPG starts the JVM the first time you call a Java method and then leaves the JVM on, this utility is very useful for quickly creating small Excel documents, say up to 1,000 rows. But you'll certainly experience poor performance for a medium-sized document, and a big one is pretty much impossible. For those cases, pure Java code makes the best use of these APIs. And by the way, POI-Jakarta is not the only way to create Excel documents directly from your iSeries for free.

That's why I present here, along with a Java implementation with POI-Jakarta, a brand new implementation that uses a powerful set of APIs called JExcelApi, written by Andy Khan and issued under the GNU Lesser General Public License. JExcelApi dramatically reduces the time needed to create large Excel documents. I tested both implementations by creating a 30,000-row by 30-column worksheet, and JExcelApi took approximately half the time POI did: I was able to create this 10 MB Excel document in its final form (with headers, edited figures, formatted dates, etc.) in less than 90 seconds.

Follow these simple steps to make these utilities work on your iSeries:

First, you need the POI and JExcelApi set of APIs. Maybe you already downloaded and installed the POI-2.0.jar from a Jakarta mirror site when you tried the RPG/Java SQL2XLS. If so, keep using this jar file; otherwise, please follow the instructions from the article.

Then, you must download JExcelApi directly from Andy Khan's home page. Put the latest version (jexcelapi_2_4_4.tar.gz) in a temporary directory of your PC and extract the content into a folder. You will find documentation, sources, examples and, most important, the jxl.jar that you should copy into the /excel folder of your iSeries IFS.

From a command line, optimize the Java classes by running the following command:

CRTJVAPGM CLSF('/excel/jxl.jar') OPTIMIZE(40)


This will take some time.

Download the utilities from the MC Press site and put all the CMD, RPGLE, and CLLE sources into a source file and the Java sources and classes into the /excel IFS folder, both in your iSeries.

Compile the programs and the commands as usual (option 14 from PDM easily does the job). When creating the command, don't forget to specify a CPP that has the same name as the command plus the letter "C." You can use the Java classes as downloaded or recreate them using a simple CL interface with Qshell (available in the download) to make the task easier.

Now you're ready to go, but before launching the commands, you need to know a few things:

The SQL2POI and SQL2JXL commands have a CL CPP that, before invoking Java, calls an RPG program that receives the SQL statement, reformats it into a valid SQL statement by replacing the special characters (like the square brackets), writes it into a text document into the excel folder, assigns an ID to it, and passes it back to the CPP. This is because the RUNJVA command we use to invoke the Java classes accepts parameters up to 256 characters each, but certainly you would be able to run a much longer SQL statement. The Java module will get this SQLID among the parameters and read the SQL statement from the IFS. At the end, the text file containing the SQL statement is deleted. If you write an Excel document from scratch, both implementations work quite the same, but if you add data or add a new sheet to an existing workbook, the POI API rewrites the document at the end, while the JExcelApi clears it at the beginning and writes the data at the end. This means that if something goes wrong while updating the workbook, you will get an empty document and, unfortunately, lose the original document. To avoid this, the CL forces Java to write data into a work copy that will be renamed at the end of procedure, if everything went well.

Last but not least, as in the RPG version, the command provides the parameter COLHDRS(*LABEL), but note that the ability to retrieve via JDBC the field label (which can be set by SQL statement LABEL ON or DDS keyword COLHDG) in addition to the field name has been added recently, and it works only starting from V5R2. To have it working correctly even at this release level, you need to be current with the latest PTFs, otherwise, if you want to include a column header, I suggest you use an SQL statement with the "as" clause, like "select MyField as [wrappedcolumnheader] from MyTable."

Enjoy it!

Giuseppe Costagliola is a programmer in Turin, Italy. You can reach him at This e-mail address is being protected from spam bots, you need JavaScript enabled to view it .






Last Updated ( Thursday, 23 September 2004 )
 
Discuss (32 posts)
MAF
Re:TechTip: Excel Flies Higher with JExcelApi
Apr 01 2008 08:48:33
Hi thanks for these great utilities. Im using SQL2JXL and am having a couple of problems. I'm sure i'm just doing something wrong.
1) When i use a template XLS, it resizes my column widths. Is there a way to stop this?

and

2) Again when using a template. It inserts a couple of blank lines between the template and the sql data retrieved. Can this be stopped?

Thanks for your help!
#121804
Guest.Visitor
TechTip: Excel Flies Higher with JExcelApi
Oct 19 2005 09:52:00
I've seen a couple of messages about "authority problems" but I've yet to see any solutions. <p>The error I see is: <BR>
MCH1001 - Attempt to use permanent system object CDRCVRT without authority. <p>CDRCVRT is an IBM supplied *PGM object in QSYS2. It has *PUBLIC set to *EXCLUDE. <p>I'm assuming that this is teh program for the Convert a Graphic Character String (CDRCVRT, QTQCVRT) API. <p>Thanks
#115613
Guest.Visitor
TechTip: Excel Flies Higher with JExcelApi
Sep 19 2005 08:09:00
We are using SQL2JXL/SQL2POI on our V5R2 for more than a year and are very happy with the tool. <p>Recently we have installed SQL2JXL/SQL2POI on our V5R1 machine, but are getting the error "Function check. JVA0122 unmonitored by SQL2JXLC at statement 0000009200".(The output of the Jave Shell Display is put below) <p>It looks to me that Java is missing something, but I don't know what. <p>The following Licensed programs are installed: <BR>
5722JC1 *BASE Toolbox for Java <BR>
5722JV1 *BASE Developer Kit for Java <BR>
5722JV1 3 Java Developer Kit 1.2 <BR>
5722JV1 4 Java Developer Kit 1.1.8 <BR>
5722JV1 5 Java Developer Kit 1.3 <p>Any ideas ? <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6b176865/29' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#115612
david.brown@kalon.co.uk
TechTip: Excel Flies Higher with JExcelApi
May 03 2005 05:49:00
I had the same problem but with the help of our friends at IBM they pointed out that you will get this error if your Jobs CCSID is 65535. <BR>
I changed my job to 37 (CHGJOB CCSID(37)) and it worked fine. Depending on your country and language you may need to use a different CCSID. <BR>
Regards <BR>
David
#115611
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Feb 28 2005 13:42:00
The mask should be "#,##0.00". <BR>
Excel formats numbers according to the windows settings.
#115610
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Feb 28 2005 13:40:00
run the cmd with JDEBUG(*YES) to have more details on the error.
#115609
MauroRomeo
TechTip: Excel Flies Higher with JExcelApi
Feb 24 2005 15:29:00
After migration in a new As400 V5R3 SQL2POI and SQL2JXL don't work
Every request end in error.
This is log:
SQL2JXL SQLSTMT('select * from mylib/clienti') TOXLS('/mydir/clienti.xls')
Programma Java completato con codice di uscita 1
Errore di funzione. JVA0122 non monitorato da SQL2JXLC all'istruzione ad alto livello 0000000092, istruzione '0000'.
Error running SQL2JXL.
As i can do.
Thank you
Mauro Romeo romeo@photorec.it
#115608
dsenger@erico.com
TechTip: Excel Flies Higher with JExcelApi
Jan 05 2005 13:31:00
I'm using the SQL2POI utility and have a requirement to format numbers using comma for the decimal symbol and a period for the thousands separator. I tried changing the code to: <p>style2d.setDataFormat(df.getFormat("#.##0,00")); <p>but the resulting numeric cell value of 2 is being formated as 2.00000 instead of 2,00 <p>Do I need to set a locale somewhere ? Any other ideas ? <p>Thanks, <p>Dave Senger
#115607
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 19 2004 13:11:00
You can use SQL2JXL to get data from a SqlServer and put them directly into an .xls in your iSeries. <p>First, download a suitable driver like jTDS, an open source 100% pure Java (type 4) JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000 and 2005) from <a href="http://jtds.sourceforge.net/">http://jtds.sourceforge.net/</a> and put it into the “/excel” folder of your iSeries. <p>Then modify the SQL2JXL* as outlined below, recompile the SQL2JXL CMD, CLLE and the class and run the command: <p>SQL2JXL <BR>
SQLSTMT('select * from Northwind.DBO.Customers') <BR>
TOXLS(…….) COLHDRS(*FLDNAM) <BR>
DRIVER(*JTDS) RMTSYS(SQLServer) <BR>
USER('…………') PASSWORD(‘…………..’) <BR>
JDEBUG(*YES) <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6b176865/23' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#115606
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 16 2004 06:54:00
Maybe this can help: <p><a href="http://www-912.ibm.com/s_dir/slkbase.NSF/0/7c96bcfc5d5f7ebd86256ac4006e25d6?OpenDocument">http://www-912.ibm.com/s_dir/slkbase.NSF/0/7c96bcfc5d5f7ebd86256ac4006e25d6?OpenDocument</a>
#115605
Guest.Visitor
TechTip: Excel Flies Higher with JExcelApi
Nov 15 2004 18:11:00
When i enter the command: <p> SQL2JXL SQLSTMT('select * from books') <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TOXLS('/xlsfiles/books.xls') <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COLHDRS(*FLDNAM) <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JDEBUG(*YES) <p>I get this in the Java Shell Display: <p> Connecting to LOCALHOST with "com.ibm.db2.jdbc.app.DB2Driver" <p> ERROR: INTERNAL ERROR: Creation of DB2Driver object for registering with DriverManager failed. <BR>
&nbsp;&nbsp;Java program completed with exit code 1 <p>Not sure what I am missing here.
#115604
buffcessna
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 18:01:00
OK, that's cool...I changed the CL to use the SQL2XLS and it works <BR>
fine...So, I guess I'll just use that one...Thanx for all the input guys... <p>Shane
#115603
buffcessna
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 17:59:00
Refer to Dan Stevens post...
#115602
dstephens@rocktenn.com
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 17:51:00
Many people put a MONMSG CPF0000 to catch any extraneous errors from crashing a CL. The proper way would be to have a standard error routine to handle the situation, like MONMSG MSGID(CPF0000 CPA0000) EXEC(GOTO CMDLBL(ERROR)). To see examples of this, do a search on this site for STANDARD ERROR HANDLING. On the search results page look over to the right in the block "Results From the Forums". <p>Funny thing is, the first post shown when I did that was one of mine. :) <p>-dan
#115601
Ralph Daugherty
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 16:08:00
The question was supposed to follow Shane's posting of CL code which has a MONMSG MSG(CPF0000) in it. I was asking why, but if everything is working just disregard. <p> rd
#115600
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 14:14:00
What do you mean ?
#115599
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 14:13:00
They use a different set of Api's. <p>SQL2XLS and SQL2POI use Jakarta/POI api's that are more powerful while SQL2JXL uses Andy Khan's JExcelApi that is faster. <p>SQL2XLS uses RPG mixed with Java. This is excellent for small xls but cannot be used for large xls. For these ones, the pure Java SQL2POI and SQL2JXL are the best solution.
#115598
Ralph Daugherty
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 11:57:00
I have a question. Why do you MONMSG CPF0000 in your CL? <p> rd
#115597
buffcessna
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 11:09:00
I changed my CL to use the SQL2XLS utility instead of the SQL2JXL <BR>
utility...and it works...is there a difference between the two? <p>Shane
#115596
buffcessna
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 10:41:00
Ok, I enabled it and ran the program in debug...again, it works fine <BR>
when I run the command...however, when a user runs the command, it <BR>
comes up with the following error in their job log: <p> 3500 - SQL2JXL SQLSTMT('select rundated, runmach, runtime, runuser from<br>
&nbsp;&nbsp;casprun') TOXLS('/excel/casprun.xls') SHEETNAME('Run Time')<br>
&nbsp;&nbsp;COLHDRS(*LABEL) TITLE('Run Time') TITLEALIGN(*CENTER) JDEBUG(*YES)<br>
&nbsp;&nbsp;4500 - CALL PGM(SQL2JXLR) /* The CALL command contains <br>
&nbsp;&nbsp;parameters */<br>
Error CrtSQLstmt : open() failed. 3401 : Permission denied..<br>
&nbsp;12200 - SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA('Error running <br>
&nbsp;&nbsp;SQL2JXL') MSGTYPE(*ESCAPE)<br>
Error running SQL2JXL.<br>
CPF9898 received by CASQAQCC at 3500. (C D I R)<br> <p>What must I do in order to let regular users use this utility in a CL <BR>
program? <p>Shane
#115595
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 10 2004 08:41:00
write SQL2JXL into cmdline and press F4 and then F10. Scroll up to the end. The last parameter is JDEBUG. Put the value *YES.
#115594
buffcessna
TechTip: Excel Flies Higher with JExcelApi
Nov 09 2004 13:28:00
How do I do that? <p>Shane
#115593
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 09 2004 11:07:00
Please enable the Java console JDEBUG(*YES)and run the command again.
#115592
buffcessna
TechTip: Excel Flies Higher with JExcelApi
Nov 08 2004 17:30:00
Hey all, <p>I'm experiencing an error when I try to run this command... <p>I'm passing in the sql statement from an RPG program...basically, I'm <BR>
letting the user select which file they want to download and which <BR>
months data they want...so I'm building my sql statment in my RPG <BR>
program and passing a CL the statement... <p>When the user runs this command, they get the following error in their <BR>
job log: <p> 700 - SQL2JXL SQLSTMT('select usgdated, usgprt#, usgmach, usgamt,<br>
usgauom, usguser from caspusg where usgdated between ''2004-09-01''<br>
and <BR>
''2004-09-30'' order by usgmach') TOXLS('/excel/caspusg.xls')<br>
SHEETNAME('Paint Usage') COLHDRS(*LABEL) TITLE('Paint Usage')<br>
TITLEALIGN(*CENTER)<br>
&nbsp;&nbsp;4500 - CALL PGM(SQL2JXLR) /* The CALL command contains <BR>
&nbsp;&nbsp;parameters */<br>
&nbsp;&nbsp;9200 - RUNJVA CLASS('SQL2JXL')<br>
&nbsp;&nbsp;PARM(X'6185A783859361E2D8D3C9C4F1F2F8F4F1F94B8481A300' <BR>
&nbsp;&nbsp;'/excel/caspusg.xls' ' ' 'Paint Usage' 'L' 'Paint Usage' '-1' 'C' 'Y' <BR>
&nbsp;&nbsp;'Y' 'N' 'N' 'Y' 'N' 'SYS' 'N' 'NATIVE' 'LOCALHOST' ' ' ' ' 'N') <BR>
&nbsp;&nbsp;CLASSPATH('/QIBM/ProdData/OS400/jt400/lib/jt400.jar:/excel:/excel/jxl.ja <BR>
&nbsp;&nbsp;r') OPTION(*NONE) OUTPUT(*NONE)<br>
Java program completed with exit code 1<br>
Function check. JVA0122 unmonitored by SQL2JXLC at statement 0000009200, instruction X'0000'.<br>
&nbsp;12200 - SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) MSGDTA('Error running <BR>
&nbsp;&nbsp;SQL2JXL') MSGTYPE(*ESCAPE)<br>
Error running SQL2JXL.<br>
Function check. CPF9898 unmonitored by CASQAQCC at statement 700, <BR>
&nbsp;&nbsp;instruction X'001A'.<br>
CPF9898 received by CASQAQCC at 700. (C D I R)<br>
CPF9898 received by CASQAQCC at 700. (C D I R) <p>Is there some kind of special authority a user needs in order to run <BR>
this command? <p>I'm posting my CL program. <p>Shane <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6b176865/7' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#115591
gcostagliola@tin.it
TechTip: Excel Flies Higher with JExcelApi
Nov 03 2004 12:49:00
There is a new PTF available to solve the problem of returning correct COLHDG with native driver. <p>These are the instructions from Michael Swenson of IBM: <p>====================== <p>The test PTF, SI16233, is available for this problem. Prior to ordering the PTF there are a couple of things to consider: <p>- TEST PTFs should not be permanently applied in the event <BR>
&nbsp;&nbsp;&nbsp;&nbsp;there is a problem discovered with the PTF. <p>- Prior to ordering this PTF, you should order and install the <BR>
&nbsp;&nbsp;&nbsp;latest available superseded PTF(s): SI14670 <p> Installing the superseded PTF(s) is required to ensure the PTF can be properly removed (if needed) and to prevent the PTF from being <BR>
permanently applied by the system due to requisite PTF processing. <p>- Make sure the to review the cover letters for all PTFs delivered <BR>
&nbsp;&nbsp;&nbsp;(see the DSPPTF or DSPPTFCVR command) <p>===========
#115590
There are too many comments to list them all here. See the forum for the full discussion.

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

   MC-STORE.COM