jtable
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jitender_kumar
Posted On:   Wednesday, April 25, 2001 12:10 AM

how can i export data from jtable to excel file?

Re: jtable

Posted By:   Sandip_Chitale  
Posted On:   Wednesday, April 25, 2001 09:35 AM

Cycle though the rows (table.getRowCount()) and columns (table.public int getColumnCount()) using nested for loops. Get the value of the cells using


table.getValueAt(i, j)


Convert the retuned object to String and add to a stringbuffer separating with "," between columns and File.separator between rows. Put the StringBuffer content into a


java.awt.datatransfer.StringSelection


Insert the StringSelection into System
Clipboard -


Toolkit.getDefaultToolkit().getSystemClipboard().setContent(stringSelection);


void doCopy() {
StringBuffer data = new StringBuffer();
int rowCount = getRowCount();
int colCount = getColumnCount();
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
if (j > 0) {
data.append(" ");
}
Object o = getValueAt(i, j);
data.append(o == null ? "" : o.toString());
}
data.append("
");
}
if (data.length() > 0) {
java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(data.toString()), null);
}
}


Assumes that this is method in a JTable subclass.
About | Sitemap | Contact