我正在使用此代码,但只有一行表数据输入到文件中......我正在使用 mysql 数据库。连接在connection.jsp 中建立。表名是登录名。使用 poi-2.5.1.jar 文件。此代码工作没有任何错误,但如何将整个表格数据放入 Excel 工作表
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@page import="java.sql.*"%>
<%@pageimport="java.util.*,java.io.*,javax.servlet.*,javax.servlet.http.*"%>
<%@ page import="java.io.*"%>
<%@include file="connection.jsp" %>
<%@page import=" org.apache.poi.hssf.usermodel.*"%>
<head>
<meta httpequiv="ContentType"content="text/html;charset=UTF-8">
<title>JSP Page</title>
</head>
<% response.setContentType("application/xls");
response.setHeader("ContentDisposition","attachment;filename=File.xls"); %>
<body>
<table>
<tr><th>CG</th><th>CD</th></tr>
<%
Statement stmt = conn.createStatement();
String filename="C:/Users/sangu/Downloads/data.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("sheet");
HSSFRow row= sheet.createRow((short)0);
ResultSet res = stmt.executeQuery("select * from login ");
while (res.next())
{
String cg = res.getString(1);
String cd = res.getString(3);
row.createCell((short) 0).setCellValue(cd);
row.createCell((short) 1).setCellValue(cd);
row.createCell((short) 1).setCellValue(cd);
%>
<tr>
<td align="center"><%=cg%></td>
<td align="center"><%=cd%></td>
</tr>
<%
FileOutputStream fileOut = new FileOutputStream(filename);
hwb.write(fileOut);
fileOut.close();
out.println("Your excel file has been generated!");
hwb.write(response.getOutputStream()); }
%>
</table>
</body>
</html>