1

I'm pretty new to JavaScript (and coding for that matter). The problem is the following.

I'm looking for a way to call an Excel file and set some filters in the document when executed. So the requested file would pop up as specified. This is supposed to work on Windows 8.1.

ActiveX apparently does not work. Does anybody have an idea how to makes this work? Is there a substitute for using ActiveX Objects?

Any help is much appreciated.

Below the code I have so far:

var sId = "1989";

var app   = "Excel.Application";
var path  = "C:\\Temp\\UmsatzGL.xls";
var sheet = "Output";
var range = "FilterWE";
var filterdefault = "(Alle)";

return_wbook = "T";
return_sheet = "T";
return_range = "T";

var UsrLanguage = "de";

switch (UsrLanguage) {
  case "de":
    alert_wbook = 'Excelmappe ' +path+' kann nicht gefunden werden!';
    alert_sheet = 'Arbeitsblatt ' +sheet+' ist nicht vorhanden!';
    alert_range = 'Bereich ' +range+' ist nicht vorhanden!';
    alert_content = 'Kunde ' +sId+' ist nicht vorhanden!';
    break;
  case "en":
    alert_wbook = 'Excel-File ' +path+' does not exist!';
    alert_sheet = 'Worksheet ' +sheet+' does not exist!';
    alert_range = 'Range ' +range+' does not exist!';
    alert_content = 'Customer ' +sId+' does not exist!';
    break;
  default:
    alert_wbook = 'Excel-File ' +path+' does not exist!';
    alert_sheet = 'Worksheet ' +sheet+' does not exist!';
    alert_range = 'Range ' +range+' does not exist!';
    alert_content = 'Customer ' +sId+' does not exist!';
    break;
}

function my_get_app(app){
try{return GetObject('',app)}
catch(e){return my_start_app(app)}
}

function my_start_app(app){
return new ActiveXObject(app)
}

function my_get_wbook(path){
try{return excel.Workbooks.Open(path)}
catch(e){return_wbook = "F";}
}

function my_get_sheet(sheet){
try{return excel.Worksheets(sheet)}
catch(e){return_sheet = "F";}
}

function my_get_range(range){
try{return excel_sheet.range(range)}
catch(e){return_range = "F";}
}

var excel = my_get_app(app);
var excel_file = my_get_wbook(path);
var excel_sheet = my_get_sheet(sheet);
var excel_range = my_get_range(range);

excel.displayalerts = false;

if (return_wbook == "F") {
                          alert(alert_wbook);
                          excel.quit();
  } else if (return_sheet == "F") {
                             alert(alert_sheet);
                             excel.quit(); 
    } else if (return_range == "F") {
                                alert(alert_range);
                                excel.quit();
                                    }

if (return_wbook != "F" && return_sheet != "F" && return_range != "F") {
        excel_range.value = filterdefault; 
        try{
            excel_range.value = sId;
            excel.Visible=true;
            excel_sheet.Activate;
           }
        catch(e){
                 alert(alert_content);
                 excel.quit();
                }
    } 

close();
}
1
  • At a quick glance, I can't see anything wrong - it should be noted however, that ActiveXObject will only work in Internet Explorer. Is this the browser you're using? Commented Mar 27, 2015 at 12:59

1 Answer 1

1

I don't have much pretty idea what is your problem. But i can refer a plug-in which could give you a push.

https://github.com/SheetJS/js-xlsx

I have found a sample code on Javascript Excel OpenFile

<html>

  <body>

    <form name="form1">
      <input type=button onClick="test()" value="Open File">
      <br><br>
    </form>

    <script type="text/javascript">
      function test() {
        var Excel = new ActiveXObject("Excel.Application");
        Excel.Visible = true;
        Excel.Workbooks.Open("teste.xlsx");
      }
    </script>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.