1.ecxcel導入導出的實現
1.創建Book類,並編寫set方法和get方法

package com.bean; public class Book { private int id; private String name; private String type; // public int a; public String getType() { System.out.println("調用了類型方法"); return type; } public void setType(String type) { this.type = type; } // public void test (String name,int a){ // System.out.println("調用了多參數的方法"); // } public String getName() { System.out.println("調用了名稱方法"); return name; } public void setName(String name) { this.name = name; } public int getId() { System.out.println("調用了序號方法"); return id; } public void setId(int id) { this.id = id; } }
2.創建ExcelBook類實現導入導出

package com.main; import java.io.File; import java.util.ArrayList; import com.bean.Book; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class ExcelBook { public static void main(String[] args) { ExcelBook excelBook = new ExcelBook(); //創建集合 // ArrayList<Book> arrayList = new ArrayList<Book>(); // Book book = new Book(); // book.setId(1); // book.setName("Java語言"); // book.setType("面向對象"); // Book book1 = new Book(); // book1.setId(2); // book1.setName("西游記"); // book1.setType("故事"); // Book book2 = new Book(); // book2.setId(3); // book2.setName("高數"); // book2.setType("難"); // arrayList.add(book); // arrayList.add(book1); // arrayList.add(book2); // excelBook.excelOut(arrayList); ArrayList<Book> ar1 = excelBook.excelIn(); for(Book bo2 : ar1){ System.out.println(bo2.getId()+" "+bo2.getName()+" "+bo2.getType()); } } public void excelOut(ArrayList<Book>arrayList){ WritableWorkbook bookk = null;//Excle對象 try { //創建excel對象 bookk = Workbook.createWorkbook(new File("D:/Sourcecode/Java/fanshe/book.xls")); //通過excel對象創建一個選項卡 WritableSheet sheet = bookk.createSheet("sheet1", 0); //創建一個單元格對象參數為 列 行 值 for(int i = 0; i < arrayList.size();i++){ Book bo = arrayList.get(i); Label la1 = new Label(0, i,String.valueOf(bo.getId())); Label la2 = new Label(1, i,bo.getName()); Label la3 = new Label(2, i, bo.getType()); sheet.addCell(la1);//放入選項卡中 sheet.addCell(la2); sheet.addCell(la3); } //寫入目標路徑 bookk.write(); } catch (Exception e) { e.printStackTrace(); }finally{ try { bookk.close(); } catch (Exception e) { e.printStackTrace(); } } } public ArrayList<Book> excelIn(){ ArrayList<Book> ar = new ArrayList<Book>();//返回集合 Workbook book = null; try { //獲取到excle對象 book = Workbook.getWorkbook(new File("D:/Sourcecode/Java/fanshe/book.xls"));//文件路徑 Sheet sheet = book.getSheet(0);//獲取第一個選項卡對象(第零列) for(int i = 0; i < sheet.getRows();i++){//向文件中讀入值,getRows()獲取數據中有多少條數據 Book bo = new Book(); Cell cell = sheet.getCell(0,i);//cell對象為單元格對象,取出數據i行0列 bo.setId(Integer.valueOf(cell.getContents()));//getContents獲取單元格對象的值 bo.setName(sheet.getCell(1,i).getContents()); bo.setType(sheet.getCell(2,0).getContents()); ar.add(bo); } } catch (Exception e) { e.printStackTrace(); }finally{ book.close(); } return ar; } }
運行結果:導出時,在指定路徑出現下圖內容
導出時結果如下:
調用了序號方法 調用了名稱方法 調用了類型方法 1 Java語言 面向對象 調用了序號方法 調用了名稱方法 調用了類型方法 2 西游記 面向對象 調用了序號方法 調用了名稱方法 調用了類型方法 3 高數 面向對象
此方法雖然可以實現excel的導入導出,但因導入導出的數據信息比較少,但如果數據信息比較多,此方法實現起來非常麻煩,所以應該利用java反射機制實現。
2.利用反射實現通用的excel導入導出
如果一個項目中存在多種信息的導入導出,為了簡化代碼,就需要用反射實現通用的excel導入導出
實例代碼如下:
1.創建一個 Book類,並編寫set和get方法
1 package com.bean; 2 3 public class Book { 4 private int id; 5 private String name; 6 private String type; 7 // public int a; 8 9 public String getType() { 10 System.out.println("調用了類型方法"); 11 return type; 12 } 13 14 public void setType(String type) { 15 this.type = type; 16 } 17 // public void test (String name,int a){ 18 // System.out.println("調用了多參數的方法"); 19 // } 20 21 public String getName() { 22 System.out.println("調用了名稱方法"); 23 return name; 24 } 25 26 public void setName(String name) { 27 this.name = name; 28 } 29 30 public int getId() { 31 System.out.println("調用了序號方法"); 32 return id; 33 } 34 35 public void setId(int id) { 36 this.id = id; 37 } 38 39 }
2.創建一個ExcelUtil類,實現導入導出

1 package com.util; 2 3 import java.io.File; 4 import java.lang.reflect.Field; 5 import java.util.ArrayList; 6 7 import com.bean.Book; 8 9 import jxl.Sheet; 10 import jxl.Workbook; 11 import jxl.write.Label; 12 import jxl.write.WritableSheet; 13 import jxl.write.WritableWorkbook; 14 15 public class ExcelUtil { 16 //第一個參數為要導出的數據的實體類的對象,實例類不確定,第二個參數為導入路徑 17 public static void excleOut(ArrayList ar,String str){ 18 WritableWorkbook book = null;//編寫WritableWorkbook對象,該對象代表了excel對象 19 try { 20 book = Workbook.createWorkbook(new File(str));//創建文件路徑str 21 WritableSheet sheet = book.createSheet("sheet",0);//獲取sheet對象 22 //對集合進行遍歷 23 for(int i = 0; i < ar.size();i++){ 24 Object ob = ar.get(i);//集合中的對象不確定,用Object代替 25 //利用反射機制 26 Class cl = ob.getClass();//運行時獲得傳遞過來的對象 27 Field[] fi = cl.getDeclaredFields();//獲取所有屬性的對象,用來獲取屬性 28 for(int j = 0; j <fi.length;j++){//將獲得的對象遍歷出來 29 fi[j].setAccessible(true);//啟用訪問權限 30 //獲取值 列(j),行(i),值fi[j]為字符串方式 31 Label la = new Label(j, i, String.valueOf(fi[j].get(ob))); 32 sheet.addCell(la);//將數據寫入sheet對象中 33 } 34 } 35 book.write(); 36 } catch (Exception e) { 37 e.printStackTrace(); 38 }finally{ 39 try { 40 book.close(); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 } 45 } 46 //第一個參數為要導入的數據的實體類的對象,第二個參數為導入路徑 47 public static ArrayList excleIn(Class cl,String str){ 48 ArrayList ar =new ArrayList();//創建空的集合用於存儲數據 49 Workbook book = null;//聲明一個workbook對象 50 try { 51 book =Workbook.getWorkbook(new File(str));//獲取到類對象 52 Sheet sheet = book.getSheet(0);//獲取sheet對象 53 Field []fi = cl.getDeclaredFields();//獲取類屬性信息 54 for(int i = 0;i < sheet.getRows();i++){//對讀取進來的excle數據進行遍歷 55 Object ob = cl.newInstance();//創建實例化對象,創建新的存儲數據的對象,用Object對象代替 56 //將所有的屬性封裝到對象中 57 for(int j = 0;j <fi.length;j++){//將屬性進行循環 58 fi[i].setAccessible(true);//啟用屬性訪問權限 59 String con = sheet.getCell(j,i).getContents();//從excel中讀取數據 60 //判斷屬性的類型 61 if(fi[j].getType().toString().equals("Class java.lang.String")){ 62 //將讀入的數據(在con中)封裝到對象(ob)的屬性(fi[i])中 63 fi[j].set(ob,con);//ob.id=con正常方法 64 }else if(fi[j].getType().toString().equals("int")){ 65 fi[j].setInt(ob,Integer.valueOf(con));//將信息封裝到對象中 66 }else if(fi[j].getType().toString().equals("Integer")){ 67 fi[j].setInt(ob,Integer.valueOf(con));//將信息封裝到對象中 68 } 69 70 ar.add(ob); 71 } 72 } 73 } catch (Exception e) { 74 e.printStackTrace(); 75 }finally{ 76 book.close(); 77 } 78 return ar; 79 80 } 81 82 public static void main(String[] args) { 83 // ArrayList<Book> arrayList = new ArrayList<Book>(); 84 // Book book = new Book(); 85 // book.setId(1); 86 // book.setName("Java語言"); 87 // book.setType("面向對象"); 88 // Book book1 = new Book(); 89 // book1.setId(2); 90 // book1.setName("西游記"); 91 // book1.setType("故事"); 92 // Book book2 = new Book(); 93 // book2.setId(3); 94 // book2.setName("高數"); 95 // book2.setType("難"); 96 // arrayList.add(book); 97 // arrayList.add(book1); 98 // arrayList.add(book2); 99 // ExcelUtil.excleOut(arrayList,"D:/Sourcecode/Java/fanshe/book1.xls"); 100 //導入的數據位Book類型 101 ArrayList<Book> ar = ExcelUtil.excleIn(Book.class,"D:/Sourcecode/Java/fanshe/book1.xls"); 102 for(Book bo:ar){ 103 System.out.println(bo.getId()+" "+bo.getName()+" "+bo.getType()); 104 } 105 } 106 107 }
運行結果:導出時在指定路徑出現excel表格,上面顯示導出的數據,如下圖所示:
導入時,在控制台輸出表格中的信息