Correct Encoding Export File

I don’t understand what is not here. I'm trying to export a csv file with extended ASCIIcharacters like ÿor ü, but all I get is
I need to specify something else in the answer?

Encoding encoding = Encoding.UTF8;

//ToCSV writes the string correctly
var bytes = encoding.GetBytes("write ÿ or ü please");
MemoryStream stream = new MemoryStream(bytes);

StreamReader reader = new StreamReader(stream);
//TextWriter tw = new TextWriter();
Response.Clear();
Response.Buffer = true;

Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.csv", fileName));
Response.Charset = encoding.EncodingName;
Response.ContentType = "application/text";
Response.Output.Write(reader.ReadToEnd());
Response.Flush();
Response.End();
+5
source share
3 answers

I believe you should add Response.ContentEncoding = Encoding.Unicodein order to get the right conclusion.

    Encoding encoding = Encoding.UTF8;
    var bytes = encoding.GetBytes("write ÿ or ü please");
    MemoryStream stream = new MemoryStream(bytes);
    StreamReader reader = new StreamReader(stream);
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.csv", "filename"));
    Response.Charset = encoding.EncodingName;
    Response.ContentType = "application/text";
    Response.ContentEncoding = Encoding.Unicode;
    Response.Output.Write(reader.ReadToEnd());
    Response.Flush();
    Response.End();
+17
source

Unfortunately, it Encoding.Unicodedid not work, using it Windows-1252worked:

Response.Clear();
Response.ContentType = "Application/x-msexcel";
Response.AddHeader("content-disposition", "attachment; filename=\"filename.csv\"");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252");
Response.Write(string.Join(Environment.NewLine, myDataLines));
Response.End();
+4
source

Java, , StreamReader? , StreamReader(stream) . , , .

0

All Articles