[Java] I/O operation to export jtable data to an Excel table

Source: Internet
Author: User
Document directory
  • Jtable example
  • Java code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.