当前位置: 代码迷 >> ASP >> ASP导入EXCEL出错,该怎么解决
  详细解决方案

ASP导入EXCEL出错,该怎么解决

热度:83   发布时间:2012-04-05 12:42:40.0
ASP导入EXCEL出错
网上下的一个ASP导入EXCEL的程序,现在有一个EXCEL导入时发生错误,没有具体的提示。把SQL打印出来,大家帮我看看有什么问题。
Insert into [ltx] (月份,姓名,工资卡号,身份证,离休金,离休人员生活补贴,离休人员护理费,离休人员交通费,离休人员其他,退休金,退休人员生活补贴,退休人员职务岗位津贴,退休人员综合物价补贴,退休人员地方岗位补贴,退休人员护理费,退职费,退休职务工资,退休级别工资,退休警衔津贴,退休人员其他,水电费,房租费,其他扣款一,其他扣款二) values ('2012-1-1','丁为民','842134921269601075','320924497719527','0','0','0','0','0','2116','2900','0','0','0','0','0','0','0','0','0','20','0','15','0')

关掉了屏蔽错误,但只出现一个错误提示,最下边一行是
Error
其它什么也没有。

------解决方案--------------------
如果字段是数字类型 insert语句中 字段对应的值的两边不要加单引号 直接写数字。
------解决方案--------------------
1,FSO生成EXCEL,并从ACCESS数据库中查询出数据之后写入EXCEL中;
[1],首先连接并打开ACCESS数据库,并查询出想要导出的数据;
[2],建立FSO对象,在通过FSO对象(以合理的覆盖和合理的字符编码,中文用unicode码)建立EXCEL文件对象;
[3],然后想EXCEL写入数据,向EXCEL中写入数据时分两个步骤,首先写入各列的标题,也即ACCESS的字段名称;
[4],然后写入数据部分,把每个记录作为一行写入EXCEL,一直循环写入所有的记录完为止;
[5],注意每个字段值之间连接一个制表符(Tab键),用代码表示chr(9),这样在EXCEL中写入时下一个值自动跳到一个输入格;
[6],注意每个行最后写入一个回车,代码chr(13)表示,这样在EXCEL中写入完一行后才会另起一行输入,否则会一直在一行往后输入; 

总方向, 通过FSO对象建立EXCEL文件, 然后向ACCESS文件查询出相关数据再写入到EXCEL文件中;
该方法建立的EXCEL文件物理存在服务器端提供客户端下载;

如下程序参考(以前写的):

VBScript code

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
'FUNCTION:(TN) QUERY DATA FROM ACCESS DB AND INSERT THEM INTO EXCEL WHICH CREATED NEWLY BY FSO OBJECT
dim msg,exlFileName
msg=""
'exlFileName=trim(request.Form("weFile01"))  'get file name from client
exlFileName="ExcelFileName001"
err.clear 

if exlFileName<>"" then
    '--------------------------------------------------------
    'Connect ACCESS DB and then query data from table
    dim acDBName,objDB,strConn
    acDBName="DBS/DB01.mdb"
    strConn="DRIVER={microsoft access driver (*.mdb)};UID=;PWD=;DBQ="&server.MapPath(acDBName)
    set objDB=server.CreateObject("adodb.connection")
    objDB.open strConn
    
    dim objRS,qSQL
    qSQL="select * from pnAddress"
    set objRS=server.CreateObject("adodb.recordset")
    objRS.open qSQL,objDB,1,1
    '--------------------------------------------------------
    
    '--------------------------------------------------------
    'Create EXCEL file and write (query)data to it
    if objRS.bof and objRS.eof then
        msg="<font color=""red"">No data in ACCESS DB, and not create EXCEL file.</font>"
    else
        dim objFSO,objExcel,saveExlFileName,strTemp,tempA,i
        
        'create EXCEL file name include path
        saveExlFileName=""
        saveExlFileName="Excels/"&exlFileName&year(now())    '可以使用程序生成保存的路径或文件夹
        if len(month(now()))<2 then
            saveExlFileName=saveExlFileName&"0"&month(now())
        else
            saveExlFileName=saveExlFileName&month(now())
        end if
        if len(day(now()))<2 then
            saveExlFileName=saveExlFileName&"0"&day(now())
        else
            saveExlFileName=saveExlFileName&day(now())
        end if
        if len(hour(now()))<2 then
            saveExlFileName=saveExlFileName&"0"&hour(now())
        else
            saveExlFileName=saveExlFileName&hour(now())
        end if
        if len(minute(now()))<2 then
            saveExlFileName=saveExlFileName&"0"&minute(now())
        else
            saveExlFileName=saveExlFileName&minute(now())
        end if
        saveExlFileName=saveExlFileName&".xls"
        
        on error resume next
        'create FSO and create EXCEL file, exist then delete and create it
        set objFSO=server.CreateObject("scripting.FileSystemObject")
        if objFSO.fileExists(server.MapPath(saveExlFileName)) then
            objFSO.deleteFile(server.MapPath(saveExlFileName))
        end if
        set objExcel=objFSO.createTextFile(server.MapPath(saveExlFileName),true,true)
        '以覆盖(第一个true)和以unicode(第二个true)编码方式建立文件,有中文内容所以需要以unicode方式建立文件

        
        'create HEAD of EXCEL file, HEAD data query from ACCESS DB
        for i=0 to objRS.fields.count -1
            tempA=tempA & objRS.fields(i).name & chr(9)        'chr(9) 为制表符 Tab 键
        next
        objExcel.write tempA & chr(13)        'Atten: make a enter in the end of line, 每行完注意回车
        
        
        'create BODY of EXCEL file, BODY data query from ACCESS DB
        do while not objRS.eof
            tempA=""
            for i=0 to objRS.fields.count-1
                tempA=tempA & objRS.fields(i).value & chr(9)
            next
            objExcel.write tempA        'write a recod, 写入一条记录
            objExcel.write chr(13)        'Atten: make a enter in the end of line, 每行完注意回车
            objRS.movenext
        loop
        
        'close OBJECT and release system source
        objExcel.close        
        set objExcel=nothing
        'objFSO.close        'Atten: (TN)带开的文件对象没有 close 方法, 可以直接设置为空,
        set objFSO=nothing    
        msg="ok"
    end if
    '--------------------------------------------------------
    objRS.close                
    set objRS=nothing
    objDB.close
    set objDB=nothing
else
    msg="<font color=""red"">File name is empty and not create EXCEL file.</font>"
end if

'Display error information if go wrong
if err.number<>0 then 
    response.Write "<br><font color=""red"">Error code:</font> " & err.number
    response.Write "<br><font color=""red"">Error description:</font> " & err.description 
    response.Write "<br><font color=""red"">Error source:</font> " & err.source 
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>FSO建立并增加数据到EXCEL中</title>
</head>
<style type="text/css">
body,td,div,input,fieldset,textarea{font-family:Verdana; font-size:11px; color:#333333; font-weight:normal;}
a:link,a:visited{font-family:Verdana; font-size:11px; color:#333333; font-weight:normal; padding:0px 3px; line-height:20px; text-decoration:none;}
a:hover,a:active{font-family:Verdana; font-size:11px; color:#FF6600; font-weight:normal;}
span{font-family:Verdana; font-size:11px; color:red; font-weight:bold; padding-left:5px; margin:0px 10px;}
</style>
<body>
<p>&nbsp;</p>
<p>
<%
if msg="ok" then
    response.Write "<b>OK! EXCEL FILE CREATED:</b>"
    response.Write "<hr size=""1"" color=""#FF6600"">"
    response.Write "Click and download <a href=""" & saveExlFileName & """><b>" & saveExlFileName & "</b></a>"
else
    response.Write "<b>Tip:</b><br>" & msg 
end if
%>
</p>
<br><br>
<div id="tip" style="text-align:right;"><a href="excels"><b>EXCEL文件管理</b></a></div> 
</body>
</html>
 
  相关解决方案