O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required