JavaScript function for exporting HTML table to Excel with the ability to choose the file name

My current challenge involves a function used to export HTML to Excel:

function generateexcel(tableid) {
  var table= document.getElementById(tableid);
  var html = table.outerHTML;
  window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}

The issue at hand is the inability to specify a file name for saving, resulting in users seeing something like this:

Do you want to save %3Ctable%20id%3D%22tableRslts%22%20tabindex%3D%2235%22%20 file?

And the saved file turns out as:

IytvT8Jo.xls.part.xls (primarily experienced on Firefox, our selected browser)

How should this be rectified?

Answer №1

If you're looking to achieve this functionality, there are two options worth exploring:

  • Filesaver API is a cutting-edge 'HTML5' feature that allows for the exact functionality you need. The only downside is that Firefox doesn't fully support it yet. To work around this, you can use a convenient wrapper library like filesaver.js.
  • Downloadify offers a flash-based solution for the same purpose. You can access it here. (Keep in mind: requires Flash)

Answer №2

Have you already addressed this task? You may need to incorporate the following snippet into your aspx page:

$(window).load(function(){
$( "#clickExcel" ).click(function() {  
var dtltbl = $('#dtltbl').html();    `enter code here`
window.open('data:application/vnd.ms-excel,' + $('#dtltbl').html());
});
});//]]>  

In the provided script, #dtltbl refers to the Table Id.

To resolve the issue, make sure to include the following code in your server-side programming:

           Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.csv");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creative ways to conceal JavaScript within your Wordpress website

After placing my javascripts in the header.php file, I realized that the code could easily be snatched by viewing the source on any post or homepage... Can you recommend a straightforward method to conceal my javascripts in WordPress and prevent them from ...

Tips for modifying a HTML table to switch one column with 60 rows to three columns with 20 rows dynamically

Need assistance with separating a dynamically loaded single-column HTML table of 60 rows into three columns of 20 rows each using jQuery in JSP. For example, starting with the loaded table: test 1 test 2 test 3 test 4 test 5 test 6 test 7 test 8 test 9 t ...

The button I have controls two spans with distinct identifiers

When I press the player 1 button, it changes the score for both players. I also attempted to target p2display with querySelector("#p2Display"), but it seems to be recognized as a nodeList rather than an element. var p1button = document.querySelector("# ...

Having difficulty accessing $scope beyond the function

One of the functions in my code is called getMail() $scope.getMail=function(){ $http.get(APISource). success(function(data) { $scope.mail =data; }). error(function(data) { console.log("error"); console.log(data); }); } In the succe ...

Content that is added dynamically seems to vanish once it is posted

Currently, I am working with an asp.net control (.ascx). I am dynamically adding rows to a table using jQuery in the following manner. $('#<%= doorFileNameTable.ClientID %>').after('<tr><td></td><tr/>').a ...

Managing Recursive Promises: A Guide

function Authenticate() { this.invalidAccessToken = false; } Authenticate.prototype.checkTokenValidity = function (accessToken, refreshToken) { var token; var self = this; return new Promise(function (resolve, reject) { Authenticat ...

Encountered an error loading resource: server returned a 400 (Bad Request) status when using Angular with NodeJS

My Angular and NodeJS application with Express (v4.17.1) exposes a REST API like this: app.get('/myRestApi', function (req, res) { console.log('here you are ... '); res.end('success!\n'); }); The body of this API ...

Creating a Circle with Pixi.js v4 and Typerscript in IONIC 2

I have been attempting to create a custom class in TypeScript that utilizes PIXI.js to draw circles. Below is the code for my home.ts class: import { Component, ViewChild, ElementRef } from '@angular/core'; import { NavController } from 'i ...

Trying to set headers in Node/Express after they have already been sent is causing an error

I am attempting to send form data from the client side using jQuery to a POST route that will then pass the data to an API in Node/Express. I am encountering an issue where I receive the error message "Can't set headers after they are sent" and I am ...

What could be causing the lack of updates for a watched object in vue?

Although there are many similar questions on this topic, mine has a unique twist. After delving into reactivity in depth, I feel like I have a solid grasp of the concept. However, one thing continues to baffle me: Summary: Why does a watched property det ...

Valid method of confirming a user's login status

Using AJAX requests, I have implemented a user area on my website. The "Log off" button is supposed to be displayed only when the user is logged in. Here is the AJAX request for logging in a user: function requestSI() { var xhr = getXMLHttpRequest(); xhr ...

Utilizing SVG with an integrated script to automatically adjust to the available space within different browser

I am attempting to develop a self-contained SVG document that automatically adjusts its size and centers itself when loaded in a web browser. It's important to note that this is not referring to embedding an SVG within an HTML document. Below is the ...

JavaScript lacks the power to persuade the mouse into altering its cursor

I am encountering an issue with my ASP.NET page that contains an Infragistics webgrid. I have implemented Javascript methods to handle the mouseover and mouseout events on the grid rows, changing the mouse cursor to a pointer and back to the default as use ...

NextJs getStaticPaths function is failing to render the correct page, resulting in a 404 error message being displayed

Hey there, I'm in a bit of a pickle as I've never used 'getStaticPaths' before and it's crucial for my current project! I followed the example code from NextJs's documentation on 'getStaticPaths', but when I try to ...

Avoid duplication of choices on two checkboxes containing the same options

I have two select boxes where I am trying to prevent duplicated values (options) in each select box. Both select boxes start with the same options list, but once an option is selected in selectA, it should no longer be visible in selectB, and vice versa. T ...

Instructions on how to navigate to a class page by clicking a button in a ReactJS interface

I am currently working with React and have implemented 3 classes in separate files. The App.js file contains a navbar and button, which when clicked, displays a table from 'Table.js'. I have also added buttons in front of each row in the table th ...

Employing getters in the toObject() method

As I delve into the code of a Node.js Express application for learning purposes, I came across the following line that sparked my curiosity regarding the inclusion of getters and virtuals. var pgmsDbObj = chnnlList[chnnlIndex] var pgmsObj = pgmsDbObj.to ...

Validation of viewstate MAC unsuccessful with SharePoint 2010

Struggling with a viewstate problem in my SharePoint 2010 application. I have created a custom master page that utilizes a Custom Control called Navigation. The HTML for the Navigation control consists of only 3 Hidden Fields to store data. After applyin ...

Exploring the power of NodeJS modules through exports referencing

Encountering difficulties referencing dynamic variables related to mongoose models based on user session location value. Two scripts are involved in this process. The first script is called location.js & reporting.js In the location.js script: module.ex ...

Script update addressing CSS application to title attributes to resolve bugs

The following script adds a CSS class to the HTML title attribute. You can find the original page where the script is located here: How to change the style of Title attribute inside the anchor tag? Although it works well, there is a small issue. If the ti ...