⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 用asp把excel数据导入sqlserver数据库中.htm

📁 较为详细的介绍了asp自定义的各种函数,方便asp的各种开发.
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0055)http://www.51base.com/article/view_article.asp?id=72470 -->
<HTML lang=zh-cn><HEAD><TITLE>NB联盟</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type><LINK 
href="NB联盟30.files/style.css" rel=stylesheet>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR></HEAD>
<BODY><!-- start page body -->
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 class=td id=position 
width=773>
  <TBODY>
  <TR>
    <TD class=shadow colSpan=2 height=20 width=262>&nbsp;&nbsp;<A 
      href="http://www.51base.com/"><IMG border=0 
      src="NB联盟30.files/logo1.gif"></A> </TD>
    <TD align=right width=505>
      <P align=left></P></TD></TR></TBODY></TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 id=main width=770>
  <TBODY>
  <TR>
    <TD class=tdlbr vAlign=top>
      <TABLE align=center border=0 cellPadding=0 cellSpacing=0 id=welcome 
      style="TABLE-LAYOUT: fixed" width="98%">
        <TBODY>
        <TR>
          <TD style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all" 
            vAlign=top><FONT color=red><B>文章标题</B></FONT><FONT color=red><B>: 
            用ASP把EXCEL数据导入SQLSERVER数据库中,哪位知道,请接分 </B></FONT>
            <HR noShade SIZE=1>
            <BR>哪位知道在ASP把EXCEL数据导入SQLSERVER数据库中,请给点思路或代码.谢谢啦. 
            &nbsp;<BR>--------------------------------------------------------------- 
            &nbsp;<BR>&nbsp;<BR>没有原代码,给你点提示,你可把EXCEL表当成一个数据库,从数据库里取数。再提交到SQL到保存。如果从EXCEL取数。论坛和技术文档里都有这方面的资料。 
            &nbsp;<BR>--------------------------------------------------------------- 
            &nbsp;<BR>&nbsp;<BR>Set &nbsp;fso &nbsp;= 
            &nbsp;CreateObject("Scripting.FileSystemObject") &nbsp;<BR>Set 
            &nbsp;of &nbsp;= &nbsp;fso.GetFile(Server.mappath(IMPORTDATAFILE 
            &nbsp;&amp; &nbsp;"/" &nbsp;&amp; &nbsp;savedfilename)) 
            &nbsp;<BR>&nbsp;<BR>Set &nbsp;ts &nbsp;= 
            &nbsp;oF.OpenAsTextStream(1, &nbsp;-2) &nbsp;<BR>do &nbsp;while 
            &nbsp;ts.AtEndOfStream &nbsp; &nbsp;&lt;&gt; &nbsp;true &nbsp;<BR>i 
            &nbsp;= &nbsp;i &nbsp;+ &nbsp;1 &nbsp;<BR>&nbsp;&nbsp; &nbsp;&nbsp; 
            &nbsp;&nbsp; &nbsp;&nbsp;strline &nbsp;= &nbsp;ts.ReadLine 
            &nbsp;<BR>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;arrLine 
            &nbsp;= &nbsp;split(strline,",") &nbsp;<BR>值的数组 
            &nbsp;<BR>&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
            &nbsp;&nbsp;sqlinsert &nbsp;= &nbsp;" &nbsp;insert &nbsp;into(...) 
            &nbsp;values(...) &nbsp;" &nbsp;<BR>&nbsp;<BR>&nbsp;&nbsp; 
            &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;oConn.execute(sqlinsert) 
            &nbsp;<BR>loop 
            &nbsp;<BR>--------------------------------------------------------------- 
            &nbsp;<BR>&nbsp;<BR>excel.xsl: &nbsp; &nbsp;<BR>&nbsp;<BR>字段: &nbsp; 
            &nbsp;aa &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bb &nbsp; &nbsp; &nbsp; 
            &nbsp;cc &nbsp; &nbsp;<BR>&nbsp;<BR>值: &nbsp; &nbsp; &nbsp; &nbsp;a1 
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b1 &nbsp; &nbsp; &nbsp; &nbsp;c1 
            &nbsp; &nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
            &nbsp;a2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b2 &nbsp; &nbsp; &nbsp; 
            &nbsp;c2 &nbsp; &nbsp;<BR>&nbsp;<BR>注意,excel中字段名和字段的值都是同样作为数据输入的,只是说 
            &nbsp; &nbsp;<BR>&nbsp;<BR>你把excel表的第一行作为是字段名就是。 &nbsp; 
            &nbsp;<BR>&nbsp;<BR>&nbsp; &nbsp; &nbsp; &nbsp;test.asp &nbsp; 
            &nbsp;<BR>&nbsp;<BR>&lt;% &nbsp; &nbsp;<BR>&nbsp;<BR>dim 
            &nbsp;myconnection &nbsp; &nbsp;<BR>&nbsp;<BR>set 
            &nbsp;myconnection=server.createobject("adodb.connection") &nbsp; 
            &nbsp;<BR>&nbsp;<BR>dim &nbsp;myconnectionstr &nbsp; 
            &nbsp;<BR>&nbsp;<BR>myconnectionstr="Provider=Microsoft.Jet.OLEDB.4.0;Data 
            &nbsp;Source=e:\test\test.x &nbsp; &nbsp;<BR>&nbsp;<BR>ls;Extended 
            &nbsp;Properties=Excel &nbsp;8.0;" &nbsp; 
            &nbsp;<BR>&nbsp;<BR>myconnection.open &nbsp;myconnectionstr &nbsp; 
            &nbsp;<BR>&nbsp;<BR>dim &nbsp;myrecordset &nbsp; 
            &nbsp;<BR>&nbsp;<BR>set 
            &nbsp;myrecordset=server.createobject("adodb.recordset") &nbsp; 
            &nbsp;<BR>&nbsp;<BR>dim &nbsp;mysql &nbsp; 
            &nbsp;<BR>&nbsp;<BR>&nbsp;<BR>&nbsp;<BR>mysql="select &nbsp;* 
            &nbsp;from &nbsp;[sheetname$]" &nbsp; 
            &nbsp;<BR>&nbsp;<BR>myrecordset.open &nbsp;mysql,myconnection,1,1 
            &nbsp; &nbsp;<BR>&nbsp;<BR>do &nbsp;while &nbsp;not 
            &nbsp;myrecordset.eof &nbsp; &nbsp;<BR>&nbsp;<BR>response.write 
            &nbsp;myrecordset("aa") &nbsp;&amp; &nbsp;"&lt;br&gt;" &nbsp; 
            &nbsp;<BR>&nbsp;<BR>myrecordset.movenext &nbsp; 
            &nbsp;<BR>&nbsp;<BR>loop &nbsp; &nbsp;<BR>&nbsp;<BR>set 
            &nbsp;myrecordset=nothing &nbsp; &nbsp;<BR>&nbsp;<BR>set 
            &nbsp;myconnection=nothing &nbsp; &nbsp;<BR>&nbsp;<BR>%&gt; &nbsp; 
            &nbsp;<BR>打印结果: &nbsp; &nbsp;<BR>&nbsp;<BR>a1 &nbsp; 
            &nbsp;<BR>&nbsp;<BR>a2 &nbsp; &nbsp;<BR>然后存入数据库你应该会了吧 
            &nbsp;<BR>--------------------------------------------------------------- 
            &nbsp;<BR>&nbsp;<BR>EXCEL可以当数据库用的 
            &nbsp;<BR>&nbsp;<BR>&nbsp;<BR>  Function &nbsp;CreateExcelRecordset( 
            &nbsp;XLS文件名,Sheet名 &nbsp;) &nbsp;<BR>&nbsp;<BR>  Dim 
            &nbsp;conn.Driver,DBPath &nbsp;<BR>&nbsp;<BR>  ‘ 
            &nbsp;建立Connection对象 &nbsp;<BR>  Set &nbsp;conn &nbsp;= 
            &nbsp;Server.CreateObject("ADODB.Connection") &nbsp;<BR>  Driver 
            &nbsp;= &nbsp;"Driver={ &nbsp;Microsoft &nbsp;Excel &nbsp;Driver 
            &nbsp;(*.xls)};" &nbsp;<BR>  DBPath &nbsp;= &nbsp;"DBQ=" &nbsp;&amp; 
            &nbsp;Server.MapPath( &nbsp;"XLS文件名" &nbsp;) &nbsp;<BR>&nbsp;<BR>  ‘ 
            &nbsp;调用Open &nbsp;方法打开数据库 &nbsp;<BR>  conn.Open &nbsp;Driver 
            &nbsp;&amp; &nbsp;DBPath &nbsp;<BR>  Set &nbsp;CreateExcelRecordset 
            &nbsp;= &nbsp;Server.CreateObject("ADODB.Recordset") &nbsp;<BR>  ‘ 
            &nbsp;打开Sheet,参数二为Connection对象,因为Excel &nbsp;ODBC驱动程序无法直接用 
            &nbsp;<BR>‘sheet名来打开sheet,所以请注意以下的select语句 
            &nbsp;<BR>  CreateExcelRecordset.Open &nbsp;"Select &nbsp;* 
            &nbsp;From &nbsp;["&amp;sheet&amp;”$]”, &nbsp;conn, &nbsp;2, &nbsp;2 
            &nbsp;<BR>&nbsp;<BR>  End &nbsp;Function 
            &nbsp;<BR>--------------------------------------------------------------- 
            &nbsp;<BR>&nbsp;<BR>excel可以把他看作一个数据库,用asp当然只能操作服务器端的数据。 
            &nbsp;<BR>对于客户端是没有办法操作了,当然如果你的客户信任你,你可以做成activex控件来操作客户端,这又是其他的问题了。<BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center border=0 cellPadding=2 cellSpacing=0 id=footer width=770>
  <TBODY>
  <TR>
    <TD align=middle class=tdt>
      <P align=center></P></TD></TR></TBODY></TABLE><BR></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -