问题
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:
- windows environment
- HTML is opened by a standard browser
- the file exists locally (simple path: C:\test.xlsx)
- excel application path is unknown
- 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.
- (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
回答1:
<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
回答2:
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>
来源:https://stackoverflow.com/questions/14487483/write-html-file-that-contains-a-link-that-open-excel-application-in-a-specific-s