7

I have an Excel Spreadsheet (.xls) which i want the users to be able to download it from my webpage. Maybe a 'download' button on the page and when users click on it, the file will be downloaded.

May i know how can this be done? Code snippets and references will be appreciated.

Naveed
  • 41,517
  • 32
  • 98
  • 131
Lloydworth
  • 743
  • 6
  • 20
  • 38
  • Why don't you just link to the file? Preferably, you should be serving it with the correct MIME type, but it should work regardless... – Joseph Silber Jan 06 '12 at 02:31

6 Answers6

14

You can use simple html links:

link:

<a href='/path/to/excel/file.xls' target="_blank">Download</a>

button:

<form>
<input type="button" value="Download" onClick="window.location.href='/path/to/excel/file.xls'">
</form>
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • is it possible to use a relative link when trying to download a file locally? using `click` tries to load a file at `file:///C:/docs/my_file.xlsx`, rather than looking in the local website directory. – user1063287 Feb 15 '20 at 05:48
1

I think this should be fine too:

<a href="path-to-file" download="abc.xls">Download</a>

download: name of file to be downloaded as.

1

How about just referring to the file through an anchor?

<a href="path-to-file.xls">Download</a>
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
  • I am using anchor but it's not downloading the file :( https://stackoverflow.com/questions/46876439/excel-file-download-failed-in-chrome-using-php-yii2 – Moeez Oct 22 '17 at 16:29
0

You cannot access files under WEB-INF folder directly via Browser hence need to follow the below steps:

Firstly create a Downloads directory under webapp directory.

webapp -> Downloads -> all the downloadable files

Afterwards you may use the below code to download into your machine once the link is clicked.

<p>Info: Please Download File 
   <a href="Downloads/Template.xlsx" download>Download File</a>
</p>
Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
0

Yesterday I encountered a related issue trying to make an Excel file downloadable. I've done this successfully many times with other web pages and WordPress(WP) themes. The file could not be downloaded by clicking on the link. I had to copy and paste the link into a new browser page. While looking for solutions, I came across this thread. I would like to share the result.

This particular site is hosted on WPengine, using the Genesis child theme Enterprise Pro. I tried another WP site hosted elsewhere and the file downloaded by clicking the link. I started a chat with WPEngine and provided them a link to the problem page and link to the file. They stated the following.

"I am not showing any issues on the server, however when looking at the Browser Inspector I see a Warning: The download attribute on anchor was ignored because its href URL has a different security origin. This could be the cause of the issue, I can add a CORS Header for you, however I would recommend following up with a Developer to see if the Anchor Code needs to be updated. I am not showing any logged errors to pin point the root source of the issue, the only error I am showing is that Browser Inspector Warning that mentions security origins resulting in the attribute being ignored. The CORS Header is essentially just to let the browser know that it is okay to load content even if the Security does not match exactly as long as it is coming from the same source."

I asked them to add the CORS Header. My WPEngine portal has a Production > Web Rules page. In that page is a Header Rules section. They created a rule with the Action: Set, Name: Access-Control-Allow-Origin, Value: *, When: All responses. After adding this rule the link to the file worked as it should. I suppose this could be caused by the theme. In any event, I hope this helps someone with the same issue.

0

There are a few ways to create a download-link to an Excel file.

Method 1: Using HTML:

<a href="my_file.xlsx" download="my_file.xlsx">Download File</a>

Method 2: Using PHP:

<?php
$file_contents = file_get_contents("my_file.xlsx");

header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=my_file.xlsx");

echo $file_contents;
?>

Method 3: Using a file sharing service:

You can also create a download-link to an Excel file using a file sharing service such as Google Drive or Dropbox.

Pacific P. Regmi
  • 1,607
  • 19
  • 15