String[][] data = {
{ "Housewares", "$1275.00" },
{ "Pets", "$125.00" },
{ "Electronics", "$2533.00" },
{ "Mensware", "$497.00" }
};
String[] headers = { "Department", "Daily Revenue" };
JFrame frame = new JFrame("JTable to Excel Hack");
DefaultTableModel model = new DefaultTableModel(data,headers);
final JTable table = new JTable(model);
JScrollPane scroll = new JScrollPane(table);
JButton export = new JButton("Export");
export.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
ExcelExporter exp = new ExcelExporter();
exp.exportTable(table, new File("results.xls"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
});
frame.getContentPane().add("Center",scroll);
frame.getContentPane().add("South",export);
frame.pack();
frame.setVisible(true);