简体   繁体   English

用javascript下载后如何打开Excel文件?

[英]How to open a excel file after it gets downloaded in javascript?

I download an excel file (.xls) by using below code: 我使用以下代码下载了一个Excel文件(.xls):

JavaScript Code: JavaScript代码:

window.location = result.filename;

After downloading, I want to open a excel file automatically without clicking on it. 下载后,我想自动打开一个excel文件而不点击它。 I want JavaScript code for opening excel file automatically. 我想要自动打开excel文件的JavaScript代码。

I tested with following function code. 我使用以下功能代码进行了测试。 But it runs only in Internet Explorer, not in Mozilla, Chrome... 但是它只能在Internet Explorer中运行,而不能在Mozilla,Chrome中运行...

function openExcelFile(strFilePath) {
var objExcel;
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = true;
objExcel.Workbooks.Open(strFilePath);
}

By using above code, the excel file open automatically after it gets downloaded in Internet Explorer. 通过使用上述代码,excel文件在Internet Explorer中下载后会自动打开。

I want JavaScript code for opening excel file automatically after downloaded by working in all browsers. 我希望通过下载所有浏览器后都能自动打开Excel文件的JavaScript代码。

How can I achieve this? 我该如何实现?

pass the excel file path to the controller. 将excel文件路径传递给控制器​​。 it will works fine with all browsers. 它将在所有浏览器上正常工作。

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM