lyn38099
lyn38099
2017-10-28 13:43

JavaScript读取Excel表格的问题

  • excel
  • echarts 数据
  • 异步
  • javascript

function importXLS(field){
var fullpath = window.document.location.pathname;
var pos1 = fullpath.indexOf("/");
var fileName1 = fullpath.substring(pos1+1);
var pos2 = fileName1.lastIndexOf("/");

   var fileName = fileName1.substring(0,pos2) + "E:/IO.xlsx";// xls文件的路径  
   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$"; //要导入的sheet的名字  

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 res = [];

var temp;

while(!rsExcel.EOF){

temp = rsExcel.Fields(field).value;
res.push(temp);

rsExcel.MoveNext;

}

// Close the connection and dispose the file

objCon.Close;

objCon =null;

rsExcel = null;

return res;

}

** 以上是加载Excel数据的函数,下面是调用过程:**

var xData = importXLS(1);
var yData = importXLS(2);
var xyData = [];
var i;
//转化为散点图需要的二维数组系列
for(i=0;i<xData.length;i++){
xyData.push(new Array(xData[i],yData[i]));
}

    **目的是将excel中的前两列作为一个二维数组读取。但是调用过程出错,各位大神帮忙看一下importXLS函数有什么问题吗?或者下面这几句代码对本地电脑有什么要求?**
        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; 

        先在此谢过了!!![图片说明](https://img-ask.csdn.net/upload/201710/28/1509198168_737188.png)
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答