Java JTable table Get storage path, file name, export Excel table

Source: Internet
Author: User

In the metering pump host computer software, you need to transfer data from the next computer, stored in the MySQL database, displayed in the Java host computer interface, and can be exported to the computer designated location.

Select the storage path and file name:

    //menu for working with saved files      Public voidexportexcelfile (String sql,string []paras) {string Absolutepath=""; //setting the style of the dialog box      Try{Uimanager.setlookandfeel (Uimanager.getsystemlookandfeelclassname ()); } Catch(Exception E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } jfilechooser JFileChooser=NewJFileChooser (); //jfilechooser.setmultiselectionenabled (true);//If you want to choose more, you can set this sentence//Set the default save file name, note that if this is set, you will not be able to get the file storage path after the user changed, because the Jfilechooser.getcurrentdirectory ()//is the path to the default file you set//Jfilechooser.setselectedfile (New//file ("default name");       intresult = Jfilechooser.showsavedialog (NULL); Switch(Result) { CaseJFileChooser.APPROVE_OPTION:System.out.println ("11"); //This approach is to save the user input as a saved file name as an Excel table. xlsAbsolutepath = jfilechooser.getcurrentdirectory () + File.separator +jfilechooser.getselectedfile (). GetName ()+ ". xls";           //.xls can also specify other formats for output Excel tables SqlHelper SqlHelper=NewSqlHelper (); ResultSet RS=Sqlhelper.query (SQL, paras); File File=NewFile (Absolutepath); Try {                This.writeexcel (rs, file);//Export Data method}Catch(SQLException E1) {e1.printstacktrace (); } joptionpane.showmessagedialog (NULL, "Export Data successfully!" ");  Break;  CaseJFileChooser.CANCEL_OPTION:JOptionPane.showMessageDialog (NULL, "Cancel Export!! ");  Break;  Casejfilechooser.error_option://System.out.println ("Wrong!!  ");            Break; }                }
To export a data method:
//Need a result set RS and file files
Public voidWriteexcel (ResultSet rs,file File)throwssqlexception{Writableworkbook Workbook=NULL; //The ResultSetMetaData can be obtained from the RS object to get the table column nameResultSetMetaData rsmt=Rs.getmetadata (); Try{ //first, use the factory method of the Workbook class to create a writable workbook (Workbook) objectWorkbook =workbook.createworkbook (file); } Catch(IOException e) {e.printstacktrace (); } if(workbook!=NULL) {Writablesheet Wsheet= Workbook.createsheet ("Sheet1", 0); Try { //get the column name of the table for(intI=0;i<rsmt.getcolumncount (); i++) {Label label_column1=NewLabel (i, 0, Rsmt.getcolumnname (i+1)); Wsheet.addcell (LABEL_COLUMN1); } } Catch(rowsexceededexception E1) {e1.printstacktrace (); } Catch(WriteException E1) {e1.printstacktrace (); } //get the contents of a table intI=1; while(Rs.next ()) { for(intJ=0;j<rsmt.getcolumncount (); j + +) {Label Label1=NewLabel (J, I, Rs.getstring (j+1)); Try{Wsheet.addcell (Label1); } Catch(rowsexceededexception e) {e.printstacktrace (); } Catch(WriteException e) {e.printstacktrace (); }} I++; } Try { //writing files from memoryWorkbook.write (); //close the resource and free up memoryWorkbook.close (); } Catch(IOException e) {e.printstacktrace (); } Catch(WriteException e) {e.printstacktrace (); } } }

Java JTable table Get storage path, file name, export Excel table

Related Article

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.