Java: Importing Excel data into a database

Source: Internet
Author: User

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

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.