Document directory
I am working on a project these two days and have a lot of knowledge about jtable.
Here is a method for exporting jtable data to an Excel table.
In the method called by the Java code in the jtable sample, the first parameter is the jtable name, and the second parameter is the Save path.
/** Export jtable to excel */Public void exporttable (jtable table, file) throws ioexception {tablemodel model = table. getModel (); bufferedwriter bwriter = new bufferedwriter (New filewriter (File); For (INT I = 0; I <model. getcolumncount (); I ++) {bwriter. write (model. getcolumnname (I); bwriter. write ("\ t");} bwriter. newline (); For (INT I = 0; I <model. getrowcount (); I ++) {for (Int J = 0; j <model. getcolumncount (); j ++) {bwriter. write (model. getvalueat (I, j ). tostring (); bwriter. write ("\ t");} bwriter. newline ();} bwriter. close (); system. out. println ("write out to:" + file );}
Add the following listener to the export button to save the Excel file to any location on the hard disk.
Private void jbutton1actionreceivmed (Java. AWT. event. actionevent EVT) {// todo add your handling code here: Export filedialog FD = new filedialog (this, "Save History", filedialog. save); FD. setlocation (400,250); FD. setvisible (true); string stringfile = FD. getdirectory () + FD. getFile () + ". xls "; try {orderdao odao = new orderdao (); odao. exporttable (jtable1, new file (stringfile);} catch (ioexception ex) {system. out. println (ex. getmessage (); Ex. printstacktrace ();}}
Select save path
Excel Sheet