关于aspsmartupload组件的使用 以及 Excel汇入

来源:互联网 发布:ubuntu手机刷机包 编辑:程序博客网 时间:2024/06/06 02:11

倒霉的该死的aspsmartupload,竟然连官网都没有了。可是更倒霉的是竟然还要用它来实现上传excel后汇入数据到SQL server。太辛苦了,所以一定要把辛苦的历程记录下来。

先贴代码:

A.asp

function  uploadDailySchedule()
{

//开始打开对话框上传文件[选择excel文件,后面要汇入]
    if (showModalDialog('B.asp?Form_No=<%=Request("Form_No")%>',window,'resizable:yes;status:no;dialogWidth:400px;dialogHeight:150px')=="R")
    {

    //从Exce汇入到SQL Server

    strurl="D.asp?Form_No=<%=Request("Form_No")%>";
    var objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    objXmlHttp.Open("POST",  strurl,false);
    objXmlHttp.Send();

    saveForm_Locale();//any fuction here
    }
}

B.asp

<%@CODEPAGE=65001%><%Response.Charset="UTF-8"%>

<script type="text/javascript">
    function uploadSchedule()
    {
        upload.action = "C.asp"
        upload.submit();
        window.returnValue = "R";
        window.close();
    }
</script>

<html>
<head><title>Upload Detailed Schedule</title></head>
<body>
<form name="upload"  method="post" enctype="multipart/form-data">

        <input type="hidden" name="hidFormNo" value='<%=Request("Form_No")%>' />
        <table width="100%">
        <tr>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td><input style="width:100%;" type="file" id="inputExcelUpload" name="fileExcel01"/></td>
        </tr>
        <tr>
            <td align="center">
                <input type="button" value="Upload Daily Schedule" onclick="uploadSchedule();"/>
            </td>
        </tr>
        </table>
</form>
</body>
</html>

C.asp

<%@CODEPAGE=65001%><%Response.Charset="UTF-8"%>
<%
    set objUpload = server.CreateObject("aspSmartUpload.SmartUpload")
    objUpload.Upload     //这里就是上传文件了

   
    strFile= Server.MapPath("./") & "/ExcelFile/"
    strFile = strFile & objUpload.Form("hidFormNo").Values & "_UploadSchedule.xls"

    objUpload.Files(1).SaveAs(strFile)//存储Excel文件
   
    set objUpload = nothing
      
%>
<script type="text/javascript">
    window.opener=null;window.open('','_self','');window.close();
</script>

 D.asp

 <%@CODEPAGE=65001%><%Response.Charset="UTF-8"%>
 <%
 
 dim strFile
 strFile= Server.MapPath("./") & "/ExcelFile/"
 strFile = strFile & Request("Form_No") & "_UploadSchedule.xls"


 
 
 dim myConnection
 set myConnection = Server.CreateObject ("ADODB.Connection")
 
 myConnection.open "Driver={Microsoft Excel Driver (*.xls)};DBQ="&strFile
 
 dim rsXsl
 set rsXsl=Server.Createobject("ADODB.Recordset") 
 
 dim str_Xsl
 str_Xsl="select * from [Sheet1$]" 
 
 rsXsl.open str_Xsl,myConnection
  
 set sqlConnection = Server.CreateObject ("ADODB.Connection")
 sqlConnection.Open Session("ConnectionString")
 
 sqlDeleteOldData = "delete from DetailSchedule where FormNo=" &request("Form_No")
 sqlConnection.execute(sqlDeleteOldData)

 i = 1
 Do While not rsXsl.eof
    sqltext="insert   into   DetailSchedule (FormNo,DetailLineNo,DetailDate,DetailBegin,DetailEnd,DetailTravelLine,DetailPlace,DetailAttendees) values("_
            &request("Form_No")&","_
            &i&",N'"_
            &rsXsl.Fields(0).Value&"',N'"_
            &rsXsl.Fields(1).Value&"',N'"_
            &rsXsl.Fields(2).Value&"',N'"_
            &rsXsl.Fields(3).Value&"',N'"_
            &rsXsl.Fields(4).Value&"',N'"_
            &rsXsl.Fields(5).Value&"')" 
            i = i+1
    sqlConnection.execute(sqltext)  

    rsXsl.movenext   
    Loop
       
 sqlDeleteEmptyData = "delete from BQYFORMT091_DetailSchedule where DetailDate='' and DetailBegin='' and  FormNo=" &request("Form_No")
 sqlConnection.execute(sqlDeleteEmptyData)      
 
set   myConnection= nothing
set   rsXsl= nothing
set   sqlConnection= nothing

%>

 

总结一下注意点:

1,smartupload组件获取参数使用的是form.name,如:C.asp中的objUpload.Form("hidFormNo").Values,所以在B.asp中就要用name,而且不能用id,貌似用了id就完蛋(相当于没有,苍天啊大地,为了这个浪费了1天一夜[晚上睡不着]).

2,excel汇入的时候注意连接字符串的形式:

myConnection.open "Driver={Microsoft Excel Driver (*.xls)};DBQ="&strFile

很faint为什么myConn_Xsl="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& strFile &";Extended Properties=Excel8.0" 不可以

以及打开连接的方式:

rsXsl.open str_Xsl,myConnection

貌似不能使用rsXsl.open str_Xsl,myConnection,1,1的方式,因为这种1,1的方式下容易rsXsl.eof,下面就不好循环获取行数据了

3,window.close在IE 7下面有问题,老会弹出提示框问是否要关闭,所以只能这样抄过来:

window.opener=null;window.open('','_self','');window.close();

 

 

另外,好像showmodaldialog用得不够好window.dialogArguments偶没用到,下次用一下。

原创粉丝点击