JLX實現將數據庫表中的信息導出到Excel文件中

 jlx  支持Excel 95-2000的所有版本、生成Excel 2000標準格式、支持字體、數字、日期操作

  能夠修飾單元格屬性、 支持圖像和圖表,這裏只簡單實現一下把數據庫表中的信息導出到excel中。記住一定要導入jlx的類包。具體代碼如下:

 

  1. import java.io.File; 
  2. import java.io.IOException; 
  3. import java.lang.reflect.Field; 
  4. import java.util.ArrayList; 
  5. import java.util.Iterator; 
  6. import java.util.List; 
  7.  
  8. import com.ecshop.dao.Goodsdao; 
  9. import com.ecshop.vo.EcsGoods; 
  10.  
  11. import jxl.Workbook; 
  12. import jxl.write.Label; 
  13. import jxl.write.WritableSheet; 
  14. import jxl.write.WritableWorkbook; 
  15. import jxl.write.WriteException; 
  16. import jxl.write.biff.RowsExceededException; 
  17. import com.ecshop.common.HibernateUtil;
  18.  
  19. public class GoodsOutToExecl { 
  20.  
  21.     /** 
  22.      * @param args 
  23.      *            實現將數據庫表EcsGoods中的字段信息導出到Execl文件中 
  24.      */ 
  25.     public static void main(String[] args) { 
  26.         int row = 0;// 記錄寫入Execl操作的當前行 
  27.         try { 
  28.             // 打開文件 
  29.             WritableWorkbook book = Workbook.createWorkbook(new File( 
  30.                     "goods.xls")); 
  31.             // 生成名爲“sheet1”的工作表,參數0表示這是工作部的第一個sheet表格 
  32.             WritableSheet sheet = book.createSheet("sheet1"0); 
  33.             List<EcsGoods> list = new ArrayList<EcsGoods>(); 
  34.             // 查詢數據庫內容保存到List中 
  35.             Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  36. session.beginTransaction();
  37. Query query = session.createQuery("from EcsGoods where isReal = 1 and isDelete = 0");
  38. list = query.list();
  39. session.getTransaction().commit();
  40.             // 使用List的迭代器循環輸出 
  41.             Iterator<EcsGoods> it = list.iterator(); 
  42.             while (it.hasNext()) { 
  43.                 EcsGoods good = it.next(); 
  44.                 // 使用反射機制獲取Class類 
  45.                 Class cl = Class.forName("com.ecshop.vo.EcsGoods"); 
  46.                 // 獲取class類的所有成員變量 
  47.                 Field[] f = cl.getDeclaredFields(); 
  48.                 if (row == 0) { 
  49.                     // 輸出表的字段名 
  50.                     for (int i = 0; i < f.length; i++) { 
  51.                         f[i].setAccessible(true); 
  52.                         // 在Label對象的構造子中指名單元格位置是第i列第row行(i,row) 
  53.                         // 以及單元格內容爲 f[i].getName()即成員變量的名字的名稱 
  54.                         Label label = new Label(i, row, f[i].getName()); 
  55.                         sheet.addCell(label); 
  56.                     } 
  57.                     row++; 
  58.                 } else { 
  59.                     // 輸出每條記錄的值 
  60.                     for (int i = 0; i < f.length; i++) { 
  61.                         f[i].setAccessible(true); 
  62.                         /* 
  63.                          * 在Label對象的構造子中指名單元格位置是第i列第row行(i,row) 以及單元格內容爲 
  64.                          * f[i].get(good).toString()即good對象每個字段的值,這裏需要注意一下,如果表中 
  65.                          * 字段爲Null時就報錯,所以要將f[i].get(good).toString()改爲: 
  66.                          * f[i].get(good) == null ? "": 
  67.                          * f[i].get(good).toString() 這樣的話如果值爲Null時就 導出空字符即可。 
  68.                          */ 
  69.                         Label label = new Label(i, row, 
  70.                                 f[i].get(good) == null ? "" : f[i].get(good) 
  71.                                         .toString()); 
  72.                         sheet.addCell(label); 
  73.                     } 
  74.                     row++; 
  75.                 } 
  76.             } 
  77.             book.write(); 
  78.             book.close(); 
  79.         } catch (IOException e) { 
  80.             // TODO Auto-generated catch block 
  81.             e.printStackTrace(); 
  82.         } catch (RowsExceededException e) { 
  83.             // TODO Auto-generated catch block 
  84.             e.printStackTrace(); 
  85.         } catch (WriteException e) { 
  86.             e.printStackTrace(); 
  87.         } catch (ClassNotFoundException e) { 
  88.             e.printStackTrace(); 
  89.         } catch (IllegalArgumentException e) { 
  90.             e.printStackTrace(); 
  91.         } catch (IllegalAccessException e) { 
  92.             e.printStackTrace(); 
  93.         } 
  94.     } 
  95.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章