1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class ExcelExporter {
public void fillData(JTable table, File file) {
try {
WritableWorkbook workbook1 = Workbook.createWorkbook(file);
WritableSheet sheet1 = workbook1.createSheet("Page 1", 0);
TableModel model = table.getModel();
for (int i = 0; i < model.getColumnCount(); i++) {
Label column = new Label(i, 0, model.getColumnName(i));
sheet1.addCell(column);
}
int j = 0;
for (int i = 0; i < model.getRowCount(); i++) {
for (j = 0; j < model.getColumnCount(); j++) {
Label row = new Label(j, i + 1,model.getValueAt(i,j).toString());
sheet1.addCell(row);
}
}
workbook1.write();
workbook1.close();
JOptionPane.showMessageDialog(null, "Data saved at " +file+" successfully", "Message", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
} |
Partager