zoukankan      html  css  js  c++  java
  • 从数据库表导出为excel表格

    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();

            }

        }

    }

  • 相关阅读:
    flash flip 效果集
    [Chatter] : 程序设计的深度跟广度
    [Architecture Pattern] Lazy Boundary
    [ASP.NET] : 可抽换式验证来源 (DB验证建立、抽换)
    [ASP.NET] : 可抽换式验证来源 (LDAP验证、Windows验证...)
    [.NET] : 测试项目生命周期
    [Objectoriented] : 重用
    [.NET] : 自定义Configuration区段的资料写入
    [Design Pattern] : Builder vs Factory的差异点
    [Windows Forms] : 跨线程控制WinForm窗体对象
  • 原文地址:https://www.cnblogs.com/chengzhipcx/p/4960082.html
Copyright © 2011-2022 走看看