从数据库表导出为excel表格
2015-11-12 19:49
411 查看
package com.test.daotest; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.hibernate.Session; import org.hibernate.Transaction; import com.test.model.Question; import com.test.until.HibernateSessionFactory; public class ExportQuestion { public static void main(String[] args) { int id=14; try { HSSFWorkbook wb=new HSSFWorkbook(); FileOutputStream fileout = new FileOutputStream("test"+id+".xls"); wb.write(fileout); HSSFSheet sheet=wb.createSheet("new sheet"); //通过Hibernate来查询addressbook_table表中的数据,将其存储在List中 Session s=HibernateSessionFactory.getSession(); Transaction tx = s.beginTransaction(); org.hibernate.Query query= s.createQuery("from Question q where q.majorId="+id); List list = query.list(); tx.commit(); int k =0; //创建表格,创建表格行和单元格,将数据库中表的字段存储在单元格中. for(Iterator it=list.iterator();it.hasNext();){ Question q =(Question)it.next(); HSSFRow row=sheet.createRow((short)k); row.createCell((short)0).setCellValue(1); row.createCell((short)1).setCellValue(q.getQuestion()); row.createCell((short)2).setCellValue(q.getOptionA()); row.createCell((short)3).setCellValue(q.getOptionB()); row.createCell((short)4).setCellValue(q.getOptionC()); row.createCell((short)5).setCellValue(q.getOptionD()); row.createCell((short)6).setCellValue(q.getAnswer()); row.createCell((short)7).setCellValue(q.getMajorId()); row.createCell((short)8).setCellValue(0); row.createCell((short)9).setCellValue(0); k++; } FileOutputStream fileout1 = new FileOutputStream("test"+id+".xls"); wb.write(fileout1); fileout1.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
相关文章推荐
- MySql 数据库编程 笔记
- PostgreSQL数组使用
- redis的一次持久化入侵
- mysql优化配置
- oracle的order by排序中空字符串处理方法
- sqlserver清除日志
- sqlserver清除日志
- MySQL存储过程详解 mysql 存储过程
- 用户、组或角色 在当前数据库中已存在
- sqlserver不能直接create table as select ......
- MySQL学习笔记(一)MAC系统下 MySQL服务启用、停止、卸载
- Redis入门
- Mysql 开源数据源笔记
- MongoDB windows系统安装
- DB2数据库常见问题汇总
- 深入Mysql,SqlServer,Oracle主键自动增长的设置详解
- CentOS 6.5下安装Oracle 11g
- mysql基础教程(4)---函数
- Sql删除重复操作
- oracle case when then