本文實(shí)例為大家分享了Java將Excel數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫的具體代碼,供大家參考,具體內(nèi)容如下
所用Jar包
1. sqljdbc4.jar
連接數(shù)據(jù)庫的Jar包(根據(jù)數(shù)據(jù)庫的不同進(jìn)行選擇,我用的SqlServer2008)
2.Jxl.jar
訪問Excel的Jar包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package xsl; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class Test { public static void main(String[] args) { //定義一維數(shù)組,存放Excel表里的每一行的各個(gè)列的數(shù)據(jù) Object[] obj = null ; //定義List集合,存放每一行的數(shù)據(jù) ArrayList<Object[]> list = new ArrayList<Object[]>(); String filePath = "C:/Users/0223000320/Desktop/student.xls" ; InputStream is = null ; Workbook rwb = null ; try { is = new FileInputStream(filePath); //定義文本輸入流 } catch (FileNotFoundException e) { e.printStackTrace(); } try { rwb = Workbook.getWorkbook(is); //打開Workbook } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //獲取Excel表的Sheet1區(qū)域的數(shù)據(jù) Sheet sht = rwb.getSheet( "Sheet1" ); int col = sht.getColumns(); //獲得Excel列 int row = sht.getRows(); //獲得Excel行 Cell c1 = null ; //先將數(shù)據(jù)按行裝入一個(gè)一維數(shù)組中, 然后將數(shù)組逐個(gè)加入到ArrayList for ( int i= 0 ; i < row; i++){ obj = new Object[col]; for ( int j = 0 ;j <col; j++){ c1 = sht.getCell(j,i); //add String contents = c1.getContents(); System.out.println(contents); obj[j] = c1.getContents(); } System.out.println( "------------" ); list.add(obj); } } } |
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/Donnnnnn/p/7742368.html