2008 East
DIAMOND SPONSOR:
Data Direct
Frontiers in Data Access: The Coming Wave in Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
Intel
Virtualization – Path to Predictive Enterprise
Green Hills
IT Security in a Hostile World
JBoss / freedom oss
Practical SOA Approach
GOLD SPONSORS:
Software AG
The Art & Science of SOA: How Governance Enables Adoption
PlateSpin
Effective Planning for Virtual Infrastructure Growth
Fujitsu
Automated Business Process Discovery & Virtualization Service
Ceedo
Workspace Virtualization
Click For 2007 West
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


Coding Business Logic in Excel
Morphing an Excel spreadsheet into Java business logic

How many times have you coded a financial, engineering, or pricing calculator and used an Excel spreadsheet as a reference? What if you could take that Excel spreadsheet and make that the business logic for a Java application? The e.SpreadSheet API from ReportingEngines makes that dream a reality.

Formula One e.SpreadSheet Engine is a complete API for reading and saving spreadsheets. It can load a complete spreadsheet into memory, and do data changes, calculations, even charting, using nothing more than the formulas coded into the spreadsheet!

Even more impressive is its ability to create spreadsheets from scratch. With this functionality, reports can be delivered the same way they were received - in a spreadsheet.

In this article, I'll take you through a simple example of using a spreadsheet for common business logic. I'll use the JSP Tag Library APIs to make the code easy to follow along. But don't be fooled. The full API lets you do these calculations in EJBs, Swing Applications, or any other code that needs spreadsheet functionality.

Requirements

To use the code in this article, you'll need to download and install the e.SpreadSheet engine from www.reportingengines.com, and you'll need an application server capable of running JSP pages. If you don't have one readily available, you can download Apache Tomcat from http://jakarta.apache.org.

You'll also need a copy of Excel or OpenOffice to create the documents. Alternatively, you can download ReportingEngines' companion product e.SpreadSheet Designer. Designer is a GUI-based on the e.SpreadSheet engine. Its features are like Excel's and it lets you graphically design a spreadsheet from a database, XML source, or text file.

After you install the necessary software, you'll need to extract the f1taginstall.war file and deploy it in Tomcat. This file contains everything a Web app needs to use the e.SpreadSheet APIs, so we'll use it as a shortcut for learning. You can find the file in the install directory under "eSS11/jars."

Creating the Spreadsheet

In this example, we're going to leverage a spreadsheet that does a useful - but not widely known - calculation called a "weighted average." The idea behind a weighted average is that a second number (such as the quantity of items) is used to give more or less importance to the numbers being averaged.

This function is commonly used to understand the true average price per product sold. For example, a grocery store may sell thousands of packs of bubblegum a week, but less than a hundred boxes of crackers. Using a normal average, the price of the crackers would raise the "average price per product," even though it sells nowhere near as well as the gum. With a weighted average, the number of items sold is properly figured into the average so that the store is aware of its real profit margins.

A weighted average can be easily calculated in Excel by using the formula, "SUMPRODUCT(RANGE1,RANGE2)/SUM(RANGE2)"; where RANGE2 is the quantity or "weighting" of the numbers.

Let's create a spreadsheet to test this.

Open Excel or one of the other spreadsheet programs mentioned above. Place the following titles in row 1 of columns 'A,' 'B,' and 'C':


"Item," "Price," and "Units."

We'll use the first column to identify the product, the second for the product's price, and the third for the number of units sold. Feel free to enter data in the three rows. When you're done, the rows should look something like those in Figure 1.

Now, let's create a cell to display a regular average and a weighted average. Click on cell "D2" and type "=AVERAGE(B2:B5000)" in the calculator bar. If you used the data in Figure 1, "D2" should now show an average of $14.12. Now click on cell "D3" and type "=SUMPRODUCT(B2:B5000,C2:C5000)/SUM(C2:C5000)." Again, if you used the data in Figure 1, you should have a weighted average of $8.28. That's quite a difference!

Save the spreadsheet in the web application folder with the name "average.xls", and we will look at loading it into a JSP page using the e.SpreadSheet API.

Displaying the Data

With our spreadsheet safely saved as part of our Web app, we can create a JSP file to display its contents. Create a new JSP file in the Web app directory, and call it "average.jsp." Open it in your favorite text editor or IDE.

Put the following line of code at the top of your JSP file:


<%@ taglib uri="f1taglib" prefix="f1"%>

This line references the e.Spread-Sheet tag library, allowing our JSP file to access its broad array of functions.

Next, we'll load the Excel file from disk with the following tag:


<f1:LoadBook book="Average" source="average.xls"/>

This tag tells the e.SpreadSheet engine we want to load the "average.xls" file, and that we want to let other tags reference it by the name "Average." This tag will keep the spreadsheet in memory until the session expires or the "UnloadBook" tag is called. By default, the engine will ignore the "LoadBook" tag if a spreadsheet by the same name has already been loaded, so there's no need to worry about losing any modifications you've made. This behavior can be overridden with the "reload" attribute.

Now that the prep work is done, we can finally display the data. We can use the "InsertRange" tag to populate an HTML table with data from the spreadsheet automatically. All we need to explain to the tag is the range and formatting. Try adding the following tag to the JSP page:


<f1:InsertRange book="Average" range="A1:C6" table="true" spreadsheetformat="true"/>

That's all there is to it! If you save your page and access it in a Web browser, you should see a table with the data from the "average.xls" spreadsheet. Basically, we've told the spreadsheet engine to pull cells A1 through C6 from the spreadsheet named "Average" and display it in a table. The "spreadsheetformat" attribute was also used to retain the cell formatting from the original spreadsheet. But what about the averages?

As one might expect, the "InsertRange" tag has a cousin called "InsertCell". Instead of generating the complete table with formatting, "InsertCell" merely pulls the data for a single cell and plops it into the sheet. The following code will display the average and weighted average for the data:


Average Price:
<f1:InsertCell book="Average" cell="D2"/>
<br>
Weighted Average Price:
<f1:InsertCell book="Average" cell="D3"/>
About Jerason Banes
Jerason Banes is a developer who was one of the original promoters of the Java language. "I was Java, before Java was coooooolllll," he says. In the past he was a lead programmer and chief architect for a million hit a day Web site and was an early adopter of servlets, JSP, and EJB. Before Java, he gained a great deal of experience in C/C++, VB, and COBOL and has contributed to a large number of open-source projects.

LATEST JAVA STORIES & POSTS
Continuent has announced support and enhancements to MySQL Server 5.1.30 GA release, the 5.1 production version of the open source database. MySQL 5.1.30 is recommended for use on production systems by the MySQL build team at Sun Microsystems. Continuent Tungsten provides advance...
As a software journalist, there are times when certain vendors will shut the door on reporting opportunities that might represent too much of an "inside view" of their technology or their organization. I've been to more developer events than I can remember where I've been handed ...
Active Endpoints has announced the general availability of ActiveVOS 6.0.2, in response to ever increasing demands for improved process performance and efficiencies. ActiveVOS is an all-in-one, 100% standards-based orchestration and business process management system (BPM) that p...
Just because the web has been open so far doesn't mean that it will stay that way. Flash and Silverlight, arguably the two market-leading technology toolkits for rich media applications are not open. Make no mistake - Microsoft and Adobe aim to have their proprietary plug-ins, ak...
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it’s still done. At this year's JavaOne, one of the keynote JavaFX demos bombed because the network was slow, something that would be forgivable had the en...
Over the course of the past few decades, the consumer media industry has evolved from a slow-moving oligopoly dominated by a handful of vertically integrated networks to a highly fragmented and competitive marketplace of content creation, publication, and distribution players. Th...
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON FEATURED WHITEPAPERS

SPONSORED BY INFRAGISTICS
In every field of design one of the first things students do is learn from the work of others. They ...
There are many forces that influence technological evolution. After a decade of building enterprise ...
2008 is going to be an important year for Rich Internet Applications. Most organizations are deliver...
The OpenAjax Alliance is developing an Ajax industry wishlist for future browsers, using a dedicated...
Infragistics announced the availability of two Community Technology Preview (CTP) User Interface (UI...
The YUI development team has released version 2.5.2; you can download the new release from SourceFor...
ADS BY GOOGLE