JSP代码生成在JSP页面检索的Excel数据表

hjqgdpho  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(118)

我想将数据导出到Excel工作表

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@include file="connection.jsp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>

    <%
    String ht = (String)session.getAttribute("ht");
    %>
    <table border="1">
    <%
    pst = con.prepareStatement("select * from attendance where ht='"+ht+"'");
    res = pst.executeQuery();
    if(res.next())
    {
        String uname = res.getString(2);
        %>
        <b>Student Name:<%=uname%></b>

        <%
        String hlt = res.getString(1);
        %>
        <b>Hallticket:<%=hlt%></b>
        <tr><th>CG</th><th>CD</th><th>MPI</th><th>HCI</th><th>WT</th><th>MPI-Lab</th><th>CT=Lab</th><th>WT-Lab</th></tr>
        <%
        String cg = res.getString(3);
        String cd = res.getString(4);
        String mpi = res.getString(5);
        String hci = res.getString(6);
        String wt = res.getString(7);
        String mpi_lab = res.getString(8);
        String ct_lab = res.getString(9);
        String wt_lab = res.getString(10);
        %>
        <tr>
        <td align="center"><%=cg%></td>
        <td align="center"><%=cd%></td>
        <td align="center"><%=mpi%></td>
        <td align="center"><%=hci%></td>
        <td align="center"><%=wt%></td>
        <td align="center"><%=mpi_lab%></td>
        <td align="center"><%=ct_lab%></td>
        <td align="center"><%=wt_lab%></td>
        </tr>
        <br/><br/>

        <%
    }
    %>
    </table>
    </body>
    </html>

我想从数据库中检索的数据,并显示在表中,应打印在excel表请任何人能告诉我如何做到这一点...:(我用mysql数据库。

knpiaxh1

knpiaxh11#

  • 尝试使用servlet进行Excel编写。*

如果您愿意,可以将其作为JSP:Include使用到现有页面中。
在servlet中,您必须执行以下操作:

ServletOutputStream out = resp.getOutputStream();
resp.setContentType("application/vnd.ms-excel")
/*
* get data 
*/

if (data != null) {
for (int i=0; i data.length; i++) {
   String dataRow = "";
   for (int j = 0; j data[0].length; j++) {
       dataRow += data[i][j] + "\t";// add tab delimiter
   }
   out.println(dataRow);// print data
}
} else {//Bad data...
   out.println("No data to report.");
}

out.flush();

希望对你有帮助。:)

hyrbngr7

hyrbngr72#

您必须将以下行添加到要导出到excel的jsp页面:

response.setContentType("application/xls");
response.setHeader("Content-Disposition", "attachment;filename=File.xls");

或者您必须了解POI
并且必须将if(res.next())更改为while(res.next())

相关问题