, если вы хотите экспортировать строковое значение в CSV-файл (Excel поддерживает его), или вы можете использовать другую библиотеку, например Apache POI.
public static void exportJTable (JTable table, String fileName) throws Exception {
StringBuilder content = new StringBuilder("");
for (int i=0; i<table.getModel().getRowCount(); i++){
for (int j=0; j<table.getModel().getColumnCount(); j++) {
int col = table.convertColumnIndexToView(j);
String value = null ;
try {
value = (String) table.getModel().getValueAt(i, col);
}
catch (java.lang.ClassCastException e) {
}
if ( value == null)
value = "" ;
// CSV file
value.replaceAll(",", "");
content.append(value + ",");
}
content.append("\n");
}
writeToFile(content.toString(),fileName);
}
private static void writeToFile (String data, String fileName) throws FileNotFoundException, UnsupportedEncodingException {
File file = new File(fileName);
PrintWriter writer = new PrintWriter(file,"UTF-8");
writer.println(data);
writer.close();
}