加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将excel中的数据导入数据库

发布时间:2020-12-17 08:00:55 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 % Response.CodePage=65001%% Response.Charset="UTF-8" %%wenjian = request.Form("select")'获取文件扩展名ext = FileExec(wenjian)'判断文件扩展名

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

<% Response.CodePage=65001%>
<% Response.Charset="UTF-8" %>
<%
wenjian = request.Form("select")

'获取文件扩展名
ext = FileExec(wenjian)
'判断文件扩展名
if ext <> "xls" then
	response.Write("<script>alert('文件类型不对,请核实!');window.location.href='index.html';</script>")
	response.End()
end if

Dim objConn,objRS
Dim strConn,strSql

set objConn=Server.CreateObject("ADODB.Connection")
set objRS=Server.CreateObject("ADODB.Recordset")

excelFile = server.mappath(wenjian) 
'针对excel 2007
strConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & excelFile & ";" & "Extended Properties=Excel 8.0;"
objConn.Open strConn

strSql="SELECT * FROM [Sheet1$]"

objRS.Open strSql,objConn,1,1
objRS.MoveFirst

%><!--#include file="conn.asp"--><%
'循环excel中所有记录
while not objRS.eof

	set rs = Server.CreateObject("Adodb.Recordset")
	'查询语句
	sql_s = "select * from ceshi where lname='" & objRS(0) & "' and old='" & objRS(1) & "' and sex='" & objRS(2) & "' and guojia='" & objRS(3) & "' and QQ='" & objRS(4) & "'"
	rs.open sql_s,conn,1
	'重复的数据不做录入操作
	if rs.eof then
		'插入语句
		'****excel中第一条不会被录入****
		sql = "insert into ceshi (lname,old,sex,guojia,QQ)values ('" & objRS(0) & "','" & objRS(1) & "','" & objRS(2) & "','" & objRS(3) & "','" & objRS(4) & "')"
		'执行插入
		conn.execute(sql)
	end if
	objRS.MoveNext	
	rs.close
	set rs = nothing
wend

'又到了各种关闭的时候
conn.close
set conn = nothing
objRS.Close
objConn.Close
set objRS = Nothing
set objConn = Nothing

response.Write("<script>alert('导入成功');window.location.href='index.html';</script>")
response.End()

Function FileExec(fileName)
  FileExec = Mid(fileName,Instr(fileName,".")+1,Len(fileName)-Instr(fileName,"."))
End Function
%>

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读