It seems to me that what you want is not possible. I have to explain more detailed what I mean.
It is not a problem to get the CSV, XLS or XLSX file contain per HTTP POST. The problem is to show the server response in Excel if you will use HTTP POST.
The code of excelExport
method is very simple you can see it here. What excelExport
do is just open an URL where some additional parameters will be added. The main part of the code is following
window.location = url;
So all real interesting things are implemented on the server. It is important that the server set some HTTP headers, especially Content-Type
, which define the HTTP response as Excel file (or as CSV if you can't generate XLSX data). I personally use Open XML SDK 2.0 to generate XLSX file contain and set "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
as the Content-Type
. In your case it should be "text/csv"
(see here). Additionally one can use Content-Disposition
HTTP header to define the preferred filename of the response. For example, it can be "attachment; filename=test.csv"
in your case. Because you has already the server code you have probably all the things implemented in the code.
The most important part is: web browser knows how to open different URL contains. If it open new url (per HTTP GET !!!) it will use the corresponding application like Excel to show it.
On the other side if you use $.ajax
you can get the CSV contain per HTTP POST, but how you want to solve the next problem - to start Excel with the data? I don't know the simple solution without the usage of ActiveX controls working in Internet Explorer only.
So I recommend you just use HTTP GET. If you don't want caching of data you can do this by setting the corresponding HTTP headers. Setting of Cache-Control: max-age=0
is enough in the most cases. Setting Cache-Control: private
additionally switch off caching the data on the proxy and declare that the data could be cached, but not shared with another users. More information about the subject you can find in the following Caching Tutorial.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…