Search code examples
htmlexcelhref

write html file that contains a link that open excel application in a specific sheet & row


I'm writing a very simple HTML file that contains some tables. I'm trying to have a cell value, which is basically a link. by clicking on the link I'd like to open a file in an excel application in a specific sheet and row.

Notes:

  1. windows environment
  2. HTML is opened by a standard browser
  3. the file exists locally (simple path: C:\test.xlsx)
  4. excel application path is unknown
  5. I'd like to keep the HTML file as simple as possible. good design is not a top priority. just need to make it happen.
  6. (low priority) if excel instance (for a specific file) is already open, I'd like to change the active sheet and highlight the row in the open instance

Solution

  • I solved it using javascript:

    <script type="text/javascript">
        function open_excel_file(path,sheet,f_range)
          {
    
            if (!window.ActiveXObject)
            {
              alert ("Your browser does not support this feature.\n"
                     "Please re-open this file using IE browser.");
              return;
            }
    
            fso = new ActiveXObject("Scripting.FileSystemObject");
    
            if (!fso.FileExists(path))
              alert("Cannot open file.\nFile '" + path + "' doesn't exist.");
    
            else
             {
               var myApp = new ActiveXObject("Excel.Application");
    
               if (myApp != null)
                 {
                   myApp.visible = true;
                   Book = myApp.workbooks.open(path);
                   var excel_sheet = Book.Worksheets(sheet).Activate;
                   myApp.range(f_range).Select;
                 }
    
               else {
                 alert ("Cannot open Excel application");
               }
             }
          }
     </script>
    

    usage example:

    <button onclick="open_excel_file('f1.csv', 's1', 'A1:Z7');">Open</button>