本文介绍了导出到Excel,带重音的名字不会被导出correctlty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出口从一个DataTable以下code csv文件。然而,随着口音的名称不正确导出

  VAR DT = JobContext.GetEngagementLetterReport(SPContext.Current.Site,int.Parse(rdlOption.SelectedValue));
            VAR COLUMNNAMES =新的List<串GT;
            {
                Constants.SearchFields.Client.Client code,
                Constants.SearchFields.Client.ClientName,
                Constants.SearchFields.Job.Job code,
                Constants.SearchFields.Job.JobName,
                Constants.SearchFields.Job.JobPartner,
                Constants.SearchFields.Job.JobDirector,
                Constants.SearchFields.Job.JobManager,
                Constants.SearchFields.Job.BillContact,
                Constants.SearchFields.Job.LineOfService,
                Constants.SearchFields.Job.BusinessUnit,
                Constants.SearchFields.Job.OperatingUnit,
                JobSiteUrl
                FileNameUrl
            };
            ExcelHelper.ExportDatatabletoExcel(DT,COLUMNNAMES);
 公共静态无效ExportDatatabletoExcel(DT数据表,表<串GT; COLUMNNAMES)
        {
            尝试
            {
                常量字符串附件=附件;文件名= elreport.csv;
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader(内容处置,附件);
                HttpContext.Current.Response.ContentType =应用程序/ vnd.ms-EXCEL;
                字符串标签=;
                的foreach(在dt.Columns的DataColumn DC)
                {
                    如果(!columnNames.Contains(dc.ColumnName))继续;
                    HttpContext.Current.Response.Write(标签+ dc.ColumnName);
                    标签=;
                }
                HttpContext.Current.Response.Write(\\ n);
                INT I;
                的foreach(在dt.Rows的DataRow博士)
                {
                    标签=;
                    对于(i = 0; I< dt.Columns.Count;我++)
                    {
                        如果(!columnNames.Contains(dt.Columns [I] .ColumnName))继续;
                        HttpContext.Current.Response.Write(标签+ DR [I]的ToString());
                        标签=;
                    }
                    HttpContext.Current.Response.Write(\\ n);
                }
                HttpContext.Current.Response.End();
            }
            赶上(异常前)
            {
                字符串的errorMessage =的String.Format(ExportToExcelError:{0},ex.Message);
                LoggingService.LogError(LoggingCategory.General,恩,的errorMessage);
                扔;
            }
        }


解决方案

尝试添加设置内容类型后,下面的。

//这是缺少的一部分,从原来的code设置字符集和编码 - 如果这不起作用,适当的值,例如更换
Response.Charset的=UTF-8;
Response.ContentEncoding = Encoding.UTF8;

//你可能需要使用适当的MIME类型内容类型或实验
Response.ContentType =TEXT / CSV;

TQ。

I am exporting a csv file with the following code from a datatable. however names with accents are not exported correctly

http://screencast.com/t/i9N2mB34DL

 var dt=JobContext.GetEngagementLetterReport(SPContext.Current.Site, int.Parse(rdlOption.SelectedValue));
            var columnNames = new List<string>
            {
                Constants.SearchFields.Client.ClientCode,
                Constants.SearchFields.Client.ClientName,
                Constants.SearchFields.Job.JobCode,
                Constants.SearchFields.Job.JobName,
                Constants.SearchFields.Job.JobPartner,
                Constants.SearchFields.Job.JobDirector,
                Constants.SearchFields.Job.JobManager,
                Constants.SearchFields.Job.BillContact,
                Constants.SearchFields.Job.LineOfService,
                Constants.SearchFields.Job.BusinessUnit,
                Constants.SearchFields.Job.OperatingUnit,
                "JobSiteUrl",
                "FileNameUrl"
            };
            ExcelHelper.ExportDatatabletoExcel(dt, columnNames);


 public static void ExportDatatabletoExcel(DataTable dt, List<string> columnNames)
        {
            try
            {
                const string attachment = "attachment; filename=elreport.csv";
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.AddHeader("content-disposition", attachment);
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                string tab = "";
                foreach (DataColumn dc in dt.Columns)
                {
                    if (!columnNames.Contains(dc.ColumnName)) continue;
                    HttpContext.Current.Response.Write(tab + dc.ColumnName);
                    tab = ";";
                }
                HttpContext.Current.Response.Write("\n");
                int i;
                foreach (DataRow dr in dt.Rows)
                {
                    tab = "";
                    for (i = 0; i < dt.Columns.Count; i++)
                    {
                        if(!columnNames.Contains(dt.Columns[i].ColumnName)) continue;
                        HttpContext.Current.Response.Write(tab + dr[i].ToString());
                        tab = ";";
                    }


                    HttpContext.Current.Response.Write("\n");
                }
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                string errorMessage = String.Format("ExportToExcelError: {0}", ex.Message);
                LoggingService.LogError(LoggingCategory.General, ex, errorMessage);
                throw;
            }
        }
解决方案

Try adding the following after setting the content type.

// This is the missing part from your original code to set the charset and encoding - if this does not work, replace it with appropriate value, egResponse.Charset = "utf-8";Response.ContentEncoding = Encoding.UTF8;

// You might want to use this content type or experiment with appropriate MIME typeResponse.ContentType = "text/csv";

TQ.

这篇关于导出到Excel,带重音的名字不会被导出correctlty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:43