網頁連結直接開啟 Word/Excel 檔
source link: https://blog.darkthread.net/blog/launch-word-excel-from-web/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
網頁連結直接開啟 Word/Excel 檔
2021-02-19 08:45 PM 1 3,659SharePoint 有項蠻酷功能,文件庫有個「在 Word/Excel 中編輯」連結,點下去可直接啟動 Word 或 Excel 開啟檔案:
反觀一般在網頁放 .docx、.xlsx 檔案 <a> 連結,都得儲存成本機檔案再開啟,流暢度輸了一截,讓我很不甘心,於是便研究了原理,發現我們也能辦到:
祕訣是將 href 網址改成「ms-word:ofv|u|可下載docx的網址」,例如:
- ms-word:ofv|u|http://localhost/aspnet/officefiles/%E6%96%87%E4%BB%B6.docx
- ms-excel:ofv|u|http://localhost/aspnet/officefiles/%E5%B7%A5%E4%BD%9C%E8%A1%A8.xlsx
這種格式的專業術語叫 Office URI Schemes,規則為:
< scheme-name >:< command-name >"|"< command-argument-descriptor > "|"< command-argument >
- scheme-name 指定要啟動的軟體,Excel 為 ms-excel,Word 為 ms-word。
- command-name 決定動作,ofv 為唯讀開啟或檢視模式,ofe 則為編輯模式。
- command-argument-descriptor 固定傳 "u"
- command-argument 下載檔案內容的 URL。
所以,只需在下載 docx/xlsx 的 URL 前面加上 ms-word:ofv|u| 或 ms-excel:ofv|u| 並換成絕對網址即可,完整範例如下:
<%@Page Language="C#"%>
<script runat="server">
string GetAbsUrl(string fileName)
{
return new Uri(Request.Url, fileName).AbsoluteUri;
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<ul>
<li><a href="ms-word:ofv|u|<%=GetAbsUrl("文件.docx")%>">文件.docx</a></li>
<li><a href="ms-excel:ofv|u|<%=GetAbsUrl("工作表.xlsx")%>">工作表.xlsx</a></li>
</ul>
</body>
</html>
最後補充一個眉角,如果 Office 檔案是靠 ASP.NET MVC 加上參數傳回,例如 /ControllerName/Download?f=fileName.docx,實測 URL 檔名結尾若不是 .docx、.xlsx 等副檔名,Word/Excel 開啟會出錯,故需使用 MVC 路由功能讓下載網址改為 .xlsx/.docx 結尾,但要注意檔名結尾 URL 導致 MVC 路由失效問題,web.config 要稍做調整。
Recommend
-
9
window.open() 有時在新頁籤有時在新視窗開啟-黑暗執行緒 有某段用 window.open() 開啟新網頁的 JavaScript 程式,在 IE 如預期開出新網頁,但在 Chrome 裡卻開啟在新頁籤,害我被迷惑了一陣子,做完研究整理筆記備忘。 測試程式如下:
-
8
開啟瀏覽器檢視本機 HTML 檔及瀏覽網站-黑暗執行緒 這是我開發 PowerShell 小工具的常用技巧 - 將複雜的執行結果以 HTML 形式呈現,遠比用 Write-Output 輸出單調文字容易理解閱讀。例如:之前介紹的
-
6
數據化營運,開啟全員增長 – 2021 成長駭客年會 加入FB社團,乾貨不漏接 GO 跳至主要內容...
-
4
將 Word 內容直接轉成 GIGO 網頁輸入介面-黑暗執行緒 系統有張表單包含一個使用者自訂區塊,允許業務單位放入備註性質資料讓使用者填寫,該備註只對承辦人員有意義,與作業程序完全無關,且各單位所而內容不同,偶爾會調整。思考了一下,我打算將其視為...
-
2
美美的 FB/LINE 連結預覽是如何產生的?-黑暗執行緒 FB 是當今網路行銷不可或缺的社群媒體。大家應該有注意到,在 FB 分享網頁有些網站 URL 會產生包含圖片、標題及摘要說明的精美預覽區塊[1],有些則只有單調的網站名稱及標題[2]:
-
7
微軟的 Outlook 系統會自動點擊信件內的連結 前幾天在 Hacker News Daily 上翻到的,微軟的 Ou...
-
5
V2EX › 问与答 m1 air,佳能 1dx2 連結 capture one 無反應
-
2
反向連結是指「從其他網站連到我們網站的連結」,是 SEO 重要的排名因素之一,擁有優質的反向連結能提升網站曝光與 SEO 排名。你知道你的網站有多少反向連結嗎?這些反向連結的品質好嗎?可以額外買反向連結嗎?這篇文章將會和你如何評估反向連結的好壞、以及反向連...
-
5
X/Twitter 在惡搞外部連結結果被抓包玩陰的 這算一個歷史記錄,現在已經改回來了... 在 Hacker News...
-
6
蒐集 Hacker News 上被不合理條件「消失」的連結 在「Stories removed from the Hacker Ne...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK