
public static StringBuffer convert(JTable table) {
StringBuffer sbf = new StringBuffer();
// Check to ensure we have selected only a contiguous block of cells
int numcols = table.getColumnCount();
int numrows = table.getSelectedRowCount();
int[] rowsselected = table.getSelectedRows();
if (numrows <= 0) {
JOptionPane.showMessageDialog(null, "Invalid Copy Selection. Please select atleast one Row.", "Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
for (int i = 0; i < numcols; i++) {
sbf.append(table.getColumnName(i));
if (i < numcols - 1) {
sbf.append(",");
}
}
sbf.append("\n");
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
Object obj = table.getValueAt(rowsselected[i], j);
if (obj instanceof String) {
sbf.append("\"");
sbf.append(obj);
sbf.append("\"");
} else if (obj instanceof XDouble) {
sbf.append("\"");
sbf.append(table.getValueAt(rowsselected[i], j));
sbf.append("\"");
} else if (obj instanceof java.sql.Timestamp) {
XDate wdt = new XDate((java.sql.Timestamp)
obj);
sbf.append(wdt);
} else {
sbf.append(table.getValueAt(rowsselected[i], j));
}
if (j < numcols - 1) {
sbf.append(",");
}
}
sbf.append("\n");
}
return sbf;
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.