convert data into JSP into .csv format
Hi,
I have a jsp page, where data comes in from a database. I want to convert this data into .csv format.
I have written the following lines of code in the onClick event of the button:
response.setContentType("text/csv");
response.setHeader("Content-Disposition","filename=\"test.csv\"");
This code is inside scriptlet tags inside the javascript function.
I find that in addition to the data, all the javascript function etc. appear in the .csv file. Also, i want the file open dialog box to open, when i click on a button. But, now this is happening on page load.
How to rectify this?
Regards,
jayashree.
Your approach is wrong!
I am assuming that you have a screen which displays the records as a HTML. Now you have a button on click of which this needs to show and excel file (Comma Separated values) in a new window.
Steps to achieve this:
1. Remove all the Javascript code you have put.
2. onClick of button, open a new window using Javascript
3. The URL to the Window will be a servlet which is dedicated to paint the CSV.
4. The statements
response.setContentType("text/csv");
response.setHeader("Content-Disposition","filename=\"test.csv\"");
need to be put in this Servlet.
5. Compile and configure servlet and click the JSP's button.
Thanks and regards,
Pazhanikanthan. P
Hi,
As suggested by Pazhani, I have a jsp page with a button, the javascript onClick() for this button is:
if(flag=="CSV"){
var fileName = "/eriapps/frameset.do?action=CsvErricsson";
var win1 = window.open(fileName,"_blank",'resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no');
}
fileName points to a servlet, SO the servlet class has the following code:
theResponse.setContentType("text/csv");
theResponse.setHeader("Content-Disposition","filename=\"test.csv\"");
This code compiles, doesnt throw any errors, but doesnt serve the purpose. It doesnt write the o/p to a .csv file.Doesnt even open the file dialog box...
please help!
Regards,
Jayashree.
Hello Jaya,
When you click on the Button, does the new window open up or not?
If the window does not open up, then the request is not sent to the Server at all.
First we need to ensure that the request is reaching the Servlet.
Thanks and regards,
Pazhanikanthan. P