效果:
1 JSP 上有 导出按钮
2 点导出
3 直接 弹出窗口 问你 是 保存EXCEL 还是打开
1 建立 jsp 文件
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="javax.servlet.http.HttpSession"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form name="kd" action="Jxlservlet" method=post>
请选择文件 <input type="file" name="filename">
<input type=submit value="导入">
</br> </br> </br> </br> </br>
......................
<a href="./file/mode.xls">模版下载</a>
<%
//下面代码用来 实现防刷新
Integer flag22=(int)Math.random()*999999999;
session.setAttribute("flag",flag22);
%>
<input type=hidden name="flag2" value="<%=flag22%>">
</form>
<form name="chaxun" action="outexcel" method=post>
<p> SELECT code_name,code_value FROM sys_code WHERE type_name='filter_word';
<input type=submit value="导出sys_code里的数据">
<input type=button value="等会测试 连接" onClick="location.href='./result2excel.jsp'">
</form>
</body>
</html>
2 建立 DAO 类
package zms.roya.comm;
import java.sql.*;
import java.sql.SQLException;
import java.lang.Math;
public class DAO {
private final static String jdbcurl = "jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GBK";
private final static String dbusername = "root";
private final static String dbpwd = "royasoft";
public static Connection dbjdbc=null;
public DAO()
{
System.out.println("DAO的构造函数");
}
/**
* @获取 连接
*/
public boolean dbconnet()
{
try {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
// 连接数据库
System.out.println("获取到得连接URL是" + jdbcurl);
dbjdbc = java.sql.DriverManager.getConnection(jdbcurl, dbusername,
dbpwd);
return true;
} catch (ClassNotFoundException e) {
System.out.println("驱动包有问题");
e.printStackTrace();
}
catch (SQLException e) {
System.out.println("连不上,可能数据库端IP和端口原因,也可能是账号密码原因,请看报错信息");
e.printStackTrace();
}
return false;
}
}
3 建立servlet
package zms.roya.servlet.File;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import zms.roya.comm.DAO;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class outexcel extends HttpServlet {
DAO dao = new DAO();
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
"yyyyMMddHHmmss");
String filename =formatter.format(new java.util.Date()) + ".xls";
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition", "attachment;filename="
+ new String(filename.getBytes(), "iso-8859-1"));
try {
// 创建一个可写入的excel文件对象
System.out.println("创建fILE对象");
System.out.println("创建的work对象");
OutputStream os = response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(os);
// WritableWorkbook workbook =
// Workbook.createWorkbook(file);//不能用file来创建对象
WritableFont wf = new WritableFont(WritableFont.TIMES, 12,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK);
WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
wcf.setBackground(jxl.format.Colour.YELLOW); // 设置单元格的背景颜色
wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置水平居中
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 设置垂直居中
wcf.setBorder(jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN);
System.out.println("创建的sheetE对象");
WritableSheet sheet = workbook.createSheet("黑名单", 1);
// 表头
//Label label0 = new Label(0, 0, "编号");
//sheet.addCell(label0);
//Label label1 = new Label(1, 0, "名称");
//sheet.addCell(label1);
Label label1 = new Label(0, 0, "名称");
sheet.addCell(label1);
if (DAO.dbjdbc == null)
dao.dbconnet();
stmt = DAO.dbjdbc.createStatement();
String sql="SELECT code_name,code_value FROM sys_code WHERE type_name='filter_word';";
rs=stmt.executeQuery(sql);
int i=1;
while (rs.next())
{
// Label label001=new Label(0,i,rs.getString("code_name"));
// sheet.addCell(label001);
// Label labbl002=new Label(1,i,rs.getString("code_value"));
// sheet.addCell(labbl002);
Label labbl002=new Label(0,i,rs.getString("code_value"));
sheet.addCell(labbl002);
i++;
}
// 关闭对象,释放资源
workbook.write();
workbook.close();
DAO.dbjdbc.close();
}
catch (Exception e) {
System.out.println(e);
}
}
//---------------------------------------------------------------------------------
}