MVC project, Export to Excel, Chinese display garbled

Source: Internet
Author: User

 1 public class Homecontroller:controller 2 {3 static list<user> Getusers () 4 {5 list<user> list = new List<user> () {6 new user{id=1,name= "Zhang San"}, 7 new U Ser{id=2,name= "Lisi"}, 8 new User{id=3,name= "Wangwu"} 9};10 return list;11}1 2 public void Savetoexcel () StringBuilder sb = new StringBuilder (); Append ("id"). Append ("\ t"); Append ("name").                  Append ("\ n"); list<user> List = Getusers (); foreach (var item in List) 19 {20 Sb. Append (item. ID). Append ("\ T"), SB. Append (item. Name). Append ("\ n");}23 response.clear (); Response.Buffer = true;25 Response . Charset = "UTF-8"; response.contentencoding = encoding.utf8;27 Response.ContentType = "ApplicatiOn/ms-excel "; Response.appendheader (" Content-disposition "," attachment; Filename=card.xls "); Response.Write (sb.) ToString ()); Response.End (); 31}

How do I set this up? Thank you

Mvcexcel Bean Rice| Beginner level | Garden Bean:143
Question in: 2014-04-01 10:49 < >Collection Best Answer1

Change it to the following:

Response.Charset = "GB2312";//define Output character set
response.contentencoding = encoding.default;//output content encoded as default encoding

Harvest Garden Beans:8Crystal passers-byShrimp Grade Three | 1432: 2014-04-01 13:56

I tried, Chinese or garbled

Bean and rice| Garden beans: 143 (Beginner level) | 2014-04-01 14:43

@ Big bin Xiao hu: Then you check your Windows environment and office, is not English version. Mine is Win7 English version, but in the area and language settings all changed to China/chinese. Also check your Excel, there is a place to set up, my is Excel 2007

Soy sauce can| Garden Bean: 386 (Rookie level two) | 2014-04-01 15:15 Other Answers (7)0

Response.Charset = "GB2312"; try it.

Soy sauce can| Garden Bean: 386 (Rookie level two) | 2014-04-01 10:50

Just tried, also no, Chinese place still show "

??????

"

Bean and rice| Garden beans: 143 (Beginner level) | 2014-04-01 11:34

@ Big bin Xiao Hu:

Response.Charset = "GB2312";
response.contentencoding = Encoding.default;

That's all I can do.

Soy sauce can| Garden Bean: 386 (Rookie level two) | 2014-04-01 13:55 0

response.contentencoding = Encoding.UTF8; this one has to be changed.

Wu Ruixiang| Beans: 13359 (Expert level six) | 2014-04-01 12:25

Just modified the next, such modifications are not, or garbled

Bean and rice| Garden beans: 143 (Beginner level) | 2014-04-01 13:22 0

It is recommended to do it with some components that Excel reads and writes.

Daily Tutorial Net| Garden Bean: 202 (Rookie level two) | 2014-04-01 13:43 0

GB2312

More Magic| Garden Bean: 927 (shrimp three) | 2014-04-01 13:50 0

Response.Charset = "GB2312";
response.contentencoding = Encoding.default;

This really solves the encoding conversion problem.

Harvest Garden Beans:1A cynical Rei Yin| Garden beans: 34 (Beginner level) | 2014-04-01 14:16 0

I have done this thing before, this is a WinForm code, the landlord to see if it will help it.

Http://www.cnblogs.com/Price/p/3528006.html

Harvest Garden Beans:1Goona| Garden Bean: 826 (shrimp three) | 2014-04-01 17:31 0

public void Savetoexcel ()
13 {
StringBuilder sb = new StringBuilder ();
SB. Append ("id"). Append ("\ t");
. Append ("name"). Append ("\ n");
list<user> List = Getusers ();
foreach (var item in list)
19 {
. Append (item. ID). Append ("\ t");
SB. Append (item. Name). Append ("\ n");
22}
Response.Clear ();
Response.Buffer = true;
Response.Charset = "UTF-8";
response.contentencoding = Encoding.UTF8;
Response.ContentType = "Application/ms-excel";
Response.appendheader ("Content-disposition", "attachment; Filename=card.xls ");

29//Add this sentence
Response.Write ("<meta http-equiv=\" content-type\ "content=\" application/vnd.ms-excel; charset=utf-8\ "/&G" t; "+ SB. ToString ());
Response.End ();
32}

Give a reward!!

Blog Park, recalled| Garden Bean: 202 (Rookie level two) | 2015-04-23 16:39

Do not quite understand why add that sentence, you can solve the problem of garbled.

Dongmusic| Garden Bean: 228 (Rookie level two) | 2015-10-31 20:22

Yes, with this sentence, it solves the garbled.

Response.Write ("<meta http-equiv=\" content-type\ "content=\" application/vnd.ms-excel; charset=utf-8\ "/>" + SB . ToString ());

Fucking

MVC project, Export to Excel, Chinese display garbled

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.