java - how to create dynamic rows of records for creating xls file using list -


here after fetching records database have added data in list , have set session variables them can access in method using get(key) method of session successful .now want want create dynamic records setting list value in row unable so.it creates file there no record displayed .below code:

package com.ca.actions;  import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.sql.sqlexception; import java.util.arraylist; import java.util.list; import java.util.map;  import org.apache.poi.hssf.usermodel.hssfrow; import org.apache.poi.hssf.usermodel.hssfsheet; import org.apache.poi.hssf.usermodel.hssfworkbook; import org.apache.struts2.dispatcher.sessionmap; import org.apache.struts2.interceptor.sessionaware;  import com.ca.database.database; import com.ca.pojo.event; import com.itextpdf.text.document; import com.itextpdf.text.element; import com.itextpdf.text.pagesize; import com.itextpdf.text.paragraph; import com.itextpdf.text.phrase; import com.itextpdf.text.pdf.pdfpcell; import com.itextpdf.text.pdf.pdfptable; import com.itextpdf.text.pdf.pdfwriter; import com.opensymphony.xwork2.actionsupport; import com.opensymphony.xwork2.preparable;  public class dataforgeneralreportsaction extends actionsupport implements         preparable, sessionaware {     private list<string> eventsgeneral = new arraylist<string>();     private list<string> companiesgeneral = new arraylist<string>();     private sessionmap<string, object> sessionmapgeneral;     list<string> eventidlist = new arraylist<string>();     list<string> eventnamelist = new arraylist<string>();     list<string> eventvenuelist = new arraylist<string>();     list<string> eventtimelist = new arraylist<string>();     list<string> companynamelist = new arraylist<string>();     list<string> totalamountlist = new arraylist<string>();     list<string> receivedamountlist = new arraylist<string>();     list<string> balanceamountlist = new arraylist<string>();     list<string> eventtdslist = new arraylist<string>();     list<string> paymentdatelist = new arraylist<string>();     list<string> chequeddlist = new arraylist<string>();      private string eventgeneral = null;     private string companygeneral = null;     list<event> dataforgeneralreports;      public list<string> geteventidlist() {         return eventidlist;     }      public void seteventidlist(list<string> eventidlist) {         this.eventidlist = eventidlist;     }      public list<string> geteventnamelist() {         return eventnamelist;     }      public void seteventnamelist(list<string> eventnamelist) {         this.eventnamelist = eventnamelist;     }      public list<string> geteventvenuelist() {         return eventvenuelist;     }      public void seteventvenuelist(list<string> eventvenuelist) {         this.eventvenuelist = eventvenuelist;     }      public list<string> geteventtimelist() {         return eventtimelist;     }      public void seteventtimelist(list<string> eventtimelist) {         this.eventtimelist = eventtimelist;     }      public list<string> getcompanynamelist() {         return companynamelist;     }      public void setcompanynamelist(list<string> companynamelist) {         this.companynamelist = companynamelist;     }      public list<string> gettotalamountlist() {         return totalamountlist;     }      public void settotalamountlist(list<string> totalamountlist) {         this.totalamountlist = totalamountlist;     }      public list<string> getreceivedamountlist() {         return receivedamountlist;     }      public void setreceivedamountlist(list<string> receivedamountlist) {         this.receivedamountlist = receivedamountlist;     }      public list<string> getbalanceamountlist() {         return balanceamountlist;     }      public void setbalanceamountlist(list<string> balanceamountlist) {         this.balanceamountlist = balanceamountlist;     }      public list<string> geteventtdslist() {         return eventtdslist;     }      public void seteventtdslist(list<string> eventtdslist) {         this.eventtdslist = eventtdslist;     }      public list<string> getpaymentdatelist() {         return paymentdatelist;     }      public void setpaymentdatelist(list<string> paymentdatelist) {         this.paymentdatelist = paymentdatelist;     }      public list<string> getchequeddlist() {         return chequeddlist;     }      public void setchequeddlist(list<string> chequeddlist) {         this.chequeddlist = chequeddlist;     }      public sessionmap<string, object> getsessionmapgeneral() {         return sessionmapgeneral;     }      public void setsessionmapgeneral(             sessionmap<string, object> sessionmapgeneral) {         this.sessionmapgeneral = sessionmapgeneral;     }      public string geteventgeneral() {         return eventgeneral;     }      public void seteventgeneral(string eventgeneral) {         this.eventgeneral = eventgeneral;     }      public string getcompanygeneral() {         return companygeneral;     }      public void setcompanygeneral(string companygeneral) {         this.companygeneral = companygeneral;     }      public list<event> getdataforgeneralreports() {         return dataforgeneralreports;     }      public void setdataforgeneralreports(list<event> dataforgeneralreports) {         this.dataforgeneralreports = dataforgeneralreports;     }      public list<string> geteventsgeneral() {         return eventsgeneral;     }      public void seteventsgeneral(list<string> eventsgeneral) {         this.eventsgeneral = eventsgeneral;     }      public list<string> getcompaniesgeneral() {         return companiesgeneral;     }      public void setcompaniesgeneral(list<string> companiesgeneral) {         this.companiesgeneral = companiesgeneral;     }      public dataforgeneralreportsaction() {         // todo auto-generated constructor stub     }      @override     public void prepare() throws exception {         // todo auto-generated method stub         connection con = null;         try {             con = new database().get_connection();              // load companies             preparedstatement ps = con                     .preparestatement("select distinct company_name event");             resultset rs = ps.executequery();             while (rs.next()) {                 companiesgeneral.add(rs.getstring("company_name"));             }              // load events             ps = con.preparestatement("select distinct event_name event");             rs = ps.executequery();             while (rs.next()) {                 eventsgeneral.add(rs.getstring("event_name"));             }          } catch (exception e) {             e.printstacktrace();         } {             con.close();         }      }      @override     public string execute() throws exception {          connection con = null;         try {             con = new database().get_connection();              // load table. first time table loaded             string sql = "select event_id, event_name, company_name,event_venue,total_amount,received_amount,event_tds,balance_amount,cheque_dd_no,"                     + "date_format(payment_date,'%d/%m/%y') dateaspayment,event_time "                     + "from event";             string = "";              // if instead action has been called jsp page,             // result filtered on event , company:             if (eventgeneral != null && companygeneral != null) {                 = " event_name = ? , company_name = ?";             }              // load companies             preparedstatement ps = con.preparestatement(sql + where);             if (where.length() > 0) {                 ps.setstring(1, eventgeneral);                 ps.setstring(2, companygeneral);             }             dataforgeneralreports = new arraylist<event>();             resultset rs = ps.executequery();             int i, j = 0;             while (rs.next()) {                  dataforgeneralreports.add(new event(rs.getstring("event_id"),                         rs.getstring("event_name"), rs                                 .getstring("company_name"), rs                                 .getstring("event_venue"), rs                                 .getstring("event_time"), rs                                 .getstring("total_amount"), rs                                 .getstring("received_amount"), rs                                 .getstring("cheque_dd_no"), rs                                 .getstring("dateaspayment"), rs                                 .getstring("balance_amount"), rs                                 .getstring("event_tds")));                  eventidlist.add(rs.getstring("event_id"));                 eventnamelist.add(rs.getstring("event_name"));                 companynamelist.add(rs.getstring("company_name"));                 eventvenuelist.add(rs.getstring("event_venue"));                 eventtimelist.add(rs.getstring("event_time"));                 totalamountlist.add(rs.getstring("total_amount"));                 receivedamountlist.add(rs.getstring("received_amount"));                 chequeddlist.add(rs.getstring("cheque_dd_no"));                 paymentdatelist.add(rs.getstring("dateaspayment"));                 eventtdslist.add(rs.getstring("event_tds"));                 balanceamountlist.add(rs.getstring("balance_amount"));              }             sessionmapgeneral.put("eventidpdf", eventidlist);             sessionmapgeneral.put("eventnamepdf", eventnamelist);             sessionmapgeneral.put("companynamepdf", companynamelist);             sessionmapgeneral.put("eventvenuepdf", eventvenuelist);             sessionmapgeneral.put("eventtimepdf", eventtimelist);             sessionmapgeneral.put("totalamountpdf", totalamountlist);             sessionmapgeneral.put("receivedamountpdf", receivedamountlist);             sessionmapgeneral.put("chequeddpdf", chequeddlist);             sessionmapgeneral.put("paymentdatepdf", paymentdatelist);             sessionmapgeneral.put("eventtdspdf", eventtdslist);             sessionmapgeneral.put("balanceamountpdf", balanceamountlist);          } catch (exception e) {             e.printstacktrace();         } {             con.close();         }          return success;      }      public string generatepdfgeneral() throws exception {          system.out.println(sessionmapgeneral.get("eventidpdf"));         document document = new document(pagesize.a4_landscape, 50, 50, 50, 50);         float[] columnwidths = { 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };          pdfwriter writer = pdfwriter.getinstance(document,                 new fileoutputstream("d:\\generalreports.pdf"));         pdfptable table = new pdfptable(11);         table.setspacingbefore(25);         table.setwidthpercentage(100);         table.setspacingafter(25);         table.setwidths(columnwidths);         pdfpcell c1 = new pdfpcell(new phrase("event id "));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);          c1 = new pdfpcell(new phrase("event name "));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);          c1 = new pdfpcell(new phrase("event time"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);          c1 = new pdfpcell(new phrase("event venue"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("company name"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("total amount"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("received amount"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("cheque/dd number"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("payment date"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("event tds"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);         c1 = new pdfpcell(new phrase("balance amount"));         c1.sethorizontalalignment(element.align_center);         table.addcell(c1);          table.setheaderrows(1);         pdfpcell cell = new pdfpcell();         list<string> list = (list<string>) sessionmapgeneral.get("eventidpdf");         (string item : list) {             cell.addelement(new paragraph(item));         }         pdfpcell cell1 = new pdfpcell();         list<string> list1 = (list<string>) sessionmapgeneral                 .get("eventnamepdf");         (string item : list1) {             cell1.addelement(new paragraph(item));         }         table.addcell(cell1);         pdfpcell cell2 = new pdfpcell();         list<string> list2 = (list<string>) sessionmapgeneral                 .get("eventtimepdf");         (string item : list2) {             cell2.addelement(new paragraph(item));         }         table.addcell(cell2);         pdfpcell cell3 = new pdfpcell();         list<string> list3 = (list<string>) sessionmapgeneral                 .get("eventvenuepdf");         (string item : list1) {             cell3.addelement(new paragraph(item));         }          table.addcell(cell3);         pdfpcell cell4 = new pdfpcell();         list<string> list4 = (list<string>) sessionmapgeneral.get("eventidpdf");         (string item : list4) {             cell4.addelement(new paragraph(item));         }          table.addcell(cell4);         pdfpcell cell5 = new pdfpcell();         list<string> list5 = (list<string>) sessionmapgeneral                 .get("companynamepdf");         (string item : list5) {             cell5.addelement(new paragraph(item));         }          table.addcell(cell5);         pdfpcell cell6 = new pdfpcell();         list<string> list6 = (list<string>) sessionmapgeneral                 .get("totalamountpdf");         (string item : list6) {             cell6.addelement(new paragraph(item));         }          table.addcell(cell6);         pdfpcell cell7 = new pdfpcell();         list<string> list7 = (list<string>) sessionmapgeneral                 .get("receivedamountpdf");         (string item : list7) {             cell7.addelement(new paragraph(item));         }          table.addcell(cell7);         pdfpcell cell8 = new pdfpcell();         list<string> list8 = (list<string>) sessionmapgeneral                 .get("chequeddpdf");         (string item : list8) {             cell8.addelement(new paragraph(item));         }          table.addcell(cell8);         pdfpcell cell9 = new pdfpcell();         list<string> list9 = (list<string>) sessionmapgeneral                 .get("paymentdatepdf");         (string item : list9) {             cell9.addelement(new paragraph(item));         }          table.addcell(cell9);         pdfpcell cell10 = new pdfpcell();         list<string> list10 = (list<string>) sessionmapgeneral                 .get("eventtdspdf");         (string item : list10) {             cell10.addelement(new paragraph(item));         }          table.addcell(cell10);         pdfpcell cell11 = new pdfpcell();         list<string> list11 = (list<string>) sessionmapgeneral                 .get("balanceamountpdf");         (string item : list11) {             cell11.addelement(new paragraph(item));         }          table.addcell(cell11);          document.open();         document.add(table);         document.close();         return "success";      }      public string generategeneralxls() throws exception {         try {             hssfworkbook workbook = new hssfworkbook();             hssfsheet sheet = workbook.createsheet("firstsheet");             hssfrow rowhead = sheet.createrow((short) 0);             rowhead.createcell(0).setcellvalue("event id");             rowhead.createcell(1).setcellvalue("event name");             rowhead.createcell(2).setcellvalue("event time");             rowhead.createcell(3).setcellvalue("event venue");             rowhead.createcell(4).setcellvalue("company name");             rowhead.createcell(5).setcellvalue("total amount");             rowhead.createcell(6).setcellvalue("received amount");             rowhead.createcell(7).setcellvalue("payment date");             rowhead.createcell(8).setcellvalue("cheque/dd no.");             rowhead.createcell(9).setcellvalue("event tds");             rowhead.createcell(10).setcellvalue("balance amount");             fileoutputstream fileout;              fileout = new fileoutputstream("d:\\samplmgjkm.xls");              // hssfrow row1 = sheet.createrow((short) 1);             system.out.println(sessionmapgeneral.size());             (int = 1; <= sessionmapgeneral.size(); i++) {                 hssfrow row1 = sheet.createrow((short) i);                 row1.createcell(i-1).setcellvalue(                         sessionmapgeneral.get("eventidpdf").tostring());                  row1.createcell(i).setcellvalue(                         sessionmapgeneral.get("eventnamepdf").tostring());              }               /*              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              * row1.createcell(1).setcellvalue(              * sessionmapgeneral.get("eventnamepdf").tostring());              */              workbook.write(fileout);             fileout.close();          } catch (exception e) {             e.printstacktrace();         }         return "success";     }      @override     public void setsession(map<string, object> map) {         // todo auto-generated method stub         sessionmapgeneral = (sessionmap) map;     }  } 

i have edited code result in string records displayed in single cell. want each record in new cell.i have attached image of how looks. enter image description here no error displayed. please me solve problem.

first poposal : use event object.you build list of event object use , put in sessionmap , if want, build other list using list of events)

here , exemple assuming have getter in event object :

  public  string generategeneralxls() throws exception {             try {                  string titles="event id,event name,event time,event venue,company name,total amount,received amount,payment date,cheque/dd no,event tds,balance amount";                 string[]arrtiltes=titles.split(",");                      fileoutputstream fileout = new fileoutputstream("poi-test.xls");                 hssfworkbook workbook = new hssfworkbook();                 hssfsheet worksheet = workbook.createsheet("poi worksheet");                   int row=0;                 hssfrow rowtitle = worksheet.createrow(row);                  // set  titles                    for(int i=0;i<arrtiltes.length;i++){                     hssfcell celltitle=rowtitle.createcell(i);                     celltitle.setcellvalue(arrtiltes[i]);                   }                  //setting values                 row++;                 for(event datavalue:dataforgeneralreports){                     hssfrow rowvalue = worksheet.createrow(row);                           hssfcell cell0 = rowvalue.createcell(0);                         cell0.setcellvalue(datavalue.geteventid());                          hssfcell cell1 = rowvalue.createcell(1);                         cell1.setcellvalue(datavalue.geteventname());                          hssfcell cell2 = rowvalue.createcell(2);                         cell2.setcellvalue(datavalue.geteventtime());                          hssfcell cell3 = rowvalue.createcell(3);                         cell3.setcellvalue(datavalue.geteventvenue());                          hssfcell cell4 = rowvalue.createcell(4);                         cell4.setcellvalue(datavalue.getcompanyname());                          hssfcell cell5 = rowvalue.createcell(5);                         cell5.setcellvalue(datavalue.gettotalamount());                          hssfcell cell6 = rowvalue.createcell(6);                         cell6.setcellvalue(datavalue.getreceivedamount());                          hssfcell cell7 = rowvalue.createcell(7);                         cell7.setcellvalue(datavalue.getpaymentdate());                          hssfcell cell8 = rowvalue.createcell(8);                         cell8.setcellvalue(datavalue.getchequeddno());                          hssfcell cell9 = rowvalue.createcell(9);                         cell9.setcellvalue(datavalue.geteventtds());                           hssfcell cell10 = rowvalue.createcell(10);                         cell10.setcellvalue(datavalue.getbalanceamount());                          row++;                                         }                    workbook.write(fileout);                 fileout.flush();                 fileout.close();              } catch (exception e) {                 e.printstacktrace();             }             return "success";         } 

you can use same idea pdf. carreful in code pdf, suspect don't have results expected.

  1. you have forgotten adding cell on table ( first cell on code)
  2. you have 12 cells declare 11:

    i sugest o remove "block"

pdfpcell cell4

second proposal : if want use lists, iterate on each list first value in each of them, second value of each of them etc..

exemple assuming list have same size (coming resultset):

public string generategeneralxlsother() throws exception {     try {          string titles="eventidpdf,eventnamepdf,companynamepdf,eventvenuepdf,eventtimepdf,totalamountpdf,receivedamountpdf,receivedamountpdf,chequeddpdf,paymentdatepdf,eventtdspdf,balanceamountpdf";          string[]arrtiltes=titles.split(",");                    fileoutputstream fileout = new fileoutputstream("poi-testotehr.xls");         hssfworkbook workbook = new hssfworkbook();         hssfsheet worksheet = workbook.createsheet("poi worksheet");            hssfrow rowtitle = worksheet.createrow(0);          // set  titles            for(int i=0;i<arrtiltes.length;i++){             hssfcell celltitle=rowtitle.createcell(i);             celltitle.setcellvalue(arrtiltes[i]);           }            int size=((list<string>) sessionmapgeneral.get(arrtiltes[0])).size();         for(int row=0;row<size;row++){               hssfrow rowvalue = worksheet.createrow(row+1);             int cell=0;           for(int i=0;i<arrtiltes.length;i++){                list<string> thelist=(list<string>) sessionmapgeneral.get(arrtiltes[i]);               hssfcell cell0 = rowvalue.createcell(cell);                 cell0.setcellvalue(thelist.get(row));                 cell++;            }         }           workbook.write(fileout);         fileout.flush();         fileout.close();      } catch (exception e) {         e.printstacktrace();     }     return "success"; } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -