2

Can anyone please suggest any idea about how to open an Excel file, kindly check below code and let me know how to achieve opening the instead of downloading the file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">

function read()  
{  
     var txtFile = new XMLHttpRequest();  
     txtFile.open("GET", "C:\Users\user-temp\Downloads\test.xlsx", true);  
     txtFile.onreadystatechange = function()   
     {  
          if (txtFile.readyState === 4)   
          {  
               // Makes sure the document is ready to parse.  
               if (txtFile.status === 200)   
               {  
                    // Makes sure it's found the file.  
                    document.getElementById("div").innerHTML = txtFile.responseText;  
               }  
          }  
     }  
     txtFile.send(null)  
}
</script>
</head>
<body onload="read();">  
<form id="form1" runat="server">  
   <div id="div">  
   </div>  
</form>  
4
  • Excel is not a web browser component, so what do you mean by "open", and what sort of "Excel file", CSV? some version of XLS, XLSX? Commented May 17, 2021 at 5:44
  • Hi Jasen, Thanks for your quick reply, while opening the file i need to always select an first option "Open With" it's should automatically open the file Commented May 17, 2021 at 6:43
  • 1
    You may find this difficult to do for security reasons. Browsers explicitly limit what can be done with external files that are retrieved from the internet to restrict the chance of some malicious code from being executed. Commented May 17, 2021 at 22:22
  • Hey Jon, Thanks for your quick response, here the problem is excel file which we trying upload is being password protected, so my logic was while we trying to upload an excel file with password protected the excel file 1)should open using XMLHttpRequest.2) then we need to provide the valid passoword with authentication 3) Finally the file should read, so please correct me if i am wrong, meantime is there any other approach kindly let me know if any plugin or some idea, it's really very helpful for me Commented May 19, 2021 at 5:02

1 Answer 1

2

in your case you can use following methods
with google viewer iframe:

<iframe src="http://docs.google.com/gview?url=https://sifr.in/img/292/1/courseAndroid.xlsx&embedded=true"></iframe>

or with microsoft viewer iframe:

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=https://sifr.in/img/292/1/courseAndroid.xlsx'></iframe>

or open it in another tab / window with following:

<a href="http://docs.google.com/gview?url=https://sifr.in/img/292/1/courseAndroid.xlsx&embedded=true">Open your excel file</a>

credit must also go to
this guy

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.