Encoding.UTF8.GetBytes()问题 [英] Problem with Encoding.UTF8.GetBytes()

查看:82
本文介绍了Encoding.UTF8.GetBytes()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一种方法,该方法从数据库中获取数据,填充一个列表,然后使用Linq过滤该列表,并将数据以foreach的形式附加到< td></td>" ;形成一个HTML表.

I have a method that takes data from the database, populates a list, and with Linq it filters the list and appends the data, in a foreach, to a StringBuilder between "<td></td>" to form an HTML table.

然后将此StringBuilder发送到Encoding.UTF8.GetBytes(stringBuilder),以便将带有变音符号的单词保留为写入格式.

Then this StringBuilder is sent to Encoding.UTF8.GetBytes(stringBuilder) so that the words with diacritics are kept in the write format. 

然后将此文件返回到FileContentResult以生成Excel文件.

And then this file is returned to FileContentResult to generate an Excel file.

但是我面临一个非常奇怪的问题.当我过滤数据以仅选择2017或2016年的记录时,带有腹泻的单词如ç,ã,á等错误地输出为Ã,Ã, ;LÃN, 等

But I'm facing a very strange problem. When I filter the data to select only records from the year 2017 or 2016 the words with diacritis like "ç", "ã", "á", etc., are output incorrectly like "ÇÃ", "LÊN", etc.

当我按2014年或2015年过滤时,一切都很好.

When I filter by the years 2014 or 2015, everything is fine.

为什么今年会影响这一年?很奇怪:|

Why would the year afect this ? Very weird :|

我已经搜索过,但我真的不知道问题出在哪里.

I have searched but I really don't know where the problem is.


这是方法的一部分:

This is part of the method:

            else if (Ordem == 1)
            {
                foreach (var group in grupoEmpCurNome)
                {
                    relatorioAnual.Append("<tr>");
                    relatorioAnual.Append("<td>" + group.NumeroRegistroEmpregado + "</td>");
                    relatorioAnual.Append("<td>" + group.EmpsAgrupados.FirstOrDefault().NomeEmpregado + "</td>");
                    foreach (var item in group.EmpsAgrupados)
                    {
                        cursos.Append(item.DescricaoCurso);
                        cursos.Append(" -- ");
                        cursos.AppendLine();
                    }
                    relatorioAnual.Append("<td>" + cursos.ToString() + "</td>");
                    #region[Quantidade horas]
                    foreach (var item in group.EmpsAgrupados)
                    {
                        total1 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 1 ? item.QuantidadeHoras : 0;
                        total2 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 2 ? item.QuantidadeHoras : 0;
                        total3 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 3 ? item.QuantidadeHoras : 0;
                        total4 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 4 ? item.QuantidadeHoras : 0;
                        total5 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 5 ? item.QuantidadeHoras : 0;
                        total6 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 6 ? item.QuantidadeHoras : 0;
                        total7 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 7 ? item.QuantidadeHoras : 0;
                        total8 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 8 ? item.QuantidadeHoras : 0;
                        total9 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 9 ? item.QuantidadeHoras : 0;
                        total10 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 10 ? item.QuantidadeHoras : 0;
                        total11 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 11 ? item.QuantidadeHoras : 0;
                        total12 += Convert.ToDateTime(item.EmpCurDataInicio).Month == 12 ? item.QuantidadeHoras : 0;
                        Total += item.QuantidadeHoras;
                    }
                    #endregion
                    relatorioAnual.Append("<td>" + total1 + "</td>");
                    relatorioAnual.Append("<td>" + total2 + "</td>");
                    relatorioAnual.Append("<td>" + total3 + "</td>");
                    relatorioAnual.Append("<td>" + total4 + "</td>");
                    relatorioAnual.Append("<td>" + total5 + "</td>");
                    relatorioAnual.Append("<td>" + total6 + "</td>");
                    relatorioAnual.Append("<td>" + total7 + "</td>");
                    relatorioAnual.Append("<td>" + total8 + "</td>");
                    relatorioAnual.Append("<td>" + total9 + "</td>");
                    relatorioAnual.Append("<td>" + total10 + "</td>");
                    relatorioAnual.Append("<td>" + total11 + "</td>");
                    relatorioAnual.Append("<td>" + total12 + "</td>");
                    relatorioAnual.Append("<td><b>" + Total + "</b></td>");
                    relatorioAnual.AppendLine();
                    relatorioAnual.Append("</tr>");
                    relatorioAnual.AppendLine("<tr><td> </td></tr>");
                    table = @"<table>
                            <caption><b>AÇÕES - CURRÍCULO<br />
                                        HORAS {1}<br />
                                        Impresso em {2} às {3}<br/><br/><hr size='5' color='black'/></b></caption>                                    
                            <thead>
                                <tr>                                            
                                    <th>RG</th>
                                    <th>NOME</th>
                                    <th>CURSOS</th>
                                    <th>JAN</th>
                                    <th>FEV</th>
                                    <th>MAR</th>    
                                    <th>ABR</th>
                                    <th>MAI</th>
                                    <th>JUN</th>    
                                    <th>JUL</th>    
                                    <th>AGO</th>    
                                    <th>SET</th>    
                                    <th>OUT</th>    
                                    <th>NOV</th>    
                                    <th>DEZ</th>    
                                    <th>TOTAL</th>    
                                </tr>
                            </thead>
                            <tbody>
                                {0}
                            </tbody>
                        </table>";
                    if (listAnos.Count() == 4)
                    {
                        table = String.Format(table, relatorioAnual.ToString(), "DOS ÚLTIMOS QUATRO ANOS", DateTime.Today.ToShortDateString(), DateTime.Now.ToShortTimeString());
                    }
                    else if (listAnos.Count() == 3)
                    {
                        table = String.Format(table, relatorioAnual.ToString(), "DOS ANOS DE " + listAnos[2] + ", " + listAnos[1] + " e " + listAnos[0], DateTime.Today.ToShortDateString(), DateTime.Now.ToShortTimeString());
                    }
                    else if (listAnos.Count() == 2)
                    {
                        table = String.Format(table, relatorioAnual.ToString(), "DOS ANOS DE " + listAnos[1] + " e " + listAnos[0], DateTime.Today.ToShortDateString(), DateTime.Now.ToShortTimeString());
                    }
                    else if (listAnos.Count() == 1)
                    {
                        table = String.Format(table, relatorioAnual.ToString(), "DO ANO DE " + listAnos[0], DateTime.Today.ToShortDateString(), DateTime.Now.ToShortTimeString());
                    }
                    cursos.Clear();
                    Total = 0;
                    total1 = 0;
                    total2 = 0;
                    total3 = 0;
                    total4 = 0;
                    total5 = 0;
                    total6 = 0;
                    total7 = 0;
                    total8 = 0;
                    total9 = 0;
                    total10 = 0;
                    total11 = 0;
                    total12 = 0;
                }
            }
            #endregion

            throw new Exception();
        }
        catch (NullReferenceException ex)
        {
            ViewBag.Message = ex.Message;
            View();
        }
        catch (Exception ex)
        {
            ViewBag.Message = ex.Message;
            View();
        }

        return File(Encoding.UTF8.GetBytes(table), "application/octet-stream", "Relatorio_Anual_(" + DateTime.Now + ").xls");

谢谢!

推荐答案

此论坛讨论的是Visual Studio WPF/SL设计器,Visual Studio指导自动化工具包,开发人员文档和帮助系统以及Visual Studio编辑器.我会将您的话题转移到正确的论坛上,以寻求专业支持.

This forum is discussing Visual Studio WPF/SL Designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System, and Visual Studio Editor. I will move your thread to correct forum for a professional support.

最诚挚的问候,

乔伊斯


这篇关于Encoding.UTF8.GetBytes()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆