简体   繁体   中英

Read Xls file using javascript

I want to make a simple javacript function which can store data from xls files in an array. My code is as below but it returns nothing can anybody help me?

 <!DOCTYPE html> <html lang="en"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script> function GetData(){ var excel = new ActiveXObject("Excel.Application"); var excel_file = excel.Workbooks.Open("C:\\\\testyt.xls"); var excel_sheet = excel.Worksheets("Sheet2"); for(var i=2;i<20;i++){ var myrow = excel_sheet.Range("A"+i); //to read values in row A document.getElementById('div1').innerHTML = myrow; } } </script> <body> <form id="From1" method="post"> <div1 id="div1"> <h2></h2> </div1> <p style="font-size:150%;margin-left:200px; margin-top:60px">Read Data </p> <input type="button" onclick="GetData();"/> </form> </body> </html> </html> 

The only browser that supported ActiveX was IE, and only with very permissive security settings. IE Edge drops ActiveX entirely.

That mean you can't use ActiveXObject anymore unless you're targeting IE11 and earlier on an intranet. You'll either need to do your processing on a server, or use the File API to read the raw xls data and process it in the browser (not trivial!).


In an IE-only intranet environment using up to IE11, with your security settings tuned to allow ActiveX in the Intranet Zone, the ActiveX part might work, but you have a bug in your code. You have this line in the loop:

document.getElementById('div1').innerHTML = myrow;

...which means that you'll fill in that div with the content of row 2, then overwrite it with the content of row 3, then ovewrite that with the content of row 4, etc.

If you want the content of all rows, you'll want multiple divs or similar:

document.getElementById('div' + i).innerHTML = myrow;

(Assumes divs with id s like div2 through div22 .)

Also note that if you're not going to use jQuery, you could not include it; or you could continue to include it, and then use it: $("#div" + i).html(myrow);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM