Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Complete example to download a .xls FILE USIGN GWT,apache-poi</p> <ul> <li>Environment : GWT SDK 2.4</li> <li>Spring 3.0.2M</li> <li>JAVA : 1.7</li> </ul> <p>Needed Jar file to create .xls file :: poi-3.0.1-FINAL.jar</p> <pre><code>exportButton.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { String link = GWT.getModuleBaseURL() + "myfiledownload"; }//onClick });//addClickHandler </code></pre> <p>now at the browser when the user hits button exportButton then the control navigates to web.xml and searches for url-pattern that ends with /myfiledownload</p> <p>web.xml</p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;fileDownload&lt;/servlet-name&gt; &lt;servlet-class&gt;com.sbabamca.server.FileDownloadServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;fileDownload&lt;/servlet-name&gt; &lt;url-pattern&gt;/app/myfiledownload&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p>After execuiting the code in web.xml the controls tries to execute the servlet with name FileDownloadServlet</p> <h2>SERVER SIDE</h2> <p><strong>FileDownloadServlet.java</strong></p> <pre><code>import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.metadata.ClassMetadata; import com.fpix.hibernate.util.HibernateUtil; import com.fpix.util.date.MyDate; import java.io.IOException; import java.util.ArrayList; public class MyFileServlet extends HttpServlet { SessionFactory sessionFactory; Session session; Transaction tx = null; @SuppressWarnings({ "unchecked", "deprecation" }) protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("application/vnd.ms-excel"); resp.setHeader("Content-Disposition", "attachment; filename=users.xls"); try { sessionFactory = HibernateUtil.getSessionFactory(); if(!sessionFactory.getCurrentSession().isOpen()) { session = sessionFactory.openSession(); } else { session = sessionFactory.getCurrentSession(); } tx = session.beginTransaction(); tx.begin(); Query qry = session.createQuery("from com.fpix.dto.User u "); ArrayList&lt;com.fpix.dto.User&gt; u = (ArrayList&lt;com.fpix.dto.User&gt;)qry.list(); Query qry1 = session.createQuery("from com.fpix.dto.Profile p "); ArrayList&lt;com.fpix.dto.Profile&gt; p = (ArrayList&lt;com.fpix.dto.Profile&gt;)qry1.list(); /* * code to get the column name of User and Profile Entity */ ClassMetadata cm = sessionFactory.getClassMetadata(com.fpix.dto.User.class); String userAttributes[] = cm.getPropertyNames(); ClassMetadata cm1 = sessionFactory.getClassMetadata(com.fpix.dto.Profile.class); String profileAttributes[] = cm1.getPropertyNames(); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Excel Sheet"); HSSFRow rowhead = sheet.createRow((short) 0); rowhead.createCell((short) 0).setCellValue("id"); /* * code to create the columns names in .xls file */ int j = 0; for(int i=0;i&lt;userAttributes.length;i++){ j = i; if(!userAttributes[i].equalsIgnoreCase("profileData") ) rowhead.createCell((short) ++j).setCellValue(userAttributes[i]); }//for for(int i=0;i&lt;profileAttributes.length;i++){ if(!profileAttributes[i].equalsIgnoreCase("userData")) rowhead.createCell((short) ++j).setCellValue(profileAttributes[i]); }//for int index = 1; for(int i=0;i&lt;u.size();i++){ HSSFRow row = sheet.createRow((short) index); row.createCell((short) 0).setCellValue(u.get(i).getId()); row.createCell((short) 1).setCellValue(u.get(i).getUser()); row.createCell((short) 2).setCellValue(u.get(i).getPassWord()); row.createCell((short) 3).setCellValue(u.get(i).getType()); row.createCell((short) 4).setCellValue(u.get(i).getRole()); row.createCell((short) 5).setCellValue(u.get(i).getProfile()); row.createCell((short) 6).setCellValue(u.get(i).getEmail()); row.createCell((short) 7).setCellValue(MyDate.timeStampToString(u.get(i).getDor())); // set the Profile data to the excel sheet cells row.createCell((short) 8).setCellValue(p.get(i).getRollNo()); row.createCell((short) 9).setCellValue(p.get(i).getFname()); row.createCell((short) 10).setCellValue(p.get(i).getLname()); row.createCell((short) 11).setCellValue(MyDate.timeStampToString(Long.parseLong(p.get(i).getDoj()))); row.createCell((short) 12).setCellValue(MyDate.timeStampToString(Long.parseLong(p.get(i).getDob()))); row.createCell((short) 13).setCellValue(p.get(i).getBloodGroup()); row.createCell((short) 14).setCellValue(p.get(i).getPhoto()); row.createCell((short) 15).setCellValue(p.get(i).getPhone1()); row.createCell((short) 16).setCellValue(p.get(i).getPhone2()); row.createCell((short) 17).setCellValue(p.get(i).getAddress()); row.createCell((short) 18).setCellValue(p.get(i).getCity()); row.createCell((short) 19).setCellValue(p.get(i).getPin()); row.createCell((short) 20).setCellValue(p.get(i).getState()); row.createCell((short) 21).setCellValue(p.get(i).getCountry()); row.createCell((short) 22).setCellValue(p.get(i).getGrade()); row.createCell((short) 23).setCellValue(p.get(i).getGroups()); row.createCell((short) 24).setCellValue(p.get(i).getAssignCourse()); index++; }//for java.io.OutputStream out = resp.getOutputStream(); wb.write(out); out.close(); System.out.println("Data is Exported to Excel file."); }//try catch (Exception e) { System.out.println(" Data cannot be imported :: getImport() :: ImportExportData "+e); }//catch }//doGet }//class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload