如何将dataTable导出到Excel文件

时间:2015-07-01 09:52:42

标签: excel struts2 jasper-reports apache-poi inputstream

我使用Struts2和Hibernate JPA与MySQL合作,我想在这里使用excel报告我希望make按钮在excel中保存所有数据导出在我的数据表中,我想要有任何API吗?或者一些将数据导出到excel的教程,我有这样的数据表:

<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

我正在尝试一些事情,但我在浏览器上提出了这个错误:

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>  

班级行动:

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;
    }

型号:

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: 我通过编辑这样的函数来解决这个问题:

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

enter image description here 现在我可以导出我的数据表到excel,但我有另一个问题我没有正确的日期为例如我得到40704而不是10/06/11

0 个答案:

没有答案