作者:乔山办公网日期:
返回目录:excel表格制作
本篇教程我们会看到如何把JSP页面导出到Excel中,会在已有的JSP页面中增加导出excel的功能。
许多时候对e799bee5baa6e79fa5e98193e78988e69d83330于用户来说,可以在excel中看到页面内容是很方便的。公共的方案会被导出成包含一些报告、数字等信息的表格。通过导出数据导出到excel中,最终用户也可以使用excel来做各种的分析,这一点对于你的java基本程序来实现,是有困难的。
这是对应的jsp源码(导出excel功能还没有加)。一个包含简单数据表格的jsp页面。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
<table align="center" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中。那么这个页面会变成上图。
下面是新版本的jsp源码。这个版本增加了“导出到excel”超链接,而且增加了相应的功能:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
<%
String exportToExcel = request.getParameter("exportToExcel");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
}
%>
<table align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
<%
if (exportToExcel == null) {
%>
<a href="excel.jsp?exportToExcel=YES">Export to Excel</a>
<%
}
%>
</body>
</html>
导出excel,常用两种办法。
1、java后台用第三方包 比如POI, 把你想要展示的数复据,填进去,导出excel文件。
2、调用第三方插件显示在jsp页面,比如金格控件。用户在页面可以在线编辑excel,然后 用户手动导出成制excel文件。
个人建议你,画面jsp展示table之后,单独留一个“导出”按钮,用户点击之后,你后台zdjava再把刚刚通过jsp展示的数据,封装成excel,给用户下载。也就是第一种方法。这种最常用。
本篇教程我们会看到如何把JSP页面导出到Excel中,会在已有的JSP页面中增加导出excel的功能。
许多时候对于用户来说,可以在excel中看到页面内容是很方便的。公共的方案会被导出成包含一些报告、数字等信息的表格。通过导出数据导出到excel中,最终用户也可以使用excel来做各种的分析,这一点对于你的java基本程序来实现,是有困难的。
这是对应的jsp源码(导出excel功能还没有加)。一个包含简单数据表格的jsp页面。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
<table align="center" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中。那么这个页面会变成上图。
下面是新e69da5e6ba90e799bee5baa6e997aee7ad94364版本的jsp源码。这个版本增加了“导出到excel”超链接,而且增加了相应的功能:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>
<%
String exportToExcel = request.getParameter("exportToExcel");
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
}
%>
<table align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>
<%
if (exportToExcel == null) {
%>
<a href="excel.jsp?exportToExcel=YES">Export to Excel</a>
<%
}
%>
</body>
</html>
无非是这样的一个过程。先通过数据库查询出数据,放到List里,然后把这个List发往页面,然后遍历这个List把数据显示到这个表格里。 要想把数据导出到execel,很简单,把页面接受的这个List用jxl写到Execel就行了。具体将List导出到Execel的类如下:
package cms.dao;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import cms.utlis.DbUtils;
public class ToExecelByQuery {
//方法接受两个参数,一个是list,这个地方我用了泛型。另一个参数是HttpServletResponse response
public static void toExcelBy(List<AccessLog> list,HttpServletResponse response) {
// 创建工作表
WritableWorkbook book=null;
response.reset();
// 创建工作流
OutputStream os =null;
try {
// 设置弹出对话框
response.setContentType("application/DOWLOAD");
// 设置工作表的标题
response.setHeader("Content-Disposition",
"attachment; filename=****.xls");//设置生成的文件名字
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//以下是我做的导出日志的一个模版
int nCount = list.size();
WritableSheet sheet = book.createSheet("访问日志", 0);
// 生成名为"商品信息"的工作表,e799bee5baa6e997aee7ad94e58685e5aeb9366参数0表示这是第一页
int nI = 1;
// 表字段名
sheet.addCell(new jxl.write.Label(0, 0, "日志编号"));
sheet.addCell(new jxl.write.Label(1, 0, "用户ID"));
sheet.addCell(new jxl.write.Label(2, 0, "用户姓名"));
sheet.addCell(new jxl.write.Label(3, 0, "访问日期"));
sheet.addCell(new jxl.write.Label(4, 0, "访问时间"));
sheet.addCell(new jxl.write.Label(5, 0, "名片ID"));
sheet.addCell(new jxl.write.Label(6, 0, "名片名称"));
sheet.addCell(new jxl.write.Label(7, 0, "创建日期"));
sheet.addCell(new jxl.write.Label(8, 0, "更新日期"));
// 将数据追加
for(int i=1;i<list.size();i++){
sheet.addCell(new jxl.write.Label(0, i, list.get(i).toString()));
sheet.addCell(new jxl.write.Label(1, i, list.get(i).getUserId()));
sheet.addCell(new jxl.write.Label(2, i, list.get(i).getUsername()));
sheet.addCell(new jxl.write.Label(3, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(4, i, list.get(i).getComplTime()));
sheet.addCell(new jxl.write.Label(5, i, list.get(i).getCopId()));
sheet.addCell(new jxl.write.Label(6, i, list.get(i).getFirstname()));
sheet.addCell(new jxl.write.Label(7, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(8, i, list.get(i).getUpdTim()));
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大体思路就是这样的,别忘了在你的项目中导入JXL必要的jar包,这个包叫jxl.jar,你可以下载一个。希望能帮到你,欢迎追问。望采纳!