Lloydworth
Lloydworth

Reputation: 763

How to create a download-link to an Excel file?

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.

Upvotes: 9

Views: 85687

Answers (6)

Pacific P. Regmi
Pacific P. Regmi

Reputation: 1607

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.

Upvotes: 0

Woodcutter56
Woodcutter56

Reputation: 1

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.

Upvotes: 0

Du-Lacoste
Du-Lacoste

Reputation: 12817

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>

Upvotes: 0

divya gangadhar
divya gangadhar

Reputation: 11

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.

Upvotes: 1

Naveed
Naveed

Reputation: 42143

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>

Upvotes: 14

Christofer Eliasson
Christofer Eliasson

Reputation: 33885

How about just referring to the file through an anchor?

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

Upvotes: 1

Related Questions