如何打开一个Excel文件后,它被下载到JavaScript?
我使用下面的代码下载了一个excel文件(.xls):
JavaScript代码:
window.location = result.filename;
下载后,我想打开一个Excel文件,而不点击它。 我想要自动打开excel文件的JavaScript代码。
我testing了以下function代码。 但它只能在Internet Explorer中运行,而不能在Mozilla,Chrome …
function openExcelFile(strFilePath) { var objExcel; objExcel = new ActiveXObject("Excel.Application"); objExcel.Visible = true; objExcel.Workbooks.Open(strFilePath); }
通过使用上面的代码,excel文件在Internet Explorer中下载后自动打开。
我希望JavaScript代码可以在所有浏览器下载后自动打开excel文件。
我怎样才能做到这一点?
将excel文件path传递给控制器。 它将适用于所有浏览器。
public ActionResult GetFile(string path) { string extension = new FileInfo(path).Extension; if (extension != null || extension != string.Empty) { switch (extension) { case ".xls": return File(path, "application/vnd.ms-excel"); case ".xlsx": return File(path, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); } } return View("Index"); }