21
Sat, Dec
3 New Articles

TechTip: Excel Flies Higher with JExcelApi

Microsoft
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
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 email address is being protected from spambots. You need JavaScript enabled to view it..





BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: