JavaScript code to read the contents of a local Excel file:
Copy Code code as follows:
<script type= "Text/javascript" >
function Read_excel () {
var filepath= "D:\abcd9.com.xls";//XLS to read
var sheet_id=2;//Read 2nd table
var row_start=3; Read
var tempstr= ';
From line 3rd try{
var oxl = new ActiveXObject ("Excel.Application "); Create Excel.Application Object
}catch (err)
{
alert (ERR);
}
var owb = OXL.Workbooks.open (FilePath);
owb.worksheets (sheet_id). Select ();
var osheet = Owb.activesheet;
var colcount=oxl.worksheets (sheet_id). UsedRange.Cells.Rows.Count;
for (var i=row_start;i<=colcount;i++) {
if ( typeof (Osheet.cells (i,8). Value) = = ' Date ') {////= read problem with date format for part 8th column cell content
d= New Date (Osheet.cells (i,8). value);
temp_time=d.getfullyear () + "-" + ( D.getmonth () + 1) + "-" +d.getdate ();
}
Else
Temp_time=$.trim (Osheet.cells (i,7). value.tostring ());
tempstr+= ($.trim (Osheet.cells (i,2). Value) + "" +$.trim (osheet.cells (i,4). Value) + "" +$.trim (Osheet.cells (i,6). Value.tostring ()) + "" +temp_time+ "\ n");
/Read 2nd, 4, 6, 8 column contents
}
return tempstr; Return
Oxl.quit ();
CollectGarbage ();
}
</script>
Must condition:
1. The client must have the Microsoft Excel ActiveX control installed (complete with Microsoft Office installed) and be allowed to run when the browser pops up "This site needs to run the following add-in ..." Otherwise, the JS code will have an error creating the Excel.Application object.
2, JS read the local Excel file involves security issues, the default settings Microsoft Excel ActiveX control does not have permission to execute. The solution is as follows:
A. Click the browser "Tools"-> "Internet Options"-> "secure" and select "Trusted Sites".
B. Click on the "site (S)" button to add this site to the list;
C. Click Custom Level (c) ..., and in the security settings, locate the ActiveX controls that are not marked as safe for scripting and execute scripts under the ActiveX controls and plugins node to set to Enabled.