Javascript code to read the contents of local Excel file:
The code copy is as follows:
<script type="text/javascript">
function read_excel(){
var filePath="D:/abcd9.com.xls"; //xls to be read
var sheet_id=2; //Read the second table
var row_start=3; //Read from line 3
var tempStr='';
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 coloncount=oXL.Worksheets(sheet_id).UsedRange.Cells.Rows.Count;
for(var i=row_start;i<=colcount;i++){
if (typeof(oSheet.Cells(i,8).value)=='date'){ //Reading problems when the content of the cell in column 8 is in the date format.
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 the contents of columns 2, 4, 6 and 8
}
return tempStr; //Return
oXL.Quit();
CollectGarbage();
}
</script>
Required conditions:
1. The client must install the Microsoft Excel activex control (install the full version of Microsoft office), and allow it to run when the prompt pops up with the browser "This website needs to run the following add-ins...", otherwise the js code will be An error occurred while creating an Excel.Application object.
2. JS reads local Excel files with security issues. Under the default settings, Microsoft Excel activex controls do not have execution permissions. The solution is as follows:
a. Click on the browser "Tools" -> "Internet Options" -> "Security" and select "Trust Site".
b. Click the "Site (S)" button to add this website to the list;
c. Click "Custom Level (C)..." and find "Initialize and execute scripts for ActiveX controls that are not marked as safe to execute scripts" under the "ActiveX Controls and Plug-ins" node in the security settings to set it for "Enable".