Java jfilechooser upload an Excel file and read the contents of the file

Source: Internet
Author: User

First, describe

Either the upload file component in JSP or swing may upload an Excel file and read the records of the file by line, can provide the display of the data after reading the record, or construct the SQL statement for querying the data in the database.

For example, we upload a list of users, including the user name, gender and social Security number, we will be the real name of the user ID card four as a user account to query the database for the user information, upload the form as follows:



Second, the required tools

Java to call Excel and read the data in the Excel file, you must use Jxl.jar, so get the packet and then introduce it into the Java project.

The bundle is free: http://download.csdn.net/detail/tongyuehong/8321997

Third, the source code

Import Java.io.file;import Javax.swing.jfilechooser;import Javax.swing.filechooser.filenameextensionfilter;import Jxl. Sheet;import JXL.       workbook;/** * * @author Tong */public class countuserserver{public static File Choosefile;         private static JFileChooser Filechooser;                     Public Countuserserver () {filechooser = new JFileChooser (); Filter Excel files, only look for Excel files ending in xls, if you want to filter Word documents can also be written on doc filenameextensionfilter filter = new Filenameextensionfilter ("Tex        T Files "," xls ");                   Filechooser.setfilefilter (filter);            int returnvalue = Filechooser.showopendialog (null); Pop up a file select the box if (returnvalue = = filechooser.approve_option) {//Get the file path after the user selects the file Choosefile = Filechoo                Ser.getselectedfile ();         Initialize Excel workbook Workbook workbook=null based on file path;         try {workBook = Workbook.getworkbook (choosefile); } catch (Exception e) {e.printstacktRace ();            }//Gets the first sheet in the worksheet Sheet sheet=workbook.getsheet (0);            Gets the number of rows for the worksheet for the following loop using int rowsize=sheet.getrows ();           System.out.println ("Number of rows:" +rowsize);          String account= "";                             for (int i=1;i<rowsize;i++) {//Get Last Name field data, column A, line I, note that the rows and columns in Excel are obtained from 0, a column is 0 columns              String Name=sheet.getcell (0,i). getcontents ();                          Remove all spaces Name=name.replaceall ("", "");               Get the ID Number field data, column C, line I, String Idcard=sheet.getcell (2,i). getcontents ();              Idcard=idcard.replaceall ("," "");              String last4idcard= "";                Last4idcard=idcard.substring (14);  if (i== (rowSize-1)) {account + = "'" +name+last4idcard+ "'";                If it is the last line, do not add a comma at the end of the}else{account + = "'" +name+last4idcard+ "',"; }}//construct a SQL query statement that queries the user account'sInformation System.out.println ("SELECT * from user where account in (" +account+ ")");              }} public static void Main (string[] args) {new Countuserserver (); }  }

Run result output: SELECT * from user where account in (' 37,224 ', ' Lee 42,814 ', ' Wang Xiao 54,173 ')

Iii. Summary

1, Java to call Excel and read the data in the Excel file, you must use Jxl.jar, so first get the packet and then introduced into the Java project;

2. Rows and columns in Excel are obtained starting from 0, and column A is 0 columns



Java jfilechooser upload an Excel file and read the contents of the file

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.