0
HttpContext.Current.Response.ContentType = "application/ms-excel"; 
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=Report.xlsx");
HttpContext.Current.Response.TransmitFile(ExcelFilePath);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.End();

Above code downloads excel file in the browser, but in case of pdf files its work as intended & opens it browser itself.

Abu
  • 61
  • 7
  • Possible duplicate of [How to force files to open in browser instead of download (pdf)?](http://stackoverflow.com/questions/6293893/how-to-force-files-to-open-in-browser-instead-of-download-pdf) – Andrew Sun Aug 28 '16 at 15:31
  • Andrew, I have already mention above that it works for pdf files but not for csv or excel files. – Abu Aug 28 '16 at 15:47
  • 2
    In your header you wrote the filename as a PDF. The reason you can open PDF files in your browser is because you have a plugin (commonly preinstalled) that handles it. If the browser doesn't know how to display it directly, the file will be downloaded. Do you have a similar plugin for Excel files installed? – Andrew Sun Aug 28 '16 at 16:14
  • I know that by default PDF files can be opened within browsers because all browsers supports it. And having any Excel plugin installed in my browser doesn't able to solve the problem because its browser level solution not an application level. – Abu Aug 28 '16 at 17:33
  • Your control your application only on the server: the end-user owns the browser. The last thing I'd want is for Excel files to open in my browser. – Tim Williams Aug 28 '16 at 21:21

0 Answers0