<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<script language="javascript" type="text/javascript">
function importXLS(fileName)
{
    objCon = new ActiveXObject("ADODB.Connection");
    objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
    objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
    objCon.CursorLocation = 1;
    objCon.Open;
    var strQuery;
    //Get the SheetName
    var strSheetName = "Sheet1$";
    var rsTemp =  new ActiveXObject("ADODB.Recordset");
    rsTemp = objCon.OpenSchema(20);
    if(!rsTemp.EOF)
    strSheetName = rsTemp.Fields("Table_Name").Value;
    rsTemp = null
    rsExcel =  new ActiveXObject("ADODB.Recordset");
    strQuery = "SELECT * FROM [" + strSheetName + "]";
    rsExcel.ActiveConnection = objCon;
    rsExcel.Open(strQuery);
    var str="";
    while(!rsExcel.EOF)
    {
     var i=0
     while(i<rsExcel.Fields.Count)
     {str=str+rsExcel.Fields(i).value+" ";
      i=i+1;
     }
     rsExcel.MoveNext;
    }
    // Close the connection and dispose the file
    objCon.Close;
    objCon =null;
    rsExcel = null;
    alert(str);    
}
</script><input type="file" id="f" />
<input type="button" id="b" value="import" onclick="if(f.value=='')alert('请选择xls文件');else importXLS(f.value)" /></body>
</html>a.xls的内容如下
读取a.xls后如下图:
为什么没有读第一行的7和8,还有字母“d”和中文“来啦”都读不出来呢?
jsexceljavascript读不出html

解决方案 »

  1.   

    “d” 和 “来啦” 被当做数字类型读取了,字符转换数字失败,变成 null
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <script language="javascript" type="text/javascript">
    function importXLS(fileName)
    {
        objCon = new ActiveXObject("ADODB.Connection");
        objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
        objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties='Excel 8.0;HDR=No;imex=1';";
        objCon.CursorLocation = 1;
        objCon.Open;
        var strQuery;
        //Get the SheetName
        var strSheetName = "Sheet1$";
        var rsTemp =  new ActiveXObject("ADODB.Recordset");
        rsTemp = objCon.OpenSchema(20);
        if(!rsTemp.EOF)
        strSheetName = rsTemp.Fields("Table_Name").Value;
        rsTemp = null
        rsExcel =  new ActiveXObject("ADODB.Recordset");
        strQuery = "SELECT * FROM [" + strSheetName + "]";
        rsExcel.ActiveConnection = objCon;
        rsExcel.Open(strQuery);
        var str="";
        while(!rsExcel.EOF)
        {
         var i=0
         while(i<rsExcel.Fields.Count)
         {str=str+(rsExcel.Fields(i).value||'') +" ";
          i=i+1;
         }
         rsExcel.MoveNext;
        }
        // Close the connection and dispose the file
        objCon.Close;
        objCon =null;
        rsExcel = null;
        alert(str);    
    }
    </script><input type="file" id="f" />
    <input type="button" id="b" value="import" onclick="if(f.value=='')alert('请选择xls文件');else importXLS(f.value)" /></body>
    </html>
      

  3.   

    但是我将
    str=str+rsExcel.Fields(i).value+" ";
    换成
    str=str+String(rsExcel.Fields(i).value)+" ";
    后,还是如下输出
      

  4.   

    问题出在 Sql 上
    select * from xxx
    预想返回的结果为
    7,8
    d,2但是,数据库引擎读取第一行时,决定了数据的类型
    int,int
    所以,返回结果为
    7(int),8(int)
    null(转换失败),2(int)