Java:將Excel資料匯入到資料庫

來源:互聯網
上載者:User

標籤: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資料匯入到資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.