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