简体   繁体   English

StringWriter 处理生成的 csv 上的奇怪字符

[英]StringWriter processes strange characters on csv generated

I'm having troubles using StringWriter on our application.我在我们的应用程序上使用StringWriter时遇到了麻烦。 I do a rest call over a nosql db and it returns a list of dynamics .我通过nosql db进行了一个休息调用,它返回一个dynamics列表。 I use StringWriter to write a csv file that contains a header and records from my list.我使用StringWriter编写一个csv文件,其中包含我的列表中的标题和记录。

I also tried to extend the StringWriter with a sealed class with constructor method which allows you to enter the type of encoding as a parameter.我还尝试使用带有constructor methodsealed class扩展 StringWriter,该constructor method允许您输入编码类型作为参数。 But trying all the encodings available it still generates wrong charachters.但是尝试所有可用的编码它仍然会生成错误的字符。

This is our extension of StringWriter:这是我们对 StringWriter 的扩展:

public sealed class StringWriterWithEncoding : StringWriter
{
    private readonly Encoding encoding;

    public StringWriterWithEncoding() : this(Encoding.UTF8) { }

    public StringWriterWithEncoding(Encoding encoding)
    {
        this.encoding = encoding;
    }

    public override Encoding Encoding
    {
        get { return encoding; }
    }
}

and this is the code for generate the csv file:这是生成csv文件的代码:

StringWriterWithEncoding sw = new StringWriterWithEncoding();

// Header
sw.WriteLine(string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};", "Soddisfazione", "Data Ricerca", "Categorie Cercate", "Id Utente", "Utente", "Categoria", "Id Documento", "Documento", "Id Sessione", "Testo Ricerca"));

foreach (var item in result.modelListDyn)
{
   sw.WriteLine(string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};", item.Satisfaction, item.Date, item.Cluster, item.UserId, item.Username, item.Category, item.DocumentId, HttpUtility.HtmlDecode(item.DocumentTitle.ToString()), item.SessionId, 
   item.TextSearch));
}

var response = Request.CreateResponse(HttpStatusCode.OK, sw.ToString());

response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");

return response;

When the file is generated on in a column with some text, it display strange chars: L’indennità di licenziamento del Jobs Act è incostituzionale当文件在带有一些文本的列中生成时,它显示奇怪的字符: L’indennità di licenziamento del Jobs Act è incostituzionale

This is italian, and the wrong chars are seems to be à è ò ' ù etc.这是意大利语,错误的字符似乎是à è ò ' ù等。

Anyone can suggest a solution?任何人都可以提出解决方案吗? Thank you!谢谢!

UPDATE更新

As user suggested, i started using CsvHelper I created a Class and a ClassMap but it still returns corrupted chars.正如用户所建议的,我开始使用 CsvHelper 我创建了一个 Class 和一个 ClassMap,但它仍然返回损坏的字符。

StringWriter sw = new StringWriter();
CsvWriter cw = new CsvWriter(sw);
using (CsvWriter csv = new CsvWriter(sw))
{
  csv.Configuration.RegisterClassMap<HistorySearchModelCsvHelperMap>();
  csv.Configuration.CultureInfo = CultureInfo.InvariantCulture;
  csv.WriteRecords(csvModelHelperList);
}

Result:结果: 在此处输入图片说明

UPDATE 2更新 2

The problem is client-side , my action returns the correct text, without broken chars.问题是client-side ,我的操作返回正确的文本,没有损坏的字符。 Action is triggered when i call it with an axios get instance.当我使用axios get 实例调用它时会触发操作。

axios.get(url, {
   headers: {
      'Accept': 'application/vnd.ms-excel',
      'Content-Type': 'application/vnd.ms-excel'
   }
})
.then(({ data }) => {
   const blob = new Blob([data], {
      type: 'application/vnd.ms-excel',
   });
   // "fileDownload" is 'js-file-download' module.
   fileDownload(blob, 'HistorySearches.csv', 'application/vnd.ms-excel');
   this.setState({ exportLoaded: true, exportLoading: false });
}).catch(() => {
   this.setState({ exportLoaded: false, exportLoading: false });
});

I read to set responseType to blob but even passing the type: 'application/vnd.ms-excel' the chars over my csv file are still corrupted.我读到将responseType设置为blob但即使传递类型:'application/vnd.ms-excel' 我的 csv 文件上的字符仍然损坏。 In my action when i return the Response :在我的action ,当我返回Response

// ... some code

StringWriterWithEncoding sw = new StringWriterWithEncoding();
CsvWriter cw = new CsvWriter(sw);
using (CsvWriter csv = new CsvWriter(sw))
{
    csv.Configuration.RegisterClassMap<HistorySearchModelCsvHelperMap>();
    csv.Configuration.CultureInfo = CultureInfo.InvariantCulture;
    csv.WriteRecords(csvModelHelperList);
}

return Request.CreateResponse(HttpStatusCode.OK, sw.ToString());
// response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.ms-excel");

return response;

I tried to set content type server-side too, but the format is incorrect anyway.我也尝试在服务器端设置内容类型,但无论如何格式都不正确。

If you want to be able to open your csv in Excel, you need to write it with an encoding of Windows-1255.如果您希望能够在 Excel 中打开您的 csv,您需要使用 Windows-1255 编码编写它。

If you open the csv in a generic text editor and it still displays incorrectly, I'm not sure what's wrong, as your code looks sane.如果您在通用文本编辑器中打开 csv 并且它仍然显示不正确,我不确定有什么问题,因为您的代码看起来很正常。

Solved directly on client-side .直接在client-side解决 I made my own download routine and passed the UTF-8 BOM as first value of response string:我制作了自己的下载例程并将UTF-8 BOM作为响应字符串的第一个值传递:

downloadFile2(data, fileName, type="text/string") {
    // Create an invisible A element
    const a = document.createElement("a");
    a.style.display = "none";

    // Using "universal BOM" https://technet.microsoft.com/en-us/2yfce773(v=vs.118)
    const universalBOM = "\uFEFF";
    a.setAttribute('href', 'data:text/csv; charset=utf-8,' + encodeURIComponent(universalBOM+data));
    // Use download attribute to set set desired file name
    a.setAttribute('download', fileName);
    document.body.appendChild(a);
    // Trigger the download by simulating click
    a.click();

    // Cleanup
    window.URL.revokeObjectURL(a.href);
    document.body.removeChild(a);
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM