code example for reading local Excel file contents through JavaScript _javascript tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.