JSP导出响应Excel文件

时间:2016-07-19 09:33:14

标签: java jsp

if (Mode.equalsIgnoreCase("View"))
{  

    try
    {

        DimEN div = new DimEN();
        BcDS divStr = new BcDS();
        if(divisionCode==null || divisionCode.equals(""))
        {
            divisionCode = "All";
        }

        ArrayList divRec = divStr.getLinkBC(divisionCode,divisionCriteria);

        if(outputExcel.equals("true")){
            out.println("outputExcel true");
            response.setContentType("application/excel");
            response.setHeader("Content-Disposition","attachment;filename=DivisionBc.xls");
        }

        if(divRec != null)
        {
        if (divRec.size() > 0)
            {
                Paging = divRec.size(); 
                RecordPerPage=15;

%>

                <br>
                <center>
                <table class="LARGE" width="90%" >
                    <tr>
                        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td colspan=7 class="HEAD" align=center>View - Division - BC</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                        <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class=LABEL>&nbsp;</td>
                        <td class=LABEL>&nbsp;</td>
                        <td class=LABEL>&nbsp;</td>
                        <td class=LABEL colspan=3 align=center>Created</td>
                        <td class=LABEL>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class=LABEL>Company Code</td>
                        <td class=LABEL>Division Code</td>
                        <td class=LABEL>Business Code</td>
                        <td class=LABEL>Created on</td>
                        <td class=LABEL>Created by</td>
                    </tr>
                    <%@ include file="Include/NavTop.jsp" %>
<%          

                    for (int i=StartIndex; i<LastIndex; i++)
                    //for (int i = 0; i < divRec.size(); i++)
                    {
                        div = (com.gil.hris.DimEN)divRec.get(i);
                        String dispDateCreate = div.getCreationDate();
                        String dispDateValid = div.getValidUpto();
                        String QDateCreate = "";
                        String QDateValid = "";

                        if (dispDateCreate == null)
                        {
                            QDateCreate  = "-";
                        }
                        else
                        {
                            String yyyyD = dispDateCreate.substring(0,4);
                            String mmD = dispDateCreate.substring(5,7);
                            String ddD = dispDateCreate.substring(8,10);
                            QDateCreate  = ddD+"/"+mmD+"/"+yyyyD;
                        }

                        if (dispDateValid == null)
                        {
                            QDateValid = "-";
                        }
                        else
                        {
                            String yyyyD = dispDateValid.substring(0,4);
                            String mmD = dispDateValid.substring(5,7);
                            String ddD = dispDateValid.substring(8,10);
                            QDateValid = ddD+"/"+mmD+"/"+yyyyD;
                        }

                        if (i%2==0)
                        {
%>
                            <tr>
                                <td class=QUERY><%=company_code%></td>
                                <td class=QUERY><%=div.getDimCode()%></td>
                                <td class=QUERY><%=div.getBcCode()%></td>
                                <td class=QUERY><%=QDateCreate%></td>
                                <td class=QUERY><%=div.getCreatedByEmp()%></td>
                            </tr>
<%
                        }
                        else
                        {
%>
                            <tr>
                                <td class=QUERY2><%=company_code%></td>
                                <td class=QUERY2><%=div.getDimCode()%></td>
                                <td class=QUERY2><%=div.getBcCode()%></td>
                                <td class=QUERY2><%=QDateCreate%></td>
                                <td class=QUERY2><%=div.getCreatedByEmp()%></td>
                            </tr>
<%
                        }

                    } 
%>
                </table>

                </center>
<%

            }

        }
        else
        {
            mesg = Database.getMessage();
            //mesg="Error - Division Code Not Found.";
%>
            <br>
            <center>
                <table class="small" width="60%">
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td class="HEAD" align=center>View - Division - BC</td>
                    </tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr>
                        <td class="LABEL" align=center><%= mesg %></td>
                    </tr>
                </table>
            </center>
<%
        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println(e);
          mesg = "General Error";
    }   
} 

这是我的代码,但它不起作用。 Excel没有生成。

任何人都可以提供帮助。谢谢。 我也按照代码使用 response.setContentType(&#34;应用程序/ vnd.ms-的excel&#34); response.setHeader(&#34; Content-Disposition&#34;,&#34; attachment; filename = DivisionBc.xls&#34;); 它仍然无法运作。

0 个答案:

没有答案