I\'ve recently started working with java and I\'m facing some issues with the apache poi library when I need to create an excel file starting from a jTable.
I\'ve re
Please read the tutorial about JTable, Creating a Table Model and implemented data types, JTable knows those Column Classes, and returns proper value for output to POI; for most of types in the MS Excel:
you have to loop inside cells in current row, too
have to format output for various data types (Date, Double, String)
then you can focus on creating formula(s) and coloring cell(s)
Code to loop through TableModel can generate MS Excel File, too, with standard windows delimiters:
public class ExcelCustomerReport {
public ExcelCustomerReport() {
}
public void exportTable(JTable table, File file) throws IOException {
TableModel model = table.getModel();
FileWriter out = new FileWriter(file);
String groupExport = "";
for (int i = 0; i < (model.getColumnCount()); i++) {//* disable export from TableHeaders
groupExport = String.valueOf(model.getColumnName(i));
out.write(String.valueOf(groupExport) + "\t");
}
out.write("\n");
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < (model.getColumnCount()); j++) {
if (model.getValueAt(i, j) == null) {
out.write("null" + "\t");
} else {
groupExport = String.valueOf(model.getValueAt(i, j));
out.write(String.valueOf(groupExport) + "\t");
}
}
out.write("\n");
}
out.close();
}
}