以及如何把數(shù)據(jù)庫(kù)的數(shù)據(jù)導(dǎo)出到EXCEL.
1) EXCEL的表格導(dǎo)入數(shù)據(jù)庫(kù)
假設(shè)有一個(gè)EXCEL,有兩個(gè)SHEET,記得第一個(gè)sheet序號(hào)是0,第2個(gè)sheet的序號(hào)是1.
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
主要方法為:
InputStream fs = null;
Workbook rbw = null;
try{
try {
fs=new FileInputStream(file);
rbw=Workbook.getWorkbook(fs);
System.out.println("加載本地excel文件成功!");
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (BiffException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
Sheet book=null;
Sheet bookSingle=null;
try{
book=rbw.getSheet(0);//第一個(gè)sheet
}catch (Exception e){
e.printStackTrace();
}
System.out.println("取得excel文件sheet成功!");
rsRows=book.getRows();//獲取第一個(gè)sheet表的總行數(shù)
for(int i=1;i<rsRows;i++){
//獲取excel文件中數(shù)據(jù)
//第i行第0列
Cell cell0 = book.getCell(0, i);
String usercode=cell0.getContents();
//第i行第1列
Cell cell1 = book.getCell(1, i);
String org_name=cell1.getContents();
}
注意這里的book.getCell(0,i),是從第I行第0列這樣寫的,就是反過(guò)來(lái)
2) 數(shù)據(jù)庫(kù)到EXCEL
File file=new File("xxx.xls");
WritableWorkbook book = Workbook.createWorkbook(new File(fileName));
// 生成名為“SheetOne”的工作表,參數(shù)0表示這是第一個(gè)sheet
WritableSheet sheet = book.createSheet("SheetOne", 0);
// 在Label對(duì)象的構(gòu)造子中指名單元格位置是第一列第一行(0,0),以及單元格內(nèi)容
Label chs_name_LbTitle=new Label(0, 0, "中文名稱");
............................................
sheet.addCell(chs_name_LbTitle);
Label ent_name_cCell=new Label(0, 1, 要寫入的實(shí)際數(shù)據(jù));
sheet.addCell(ent_name_cCell);
book.write();
book.close();
標(biāo)簽:java培訓(xùn)課程 java軟件工程師培訓(xùn) java就業(yè)培訓(xùn)教程 java培訓(xùn)機(jī)構(gòu) java培訓(xùn)班 java軟件培訓(xùn)