23
Tue, Apr
1 New Articles

TechTip: SQL2XLSX - The Starter Kit

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Two popular utilities from some years back get updated, and a favorite author from the past resurfaces.

It's been over a decade since I presented two utilities that gave you the ability to create XLS worksheets from an SQL instruction in System i. Now it's time to present a revamped version of these utilities, the new SQL2XLSX command that will allow you to create XLSX worksheets.

I won't discuss details about the differences between XLS and XLSX; let's just say that XLS, created by Microsoft in 2003 (officially designated "Microsoft Excel 97-2003 Worksheet"), is still very popular, but in the Microsoft Office 2007 release, Microsoft decided to change the default file format to XLSX, based on the Office Open XML format.

You can get several benefits from using the new XLSX format; for example, in terms of the file size issue, it is reported that the new XLSX files can be up to 75 percent smaller than the old XLS files. Furthermore, in an XLS workbook, the limit is 65,536 rows and 256 columns while XLSX workbooks limits are increased to 1,048,576 rows and 16,384 columns.

XLSX is stored in a text file format, while XLS was stored in binary format. Therefore, the old macro support for this new file format is no longer supported, and Microsoft assigned a totally different file extension that allows the use of macros; it is named XLSM. The standard XLS file was able to hold spreadsheets that contain macros or not.

You can find more detailed info on Office Open XML format here.

Are you eager to build and try the new SQL2XLSX command that allows you to create both XLSX and XLSM? Let's do that.

In this article, I will give you the code and the instructions needed to create basic XLSX worksheets. You could create XLSM as well; just read the warnings at the end of the article.

You should be able to run this command on your system if your OS is between V5R3 and V7Rxx, provided that you have at least Java 6 installed (if you are still at V5R3/R4, you may need additional jar files, which you can find and download from the web; if you get errors while running the command on old releases, specify the JDEBUG(*YES) option and see what's missing). You can check the current Java version by typing QSH CMD('java -version') from the command line and the Java versions available by typing DSPLNK OBJ('/QOpenSys/QIBM/ProdData/JavaVM/*').

This SQL2XLSX command relies on POI Apache libraries as well, but you will see that if you want to create XLSX worksheets, you need more jars than in the past. You can find all in the official Apache POI - the Java API for Microsoft Documents site.

In order to have this utility running on earlier OS releases, instead of downloading the latest Apache POI library version, we'll use POI 3.9 because this is the latest one that runs with Java 6 on V5R3. A version implemented with the latest (at time of writing) Apache POI 1.17 will be presented in a future article.

That said, you can start to implement this utility by creating a subdirectory in your system where you will put the Apache POI jar files. Provided that in the past you already created the main directory /excel as required in the original SQL2XLS version, now create a new subdirectory in it with MD DIR('/excel/poi-3.9'). If you haven't yet created the main /excel directory, create it and then create the subdirectory in it.

Now connect to the download section of the Apache POI - the Java API for Microsoft Documents site and download the poi-bin-3.9-20121203.zip on your desktop.

Then extract and copy the following jar files into the subdirectory /excel/poi-3.9 you just created:

poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar

Rename them:

poi-3.9.jar
poi-ooxml-3.9.jar
poi-ooxml-schemas-3.9.jar

You can rename with Navigator or from the command line:

RNM ('/excel/poi-3.9/poi-3.9-20121203.jar') NEWOBJ('poi-3.9.jar')  

...

In the downloaded zip file within the folder ooxml-lib, you will find the following jar files:

dom4j-1.6.1.jar
xmlbeans-2.3.0.jar

You must also extract these and copy them into your subdirectory.

At the end, just to make sure that the required jars are in the right place, use this command to check:

DSPLNK OBJ('/excel/poi-3.9/*') DETAIL(*NAME)

101317CostagliolaFigure1 compressed 

Figure 1: Ensure that your jar files are in your subdirectory.

Now download the zip file of code onto your desktop.

Extract and copy SQL2XLSX(CMD), SQL2XLSXC (CLLE), and SQL2XLSXR (RPGLE) into a source file and compile as usual. Remember to create the SQL2XLSX command with parameter PGM(SQL2XLSXC).

Extract and copy the SQL2XLXNS.java into the /excel directory and create the .class file. To do so, create a CLP source member and paste the following QSH command into it:

QSH CMD('javac +
         -classpath /QIBM/ProdData/OS400/jt400/lib/jt400.jar:+
         /excel/poi-3.9/poi-3.9.jar:+
         /excel/poi-3.9/poi-ooxml-3.9.jar:+
         /excel/jtds.jar:+
         /excel/ojdbc14.jar: +
         -d /excel +
         /excel/SQL2XLXNS.java')

Compile the CLP and run it.

The jtds.jar and ojdbc14.jar are needed if you want to extract data from SQL server or an Oracle database; otherwise, they are not necessary.

That's all. Now you should be able to create XLSX worksheets.

Create the Worksheets

As I mentioned, the command presented here is a revamped version of the original SQL2XLS and SQL2POI commands presented in 2004. SQL2XLSX has some new parameters, but old SQL2XLS/SQL2POI parameters are consistent within both implementations; therefore, you could easily replace old command calls with this new one. In this version, when using templates, you can put the same file name into TOXLS and FROMXLS parameters, which allows you to easily create a multi-sheet XLSX. The new parameter SETLIBL(*LIBL) makes the Java thread run with the library list of the requester.

Just be aware of the following:

  • While an XLSX worksheet can hold up to 1,048,576 rows, the command version presented here (the starter kit) runs in XSSF mode; this means that, while the command runs, rows and columns are prepared and kept in memory, but the final XLSX is written to disk, and memory is freed only at the end of the process. This means that you can create your worksheet with a reasonable number of rows (say, like the old XLS limits). In a future article, you will find a SXSSF low-memory version that writes data in a small temporary buffer and flushes them while the buffer is full. Then you will be able to reach the 1,048,576 with a low memory usage without crashing the system.
  • The POI 3.9 contains a bug fixed in more POI recent releases: each extraction leaves a poi-sxssf-sheetnnnnnnnnnnn.xml file in the /tmp directory. I suggest you periodically clear them by running the command DEL PARM('poi-sxssf-sheet*.xml').
  • When explaining the differences between XLS and XLSX, I've mentioned that the new worksheet with macros differs from the old one. SQL2XLSX preserves existing macros contained in the template as do the old versions. But now, if you plan to create a worksheet with macros, both the template (that has macros inside) and the final file must have the XLSM extension, and you should download the additional poi-ooxml-schemas-1.0.jar file. Consider that this jar file has a size of 14 MB and is not needed for XLSX files.

 

Giuseppe Costagliola is a programmer in Turin, Italy. You can reach him atThis 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

$0.00 Raised:
$

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: