لماذا تنتج إخراج استجابة دخلي لهجات القمامة في Excel ولكن تبدو جيدة في المفكرة؟

StackOverflow https://stackoverflow.com/questions/1497039

سؤال

أنا أستخدم تقنية من سؤال فائض مكدس آخر لكتابة ملف CSV إلى Response إخراج للمستخدم لفتح / حفظ. يبدو الملف جيدا في المفكرة، ولكن عندما أفتحها في Excel الأحرف المعلمة هي القمامة. افترضت أن هذا كان شيئا ما يجب القيام به مع ترميز الشخصية، لذلك حاولت إعداده يدويا إلى UTF-8 (الافتراضي ل StreamWriter). هنا هو الرمز:

// This fills a list to enumerate - each record is one CSV line
List<FullRegistrationInfo> fullUsers = GetFullUserRegistrations();

context.Response.Clear();
context.Response.AddHeader("content-disposition",
                           "attachment; filename=registros.csv");
context.Response.ContentType = "text/csv";
context.Response.Charset = "utf-8";

using (StreamWriter writer = new StreamWriter(context.Response.OutputStream))
{
    for (int i = 0; i < fullUsers.Count(); i++)
    {
        // Get the record to process
        FullRegistrationInfo record = fullUsers[i];

        // If it's the first record then write header
        if (i == 0)
            writer.WriteLine(Encoding.UTF8.GetString(
                Encoding.UTF8.GetPreamble()) + 
                "User, First Name, Surname");

        writer.WriteLine(record.User + "," +
                         record.FirstName + "," +
                         record.Surname);
    }
}

context.Response.End();

أي أفكار فيما يتعلق بما أود القيام به لتشفير الملف بشكل صحيح، لذلك يمكن ل Excel عرض الأحرف المعلمة؟

هل كانت مفيدة؟

المحلول

قد تضطر إلى كتابة مؤشر UTF-8 يسمى علامة البايت إلى بداية الناتج لإخطار Excel حول UTF-8. إكسل سخيف.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top