標籤:string rgs obj 訪問 new getc logs cell cep
所用Jar包
1. sqljdbc4.jar
串連資料庫的Jar包(根據資料庫的不同進行選擇,我用的SqlServer2008)
2.Jxl.jar
訪問Excel的Jar包
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) { //定義一維數組,存放Excel表裡的每一行的各個列的資料 Object[] obj = null; //定義List集合,存放每一行的資料 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地區的資料 Sheet sht = rwb.getSheet("Sheet1"); int col = sht.getColumns(); //獲得Excel列 int row = sht.getRows(); //獲得Excel行 Cell c1 = null; //先將資料按行裝入一個一維數組中, 然後將數組逐個加入到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); } } }
Java:將Excel資料匯入到資料庫