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文件中的实现,代码注释比较全面,很容易看懂:
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import com.ecshop.common.HibernateUtil;
- import com.ecshop.vo.EcsGoods;
- public class POIexportExcel {
- /**
- * @param args
- */
- /*
- * Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft
- * Office格式档案读和写的功能。结构:
- * HSSF - 提供读写Microsoft Excel格式档案的功能。
- * XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
- * HWPF - 提供读写Microsoft Word格式档案的功能。
- * HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
- * HDGF - 提供读写Microsoft Visio格式档案的功能。
- */
- @SuppressWarnings("deprecation")
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int row = 0;
- try {
- // 创建Execl工作薄
- HSSFWorkbook book = new HSSFWorkbook();
- // 在Excel工作簿中建一工作表,其名为缺省值 ,可以指定也可以不指定
- HSSFSheet sheet = book.createSheet("goods");
- //将需要导出的数据库表中的数据存入 List 中
- List<EcsGoods> list = new ArrayList<EcsGoods>();
- Session session = HibernateUtil.getSessionFactory()
- .getCurrentSession();
- session.beginTransaction();
- Query query = session
- .createQuery("from EcsGoods where isReal = 1 and isDelete = 0");
- list = query.list();
- session.getTransaction().commit();
- // 使用List的迭代器循环输出
- Iterator<EcsGoods> it = list.iterator();
- while (it.hasNext()) {
- EcsGoods good = it.next();
- // 使用反射机制获取Class类
- Class cl = Class.forName("com.ecshop.vo.EcsGoods");
- // 获取class类的所有成员变量
- Field[] field = cl.getDeclaredFields();
- if(row==0){
- //创建sheet表中的行
- HSSFRow top = sheet.createRow(row);
- HSSFRow reco = sheet.createRow(row+1);
- //第0行输出字段名同时第一行输出第一条记录值
- for(int tem =0;tem<field.length;tem++){
- field[tem].setAccessible(true);
- //建立单无格
- HSSFCell topc = top.createCell(tem,HSSFCell.CELL_TYPE_STRING);
- topc.setCellValue(field[tem].getName());
- HSSFCell recoc = reco.createCell(tem,HSSFCell.CELL_TYPE_STRING);
- recoc.setCellValue(field[tem].get(good) == null ? "" : field[tem].get(good)
- .toString());
- }
- row+=2;
- }else{
- HSSFRow reco = sheet.createRow(row);
- for(int tem =0;tem<field.length;tem++){
- field[tem].setAccessible(true);
- //建立单无格
- HSSFCell recoc = reco.createCell(tem,HSSFCell.CELL_TYPE_STRING);
- /*
- * 在Label对象的构造子中指名单元格位置是第i列第row行(i,row) 以及单元格内容为
- * field[tem].get(good).toString()即good对象每个字段的值,这里需要注意一下,如果表中
- * 字段为Null时就报错,所以要将field[tem].get(good).toString()改为:
- * field[tem].get(good) == null ? "":
- * field[tem].get(good).toString() 这样的话如果值为Null时就 导出空字符即可。
- */
- recoc.setCellValue(field[tem].get(good) == null ? "" : field[tem].get(good)
- .toString());
- }
- row++;
- }
- }
- // 新建一输出文件流
- FileOutputStream output = new FileOutputStream("E:\\poiGoods.xls");
- // 把相应的Excel 工作簿写入到文件中
- book.write(output);
- //即清空缓冲区数据
- output.flush();
- // 操作结束,关闭文件
- output.close();
- System.out.println("文件生成成功!");
- } catch (ClassNotFoundException e) {
- System.out.println("未找到指定的类!");
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- System.out.println("打开文件失败!");
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println("写入文件失败!");
- e.printStackTrace();
- }
- }
- }
转载于:https://blog.51cto.com/lossie/1082083