write html file that contains a link that open exc

2019-08-07 00:30发布

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

标签: html excel href
2条回答
Emotional °昔
2楼-- · 2019-08-07 01:09

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>
查看更多
▲ chillily
3楼-- · 2019-08-07 01:10
<HTML>
<HEAD>
<Title>Excel Linking Example</Title>
</HEAD>
<body>
<p>
<a href="http://localhost/excel/asheet.xls#Sheet2!D4">
This link will open the Excel file to the second page with the focus on
cell D4</a>.
<a href="http://localhost/excel/asheet.xls#TableName">
This link will set the focus on a named area of the spreadsheet
</a>.
</p>
<form>
<input type=button
 value="Via Jscript"
 onclick='location.href = "asheet.xls#TableName"'>
</form>
</body>
</html>

source: http://support.microsoft.com/kb/197922

查看更多
登录 后发表回答