Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser

Cover Image for Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Exporting HTML Table Data to Excel: Fixing the Chrome Browser Issue

If you've ever tried to export HTML table data to Excel using JavaScript or jQuery, you might have encountered some challenges when using the Chrome browser. The script that works perfectly in Mozilla Firefox fails to produce the desired result in Chrome. In this blog post, we will address this specific problem and provide you with an easy solution.

The Problem

When attempting to export HTML table data to Excel using the provided script, Chrome doesn't display the pop-up dialog box asking you to open or save the file. Instead, it downloads a file with a generic "file" type, without any file extension like ".xls". Surprisingly, there are no related errors shown in the Chrome console, making it more challenging to identify the issue.

Example and Reproduction

To better understand the problem, let's look at a Jsfiddle example: http://jsfiddle.net/insin/cmewv/.

In this example, when you click on the "Export to Excel" button, it should ideally prompt you to open or save the Excel file. Unfortunately, in the Chrome browser, the pop-up dialog box doesn't appear, and the file is downloaded with a generic file type.

To reproduce the issue, follow these steps:

  1. Click on the "Export to Excel" button.

  2. Observe the downloaded file with the generic file type.

The Solution

To fix the issue and ensure proper functionality across all browsers, including Chrome, we need to make a minor modification to the existing script. By adding a file extension and setting the appropriate Content-Type header, we can prompt the browser to recognize the file as an Excel file.

Update your script as follows:

<script type="text/javascript">
function ExportToExcel(mytblId){
    var htmltable= document.getElementById('my-table-id');
    var html = htmltable.outerHTML;

    // Add the file extension and set Content-Type header
    var uri = 'data:application/vnd.ms-excel;base64,';

    // Create a new XLSX file using the HTML table data
    var template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
                      <head><meta charset='UTF-8'></head>
                      <body>${html}</body>
                    </html>`;

    // Convert the template to base64
    var base64 = function(s) {
        return window.btoa(unescape(encodeURIComponent(s)));
    };

    // Append the base64-encoded template to the URI
    var url = uri + base64(template);

    // Open the URL in a new window/tab
    window.open(url);
}
</script>

With this updated script, when you click on the "Export to Excel" button, Chrome (and other browsers) will now display the pop-up dialog box, allowing you to open or save the file with the correct ".xls" extension.

Conclusion and Call-to-Action

Exporting HTML table data to Excel is a common task in web development, and compatibility across different browsers is crucial. By implementing the solution provided in this blog post, you will be able to overcome the issue with Chrome and maintain consistent functionality across all browsers.

If you found this guide helpful or have any additional questions, please share your thoughts in the comments section below. Happy exporting! 🚀💪😊


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello