在c#中导出.csv文件中的不同语言内容

时间:2015-06-24 11:00:26

标签: c# asp.net csv export-to-csv

我正在尝试导出.csv文件中的数据。在这里,数据是用不同的语言(阿拉伯语,丹麦语,等等)..文件可以成功导出但文件中的数据内容被替换为?和其他符号。我需要在.csv文件中按原样导出数据。我尝试了我在网上发现的每一种可能的方式,但无法导出正确的数据。你能帮帮我吗?

由于

下面是我的代码。

 protected void lnkExport_Click(object sender, EventArgs e)
        {
            List<Entities.Users_Users> lstUsers = new List<Entities.Users_Users>();

            lstUsers = BLL.Users_Users.GetDealerUsers(Convert.ToInt16(dealerid));

            StringBuilder sb = new StringBuilder();

            sb.Clear();

            sb.Append("[USERNAME],[PREFIX],[FIRSTNAME],[MIDDLENAME],[LASTNAME],[COMPANYNAME],[JOBTITLE],[CONTACTEMAILID],[USERTYPECODE],[STATUSCODE],[DEALER],[PHONE],[COMPANYLANGUAGE],[ISRECEIVECAMPAIGN],[RECEIVECAMPAIGNEMAILID]");

            sb.Append("\r\n");

            foreach (Entities.Users_Users UserDetail in lstUsers)
            { 
                sb.Append(UserDetail.UserName + "," + UserDetail.Prefix + "," + UserDetail.FirstName + "," + UserDetail.MiddleName + ",");
                sb.Append(UserDetail.LastName + "," + UserDetail.CompanyName + "," + UserDetail.JobTitle + ",");
                sb.Append(UserDetail.ContactEmailID + "," + UserDetail.UserTypeCode + "," + UserDetail.StatusCode + ",");
                sb.Append(UserDetail.DealerName + "," + UserDetail.Phone + "," + UserDetail.Language + ",");
                sb.Append(UserDetail.IsReceiveCampaign + "," + UserDetail.ReceiveCampaignEmailId);

                sb.Append("\r\n");
            }


            UTF8Encoding utf8 = new UTF8Encoding();


            var preamble = utf8.GetPreamble();

            var data = utf8.GetBytes(sb.ToString());



            //string attachment = String.Format("attachment;filename=ExportUsers.csv", "uomEncoded");
          //  Encoding encoding = Encoding.UTF8;
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.ContentType = "text/csv";  
            Response.ContentEncoding = System.Text.Encoding.UTF8;
           // Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=ExportUsers.csv");
            //Response.Charset = "";
           // Response.Charset = encoding.EncodingName;


            Response.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }

1 个答案:

答案 0 :(得分:0)

这就是你可能错过的(第一行):

Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
Response.Write(sb.ToString());