简体   繁体   中英

Generate excel from javascript

I have written all the code to generate excel in an ASPX page named GenerateXLS.aspx and I am calling the export to excel from javascript as

var BudgetID = document.getElementById("ctl00_ContentPlaceHolder1_CmbBudget").value;
var BudgetType = document.getElementById("ctl00_ContentPlaceHolder1_CmbBudgetType").value;
var CompanyID = document.getElementById('<% =HidCompanyID.ClientID %>').value;
var UserID = document.getElementById('<% =HidUserID.ClientID %>').value;
var BudgetName = document.getElementById("ctl00_ContentPlaceHolder1_CmbBudget").options[document.getElementById("ctl00_ContentPlaceHolder1_CmbBudget").selectedIndex].text;
var BudgetTypeName = document.getElementById("ctl00_ContentPlaceHolder1_CmbBudgetType").options[document.getElementById("ctl00_ContentPlaceHolder1_CmbBudgetType").selectedIndex].text;

document.getElementById("iFrame_Excel").src = "../Reports/GenerateXLS.aspx?Source=BudgetReport&BudgetID=" + BudgetID + "&BudgetType=" + BudgetType + "&CompanyID=" + CompanyID + "&UserID=" + UserID + "&BudgetName=" + BudgetName + "&BudgetTypeName=" + BudgetTypeName;   alert("works now");

The problem is it is generating the excel, but only if I keep the alert at the end of the javascript function. If I remove the alert from my function, the page is getting posted back but not generating the excel file. What might be the problem? Why is it generating the excel file only if I keep the alert?

Add e.preventDefault(); to avoid reload and, if you have to go to a specific URL after function execution, window.location.href = your_url;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM