【问题标题】:Export to Excel, names with accent are not exported correctlty导出到 Excel,带重音的名称无法正确导出
【发布时间】:2014-10-23 08:41:34
【问题描述】:

我正在从数据表中导出包含以下代码的 csv 文件。但是带有重音符号的名称无法正确导出

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;
            }
        }

【问题讨论】:

  • 您是否尝试使用值为“text/csv; charset=utf-8”的标题“Content-Type”?
  • 那没有帮助,我注意到数据表上的数据是正确的。 screencast.com/t/4tVJKW5jG9p

标签: c# asp.net excel


【解决方案1】:

设置内容类型后尝试添加以下内容。

// 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, eg Response.Charset = "utf-8"; Response.ContentEncoding = Encoding.UTF8;

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

TQ。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多