Java Web export CSV file in Chinese garbled problem resolution.
Source: Internet
Author: User
To output a CSV file, just change the bottom of the information can be, the following settings can be:
Response.setcontenttype ("application/csv;charset=gb18030");
Response.setheader ("Content-disposition", "attachment; Filename=x.csv ");
Garbled problem:
At first I think the code set should be correct (on the Internet to see the data is also such settings), it should not be coding problems, do not understand, and then I put the statement of the set header information in the front, the Chinese garbled problem solved!
* Here also uses the Chinese pinyin to outsource (Pinyin4j.jar)--only cares about exporting the CSV file not to manage it, mainly looks the green font the Code
I wrote it in the servlet.
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("application/csv;charset=gb18030");
Response.setheader ("Content-disposition", "attachment; Filename=x.csv ");
PrintWriter out = Response.getwriter ()//In the first sentence there will be garbled
String str = request.getparameter ("name");
Out.println (Str.charat (0));
try {
Class.forName ("Com.mysql.jdbc.Driver");
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try{
Java.sql.Connection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "ACCP");
Out.println (con);
Java.sql.Statement stat = con.createstatement ();
rs = Stat.executequery ("SELECT * from book");
while (Rs.next ()) {
String name=rs.getstring (3);
char c = name.charat (0);
string[] t2 = new STRING[10];
The format of the declared output
net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 = new
Hanyupinyinoutputformat ();
T3.setcasetype (hanyupinyincasetype.uppercase);
T3.settonetype (Hanyupinyintonetype.without_tone);
T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);
T2 = Pinyinhelper.tohanyupinyinstringarray (c, T3);
if (T2[0].charat (0) ==str.touppercase (). CharAt (0)) {
Out.print ("/" "+rs.getint (1) +"/","); The contents of the cell are separated by "" and "".
Out.print ("/" "+rs.getstring (2) +"/",");
Out.print ("/" "+name+"/",");
Out.print ("/" "+rs.getfloat (4) +"/"/n");/n
}
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