130
|
Chapter 3, Tables and Trees
#25 Export Table Data to an Excel Spreadsheet
HACK
The createSampleTable( ) method is something you could rewrite to insert
your own types and values easily. In fact, because it returns the name of the
table you’ve created, you could create many different tables in your data-
base and test out how the model handles them. Or, use a loop to create lots
of rows and see how long it takes to load them.
At any rate, when run, the
TestJDBCTable produces a JFrame with the data-
base table’s contents, as seen in Figure 3-9.
H A C K
#25
Export Table Data to an Excel Spreadsheet Hack #25
I don’t want an entire spreadsheet API, I just want to get a table of values into
Excel.
Most corporate intranet applications require interfacing with standard office
software, which usually means Microsoft Word and Excel. Interfacing with
Microsoft products can be tricky business. Whole suites of products have
been created just to address this issue. One of the most commonly requested
features is generating a report from the data in a
JTable. You could use a
library like Poi (http://jakarta.apache.org/poi/) to read and write Excel files
natively, but most of the time that’s overkill—you probably don’t need to
support Excel formulas or complicated formatting. All most users really
want to do is dump tabular data into a file that will open in Excel with a
double-click. And with a little bit of cleverness, you can do just that.
statement.close( );
return "EMPLOYEES";
}
}
Figure 3-9. JTable populated from a database
Example 3-13. Testing the JDBC-based table (continued)

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.