New options for Java reporting

SpreadsheetML and WordprocessingML ease the creation of Word and Excel documents

1 2 Page 2
Page 2 of 2

The method write() gets the root element for the document, inserts the appropriate processing instructions, and writes the XML document to a file. main() just initiates a new JavaExcel object and calls write() with a filename. getElements() does most of the work for this class. It creates the workbook, worksheet, table, rows, cells, and data elements. Then it adds each element to its appropriate parent. The methods getWorkbook(), getWorksheet(), getTable(), getRow(), getCell(), and getData() prepare an appropriate element and return it.

This example just populates two cells. Yet, it could do much more. It could add formulas to cells, populate cells from a database, change cell formatting, and so on. An improvement to this class would use dom4j's prefix and namespace functionality to avoid manually adding the prefix to the element name and its attributes. One more note: This example does not use the schemas provided by Microsoft. Using a schema makes the process of reading and writing XML files robust and safer. I didn't use the schemas for simplicity's sake. For more information on schemas and its applications see Resources.

Conclusion

Reading and writing Word and Excel documents with WordprocessingML and SpreadsheetML from a Java program is easy. If you use the schemas for these formats, you can easily detect errors in the documents you are creating and reading. Microsoft also provides a similar format for Visio documents called DatadiagramML. With this format, you can create and manipulate Visio documents with Java programs in the same way you create and manipulate Excel documents with SpreadsheetML.

There are numerous applications of WordprocessingML and SpreadsheetML in Java programs. The most obvious application is to create complex and professional reports in Word or Excel format. Another application is to read Word and Excel documents to populate databases. On the server side, these formats are also quite useful. For example, Web applications where users can upload Word documents for publication (resumes, publications, etc.) are easier to write with WordprocessingML. A WordprocessingML document is easier to parse than a regular Word file. WordprocessingML and SpreadsheetML are two exciting new developments for Java developers.

Hugo Troche is currently a software engineer living in Auburn, Alabama. He received his undergraduate degree in computer science from Auburn University (Magna Cum Laude) in 2002 and a master's in software engineering from Auburn in 2004. He is a software consultant and contractor specializing in Web services and n-tier business systems. He has worked in more than 14 n-tier J2EE business systems projects and in six Web services projects. Currently, he is working on a project to centralize reporting for a large human resources application. He has five years of experience developing Java applications. His areas of interests are design patterns, aspect-oriented design and patterns, software architecture, software process for small groups, software size estimation techniques, and software tools.

Learn more about this topic

1 2 Page 2
Page 2 of 2