In JSP, click the button to generate a CSV file for the detected data in bean, and then open it automatically in IE.

Source: Internet
Author: User
Quote:
This issue can be discussed in two parts:

1. CSV file format
2. Output CSV files to the client through JSP

The first question is discussed in a simple way. It can be considered that each field is included and reused with "" and separated by numbers.
For example:
"A", "B", "C"
"D", "E", "F"

Now we have the contents of the CSV file. Let's see how to output it to the client.

A work und is to write the CSV content to a temporary file, and then ask the client to download the temporary file.

However, the simplest method is to directly output the following content to the client: CSV. jsp
"A", "B", "C"
"D", "E", "F"

In this case, the information is output to the client.

However, it is estimated that the current situation will not meet the requirements of the landlord, because the browser handles the information itself, rather than handing it over to excel for processing.
In order for the browser to handle the processing right, we need to add some information in the header of the JSP to tell the browser that you do not have to worry about this information, hand it over to other appropriate programs (Excel for CSV) for processing.
First, tell it "The following information is M $, not html ":
<% @ Page Language = "Java" contenttype = "application/MS-excel" %>
Then upload the file test.csv.
<% Response. setheader ("content-disposition", "filename=test.xls"); %>
Finally, the content of the file:
"A", "B", "C"
"D", "E", "F"

This is the full text.
<% @ Page Language = "Java" contenttype = "application/MS-excel" %>
<% Response. setheader ("content-disposition", "filename=test.xls"); %>
"A", "B", "C"
"D", "E", "F"

The landlord can replace ABCD with something you can find from Javabean.

When I access this page again, I will prompt you to open or save it. It should be the result that the landlord wants.

The following code is implemented in combination with jstl:
Test. jsp file
Code:

<% @ Page contenttype = "text/html; charset = GBK" Language = "Java" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "C" %>
<% @ Taglib prefix = "FMT" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix = "FN" %>
<%
Response. resetbuffer ();
Response. setcontenttype ("application/unknow ");
Response. setheader ("content-disposition ",
"Attachment; filename=test.csv ");
%> <C: foreach items = "$" Var = "record" >$, $ </C: foreach>

Collection is a request.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.