0

i work with Struts2 and Hibernate JPA with MySQL and i want reporting with excel here i want make button to save in excel all data exporting in my datatable, I want is there any API for that ?? or some tutorial to export data to excel, i have my datatable like this :

<table class="table table-striped table-condensed" id="dt_b">
<thead>
 <tr>
 <th>codeOracle</th>
 <th>date</th>
  <th>description</th>
 </tr>
</thead>
<tbody>
 <s:iterator value="typeList" >
<tr class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
   <td><s:property value="typeSiteDesc" /></td>
   <td><s:property value="dateSiteType" /></td>
   <td><s:property value="description" /></td>
</tr>
</s:iterator>
</tbody>
</table>

Edite 1

I 'm trying some thing but i give this error on the browser :

java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
    org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:369)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:273)
    com.googlecode.s2hibernate.struts2.plugin.s2hibernatevalidator.interceptor.HibernateValidatorInterceptor.intercept(HibernateValidatorInterceptor.java:123)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)

Struts.xml

<action     name="export"                   method="exportExcel"    class="TypeSitesAction">  
    <result name="success" type="stream">  
        <param name="contentType">application/vnd.ms-excel</param>  
        <param name="inputName">inputStream</param>  
        <param name="contentDisposition">attachment;filename="typesite.xls"</param>  
        <param name="bufferSize">4096</param>  
    </result>  
</action>  

Class Action :

public class TypeSitesAction extends ActionSupport implements ModelDriven<TypeSites> {

private static final long serialVersionUID = -6659925652584240539L;

private TypeSites               type     = new TypeSites();
private List<TypeSites>         typeList = new ArrayList<TypeSites>();
private TypeSitesDAO            typeDAO  = new TypeSitesDAOImpl();

public TypeSites getModel() {
    return type;
}

/* -------------EXCEL-------------*/  
    public String  exportExcel() {  
        return SUCCESS;  
    }  

    public InputStream getInputStream() throws Exception {  
        return this.getInStream();  
    }  
    public InputStream getInStream() throws Exception {  
        HSSFWorkbook wb = new HSSFWorkbook(); //  
        HSSFSheet sheet = wb.createSheet(); //,""  

        //  
        HSSFCellStyle style = wb.createCellStyle();  
        HSSFFont font = wb.createFont();  
        font.setFontHeightInPoints((short)10); //  
        font.setColor(HSSFFont.COLOR_NORMAL); //  
        font.setFontName("");  
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //  

        style.setFont(font);  
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //?  
        style.setWrapText(false);  

        HSSFRow row = sheet.createRow(0); //  

        HSSFCell cell = row.createCell(0); //  
        cell.setCellStyle(style); //  
        cell.setCellValue(""); //  

        cell = row.createCell(1); //  
        cell.setCellStyle(style);  
        cell.setCellValue("");  

        cell = row.createCell(2); //  
        cell.setCellStyle(style);  
        cell.setCellValue("");  

        cell = row.createCell(3);  //  
        cell.setCellStyle(style);  
        cell.setCellValue("");   

        //typeList = this.userBO.findUser(userDO);
        typeList = typeDAO.listTypeSites();
        System.out.println(typeList.size());  
        if(typeList != null) {  
            for(int i=0; i<typeList.size(); i++) {  
                row = sheet.createRow(i+1);  
                cell = row.createCell(i);  
                row.createCell(0).setCellValue(typeList.get(i).getIdTypeSite());  
                row.createCell(1).setCellValue(typeList.get(i).getTypeSiteDesc());  
                row.createCell(2).setCellValue(typeList.get(i).getDescription());  
                row.createCell(3).setCellValue(typeList.get(i).getDateSiteType());  
            }  
        }  
        String fileName = "Typee";  
        StringBuffer sb = new StringBuffer(fileName);  
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
        sb.append(df.format(new Date()));  
        sb.append(".xls");  
        File file = new File(sb.toString());  
        try{  
            OutputStream os = new FileOutputStream(file);  
            wb.write(os);  
            os.flush();  
            os.close();  
            InputStream inputStream = new FileInputStream(file);  
            return inputStream;  
        }catch(Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }
public String list()
    {
        typeList = typeDAO.listTypeSites();
        return SUCCESS;
    }

Model :

public class SitesDAOImpl implements SitesDAO {

    @SessionTarget
    Session session;

    @TransactionTarget
    Transaction transaction;

    public List<Sites> listSites() {
        List<Sites> site = null;
        try {
            site = session.createQuery("from Sites").list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return site;
    }

Edite 2 : I was solve that by editing the function like that :

public String  exportExcel() throws Exception {  
    getInStream();
    return SUCCESS;  
}  

enter image description here now i can export my datatable to excel ,but i have another issue i don't get date correctly for exemple i get 40704 instead 10/06/11

Ahmed
  • 259
  • 2
  • 9
  • 26

0 Answers0