Export from a database table to an Excel table

Source: Internet
Author: User

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 to query the data in the Addressbook_table table and store it in the 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;

?

???????????? Create tables, create table rows and cells, and store the fields of tables in a database in a cell.

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

????????}

????}

}

Export from a database table to an Excel table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.