重新开始写代码 我容易嘛我 都一把年纪了
实现效果
1 JSP 显示 请浏览文件
2 选择文件
3 导入
4 到数据库能看到导入结果
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 写一个 数据库连接类
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.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import javax.servlet.http.HttpSession;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import zms.roya.comm.DAO;
public class Jxlservlet extends HttpServlet {
public Workbook workbook;
public Sheet sheet;
DAO dao = new DAO();
HttpSession session = null;
// Connection dbjdbc=null;
java.sql.Statement stmt = null;
// SELECT code_name,code_value,code_desc,type_name,show_order FROM sys_code
// WHERE type_name="FILTER_WORD"
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("GBK");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("解析EXCEL");
String strname = request.getParameter("filename");
strname = new String(strname.getBytes("ISO8859_1"), "GBK");
out.println("传递过来的文件路径是" + strname);
// 检验传递过来的隐藏参数 防刷新
if (request.getParameter("flag2") != null) {
Integer flag = new Integer(Integer.parseInt(request
.getParameter("flag2")));
out.println("这里是传递过来的隐藏session" + flag);
HttpSession session = request.getSession(true);
if (flag.equals(session.getAttribute("flag"))) {
// 更新数据
session.removeAttribute("flag");
try {
if (DAO.dbjdbc == null) {
dao.dbconnet();
}
stmt = DAO.dbjdbc.createStatement();
// 生成一个可读取的excel文件对象
File file = new File(strname);
System.out.print(file.getPath());
workbook = Workbook.getWorkbook(file);
// 使用第一个工作表
sheet = workbook.getSheet(0);
int rows = sheet.getRows();
System.out.printf("总共%d行", rows);
int cols = sheet.getColumns();
System.out.printf("总共%d列", cols);
for (int x = 1; x < rows; x++) {
System.out.printf("第%d行", x);
System.out.println("换行");
for (int j = 0; j < cols; j++) {
System.out.printf("第%d行,第%d列", x, j);
// 注意,这里的 是列放在行前面
Cell xy = sheet.getCell(j, x);
System.out.println(xy.getContents());
out.print(xy.getContents());
// INSERT INTO
// sys_code(code_name,code_value,code_desc,type_name,show_order)
// VALUES('日','日','日','FILTER_WORD',2);
String sql = "INSERT INTO sys_code(code_name,code_value,code_desc,type_name,show_order)";
sql = sql + "values('" + Math.random() * 10000 + "','"
+ xy.getContents() + "','" + xy.getContents()
+ "','FILTER_WORD'," + 33 + ");";
stmt.execute(sql);
}
out.println("</br>");
}
stmt.close();
// DAO.dbjdbc.close();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println(e);
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} else {
out.println("<html><body>重复提交</body></html");
}
}
else
{
out.println("<html><body>没有收到参数啊,别乱刷新</body></html>");
}
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
if (DAO.dbjdbc == null) {
dao.dbconnet();
}
}
@Override
public void destroy() {
// TODO Auto-generated method stub
try {
DAO.dbjdbc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}