Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
 
  其功能结构如下:
 
  HSSF - 提供读写Microsoft Excel格式档案的功能。
 
  XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
 
  HWPF - 提供读写Microsoft Word格式档案的功能。
 
  HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
 
  HDGF - 提供读写Microsoft Visio格式档案的功能。
下面将本人写的将数据库中商品信息全部输出到Execl文件中的实现,代码注释比较全面,很容易看懂:
 
 
 
  
  1. import java.io.FileNotFoundException; 
  2. import java.io.FileOutputStream; 
  3. import java.io.IOException; 
  4. import java.lang.reflect.Field; 
  5. import java.util.ArrayList; 
  6. import java.util.Iterator; 
  7. import java.util.List; 
  8.  
  9. import org.apache.poi.hssf.usermodel.HSSFCell; 
  10. import org.apache.poi.hssf.usermodel.HSSFRow; 
  11. import org.apache.poi.hssf.usermodel.HSSFSheet; 
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
  13. import org.hibernate.Query; 
  14. import org.hibernate.Session; 
  15.  
  16. import com.ecshop.common.HibernateUtil; 
  17. import com.ecshop.vo.EcsGoods; 
  18.  
  19. public class POIexportExcel { 
  20.  
  21.     /** 
  22.      * @param args 
  23.      */ 
  24.     /* 
  25.      * Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft 
  26.      * Office格式档案读和写的功能。结构:    
  27.      * HSSF - 提供读写Microsoft Excel格式档案的功能。 
  28.      * XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。    
  29.      * HWPF - 提供读写Microsoft Word格式档案的功能。    
  30.      * HSLF - 提供读写Microsoft PowerPoint格式档案的功能。    
  31.      * HDGF - 提供读写Microsoft Visio格式档案的功能。 
  32.      */ 
  33.     @SuppressWarnings("deprecation"
  34.     public static void main(String[] args) { 
  35.         // TODO Auto-generated method stub 
  36.         int row = 0
  37.         try { 
  38.              
  39.             // 创建Execl工作薄 
  40.             HSSFWorkbook book = new HSSFWorkbook(); 
  41.             // 在Excel工作簿中建一工作表,其名为缺省值 ,可以指定也可以不指定 
  42.             HSSFSheet sheet = book.createSheet("goods"); 
  43.             //将需要导出的数据库表中的数据存入 List 中 
  44.             List<EcsGoods> list = new ArrayList<EcsGoods>(); 
  45.             Session session = HibernateUtil.getSessionFactory() 
  46.                     .getCurrentSession(); 
  47.             session.beginTransaction(); 
  48.             Query query = session 
  49.                     .createQuery("from EcsGoods where isReal = 1 and isDelete = 0"); 
  50.             list = query.list(); 
  51.             session.getTransaction().commit(); 
  52.             // 使用List的迭代器循环输出 
  53.             Iterator<EcsGoods> it = list.iterator(); 
  54.             while (it.hasNext()) { 
  55.                 EcsGoods good = it.next(); 
  56.                 // 使用反射机制获取Class类 
  57.                 Class cl = Class.forName("com.ecshop.vo.EcsGoods"); 
  58.                 // 获取class类的所有成员变量 
  59.                 Field[] field = cl.getDeclaredFields(); 
  60.                 if(row==0){ 
  61.                     //创建sheet表中的行 
  62.                     HSSFRow top = sheet.createRow(row); 
  63.                     HSSFRow reco = sheet.createRow(row+1); 
  64.                     //第0行输出字段名同时第一行输出第一条记录值 
  65.                     for(int tem =0;tem<field.length;tem++){ 
  66.                         field[tem].setAccessible(true); 
  67.                         //建立单无格 
  68.                         HSSFCell topc = top.createCell(tem,HSSFCell.CELL_TYPE_STRING); 
  69.                         topc.setCellValue(field[tem].getName()); 
  70.                         HSSFCell recoc = reco.createCell(tem,HSSFCell.CELL_TYPE_STRING); 
  71.                         recoc.setCellValue(field[tem].get(good) == null ? "" : field[tem].get(good) 
  72.                                         .toString());        
  73.                     } 
  74.                     row+=2
  75.                 }else
  76.                     HSSFRow reco = sheet.createRow(row); 
  77.                     for(int tem =0;tem<field.length;tem++){ 
  78.                         field[tem].setAccessible(true); 
  79.                         //建立单无格 
  80.                         HSSFCell recoc = reco.createCell(tem,HSSFCell.CELL_TYPE_STRING); 
  81.                         /* 
  82.                          * 在Label对象的构造子中指名单元格位置是第i列第row行(i,row) 以及单元格内容为 
  83.                          * field[tem].get(good).toString()即good对象每个字段的值,这里需要注意一下,如果表中 
  84.                          * 字段为Null时就报错,所以要将field[tem].get(good).toString()改为: 
  85.                          * field[tem].get(good) == null ? "": 
  86.                          * field[tem].get(good).toString() 这样的话如果值为Null时就 导出空字符即可。 
  87.                          */ 
  88.                         recoc.setCellValue(field[tem].get(good) == null ? "" : field[tem].get(good) 
  89.                                         .toString());        
  90.                     } 
  91.                     row++; 
  92.                 } 
  93.                  
  94.             } 
  95.             // 新建一输出文件流 
  96.             FileOutputStream  output = new FileOutputStream("E:\\poiGoods.xls"); 
  97.             // 把相应的Excel 工作簿写入到文件中 
  98.             book.write(output); 
  99.             //即清空缓冲区数据 
  100.             output.flush(); 
  101.             // 操作结束,关闭文件 
  102.             output.close(); 
  103.              
  104.             System.out.println("文件生成成功!"); 
  105.         } catch (ClassNotFoundException e) { 
  106.             System.out.println("未找到指定的类!"); 
  107.             // TODO Auto-generated catch block 
  108.             e.printStackTrace(); 
  109.         } catch (IllegalArgumentException e) { 
  110.             // TODO Auto-generated catch block 
  111.             e.printStackTrace(); 
  112.         } catch (IllegalAccessException e) { 
  113.             // TODO Auto-generated catch block 
  114.             e.printStackTrace(); 
  115.         } catch (FileNotFoundException e) { 
  116.             System.out.println("打开文件失败!"); 
  117.             // TODO Auto-generated catch block 
  118.             e.printStackTrace(); 
  119.         } catch (IOException e) { 
  120.             // TODO Auto-generated catch block 
  121.             System.out.println("写入文件失败!"); 
  122.             e.printStackTrace(); 
  123.         } 
  124.  
  125.     }