I'm having troubles using StringWriter
on our application.
I do a rest call over a nosql db
and it returns a list of dynamics
.
I use StringWriter
to write a csv
file that contains a header and records from my list.
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. But trying all the encodings available it still generates wrong charachters.
This is our extension of 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:
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
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.
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);
}
UPDATE 2
The problem is client-side
, my action returns the correct text, without broken chars.
Action is triggered when i call it with an axios
get instance.
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.
In my action
when i return the 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.