Ask a PHPExcel question. Recently, I am working on a project. I need to export the queried table data to the excel client, because the page table data is not directly queried from the database, it is obtained through computation and other processing. There is a button on the page. the onclick event uses ajax to pass some necessary parameters to the server. the exportexcel. php file receives the parameters and then queries the database. after processing, it exports them to the excel file using phpexcel.
The problem is that ajax has been executed, but the browser does not pop up a window to download the exported excel file. How can I use ajax to transmit parameters to exportexcel. php and the download and save window is displayed?
Ajax code
Function excel (url1, params)
{
Try {
Var xhr = new XMLHttpRequest ();
} Catch (trymicrosoft ){
Try {
Var xhr = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (othermicrosoft ){
Try {
Var xhr = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (failed ){
Var xhr = false;
}
}
}
Var url = url1 + "? "+" & R = "+ Math. random ();
Xhr. open ("POST", url, true); // readyState = 1
Xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
Xhr. send (params );
Xhr. onreadystatechange = function ()
{
// If the value is 4, the interaction is complete. we can retrieve the content returned by the server.
If (xhr. readyState = 4 & xhr. status = 200)
{
}
}
}
The exportexcel. php code is correct. I tested it separately. the download window is displayed.
Please give me some advice on how to do this. it has been bothering me for several days and I have never been able to come up with a solution.
Reply to discussion (solution)
The returned file stream is received by ajax and will not be downloaded.
After receiving the ajax request, you can generate an excel file on the server, and only return the connection. after ajax is sent to A or IFRAME for download
I still don't quite understand. what link is returned when excel is generated on the server, and how can I use the tag to download ajax?
If you use a connection to pass parameters without ajax, will this happen?
Function execl is used for splicing ??, Submit to url1.
If you can change it to get ????,????? You can just download it? Now
function excel(url1,params){ window.location = url1+"?"+ url1+"?"+"&r="+Math.random() + params;}
Or use the method provided by the moderator. If excel ratio? Big, use? You need to wait until the excel file is generated ?. But using file streams? Yes? Next? Generate.
Watch? Yes.
Correct it ,????.
function excel(url1,params){ window.location = url1+"?"+"&r="+Math.random() + "&" + params;}
I still don't quite understand. what link is returned when excel is generated on the server, and how can I use the tag to download ajax?
For example? After the request is completed, a? is returned? Execl
What does phpexecl have? For the savetofile method, use ??, Then? Generate? Execl file.
Then ?? File path ?, For example, the http://www.example.com/excel/a.xls returns? Js, js and window. location? Use? You can.
Reply from the landlord on the third floor:
If you use a connection to pass parameters without ajax, will this happen?
No. In other words, if I run IE9 in the environment, the get method won't work. ie9 does not respond with the get method, and the post method can run successfully. this is what I found in the test.
Reply to fdipzone on the 6th floor:
For example? After the request is completed, a? is returned? Execl
What does phpexecl have? For the savetofile method, use ??, Then? Generate? Execl file.
I tried. phpexcel does not have the savetofile method. I downloaded the latest phpexcel version from the official website. In addition, try the method you mentioned on the fifth floor first. thank you for your guidance!
function excel(url1,params){ window.location = url1+"?"+"&r="+Math.random() + "&" + params; }
Ask another question: after modifying the excel function, how can I receive parameters in php? in this case, I will not use ajax. Can I still get parameters after post in php?
No savetofile method
But there is a save method.
function excel(url1,params){ window.location = url1+"?"+params+"&r="+Math.random() ; }
The download window can be displayed using this method, but why does $ _ GET ['bbsty'] in php fail to receive url-based parameters? Who can help me, online...
function excel(url1,params){ window.location = url1+"?"+params+"&r="+Math.random() ; }
The download window can be displayed using this method, but why does $ _ GET ['bbsty'] in php fail to receive url-based parameters? Who can help me, online...
function excel(url1,params){ window.location = url1+"?"+params+"&r="+Math.random() ; }
The download window can be displayed using this method, but why does $ _ GET ['bbsty'] in php fail to receive url-based parameters? Who can help me, online...
You ?? ?? Bbsty ?? If other ?? Yes ,?? No, estimate? Yes ?? Name? .
Alert (url1 + "? "+ Params +" & r = "+ Math. random (); to see what is there ?.
Yes. in function excel, alert (url1 + "? "+ Params +" & r = "+ Math. random (); check is normal,
Print_r ($ _ REQUEST); what is output?
Yes. in function excel, alert (url1 + "? "+ Params +" & r = "+ Math. random (); check is normal,
Can txtdate and dev be obtained using $ _ GET? Only bbsty does not work? ?? Strange
Txtdate, dev, and bbsty cannot be received with $ _ GET. print_r ($ _ REQUEST); there is nothing. it is strange that get cannot be received, and no parameter can be received.
Paste ExportToExcel. php to SAVE database operations and code generated in excel
Your ajax method sends data in post mode. Obviously, this program does not have code to read the get data.
After using this function, you will not use ajax. the onclick event of the button is function excel (url1, params)