例如,文件可以使用 .xsl 或 .xlsx
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableModel;
class ExcelExporterTodayFxDeals {
ExcelExporterTodayFxDeals() {
}
public void exportTable(JTable table, File file) throws IOException {
TableModel model = table.getModel();
FileWriter out = new FileWriter(file);
String diakritika = "áäÁÄčČďĎéěÉĚíÍľĺĽĹňŇóôöőÓÔÖŐŕřŔŘšŠťŤúüůÚÜŮýÝžŽ";
String diakritikaBez = "aaAAcCdDeeEEiIllLLnNooooOOOOrrRRsStTuuuUUUyYzZ";
String novyPreklad = "";
String groupExport = "";
Integer remDia = 1;
for (int i = 0; i < (model.getColumnCount()); i++) {//* disable export from TableHeaders
groupExport = String.valueOf(model.getColumnName(i));
novyPreklad = groupExport;
if (groupExport != (null)) {
remDia = 1;
for (remDia = 0; remDia < (diakritika.length());) {
novyPreklad = diakritika.substring(1, 2);
novyPreklad = (diakritika.substring(remDia, (remDia + 1))).toString();
groupExport = groupExport.replace((diakritika.substring(remDia, (remDia + 1))),
diakritikaBez.substring(remDia, (remDia + 1)));
remDia++;
}
} else {
groupExport = "";
}
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++) {//16
if (model.getValueAt(i, j) == null) {
out.write("null" + "\t");
} else {
groupExport = String.valueOf(model.getValueAt(i, j));
novyPreklad = groupExport;
if (groupExport != (null)) {
remDia = 1;
if (j == 0) {//Replace non_ACSII chars, not required in most cases
for (remDia = 0; remDia < (diakritika.length());) {
novyPreklad = diakritika.substring(1, 2);
novyPreklad = (diakritika.substring(remDia, (remDia + 1))).toString();
groupExport = groupExport.replace((diakritika.substring(remDia, (remDia + 1))),
diakritikaBez.substring(remDia, (remDia + 1)));
remDia++;
}
out.write(34);
out.write(String.valueOf(groupExport));
out.write(34);
out.write("\t");
} else if (j == 5) {//Number Instance
if (((model.getValueAt(i, j)) != null) && ((model.getValueAt(i, j)) instanceof Number)) {
groupExport = String.format("%.2f", (Number) (model.getValueAt(i, j)));
}
groupExport = groupExport.replace(".", ",");
out.write(34);
out.write(String.valueOf(groupExport));
out.write(34);
out.write("\t");
} else if (j == 10) {//Date instance
groupExport = groupExport.replace("-", "/");
out.write(34);
out.write(String.valueOf(groupExport));
out.write(34);
out.write("\t");
}
} else {
groupExport = "";
out.write(34);
out.write(String.valueOf(groupExport));
out.write(34);
out.write("\t");
}
}
}
out.write("\n");
}
out.close();
}
}