The jar package used
1. Sqljdbc4.jar
The jar package that connects the database (choose according to the different database, I use the SqlServer2008)
2.jxl.jar
Accessing the jar package for Excel
PackageXSL;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList;ImportJXL. Cell;ImportJXL. Sheet;ImportJXL. Workbook;Importjxl.read.biff.BiffException; Public classTest { Public Static voidMain (string[] args) {//defines a one-dimensional array that holds the data for each column of each row in the Excel tableobject[] obj =NULL; //Define a list collection, storing data for each rowarraylist<object[]> list =NewArraylist<object[]>(); String FilePath= "C:/users/0223000320/desktop/student.xls"; InputStream is=NULL; Workbook RWB=NULL; Try{ is=NewFileInputStream (FilePath);//defining text input streams}Catch(FileNotFoundException e) {e.printstacktrace (); } Try{RWB= Workbook.getworkbook (IS);//Open Workbook}Catch(biffexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } //get data for the Sheet1 area of an Excel tableSheet Sht = Rwb.getsheet ("Sheet1"); intcol = Sht.getcolumns ();//Get Excel Columns introw = Sht.getrows ();//Get Excel LinesCell C1=NULL; //first, the data is packed into a one-dimensional array, and then the array is added to the ArrayList for(inti=0; i < row; i++) {obj=NewObject[col]; for(intJ =0; J <col; J + +) {C1=Sht.getcell (j,i); //AddString contents =c1.getcontents (); SYSTEM.OUT.PRINTLN (contents); OBJ[J]=c1.getcontents (); } System.out.println ("------------"); List.add (obj); } } }
Java: Importing Excel data into a database