• 从数据库表导出为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();

            }

        }

    }

  • 相关阅读:
    【Mybatis源码解析】Mybatis的日志系统
    20200728
    【Mybatis源码解析】-Configuration
    【日志】怎么打印日志
    【OOM】几种常见的OOM异常
    树 [虚树, 动态规划]
    最大公约数 [动态规划]
    送分题 [组合计数]
    LCM [树状数组, HH的项链]
    AT1219 歴史の研究 [回滚莫队]
  • 原文地址:https://www.cnblogs.com/chengzhipcx/p/4960082.html
Copyright © 2020-2023  润新知