Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExcelWriter converts field containing hyphen into excel DateTime field #33

Open
idbates opened this issue Nov 3, 2022 · 0 comments
Open

Comments

@idbates
Copy link

idbates commented Nov 3, 2022

Using the following code snippet to write an excel file to a memory stream.

using var ms = new MemoryStream();
var operators = _operatorRepository.Table.Where(p=> p.ForsApiProviderId == id ).ToList();
var records = _mapper.Map<IEnumerable<OperatorImportDto>>(operators);

 using (var excelWriter = new ExcelWriter(ms, CultureInfo.InvariantCulture))
 {
       excelWriter.Context.TypeConverterOptionsCache.AddOptions<DateTime?>(new CsvHelper.TypeConversion.TypeConverterOptions
       {
                Formats = new[] { "yyyy-MM-dd HH:mm:ss" }
       });

        excelWriter.WriteRecords(records);
 }

With a dto such as this:

    public class OperatorImportDto
    {
        public int company_id { get; set; }
        public string scheme_id { get; set; }
        public string level { get; set; }
        public string name { get; set; }
        public string status { get; set; }
        public string address_line_1 { get; set; }
        public string address_line_2 { get; set; }
        public string address_line_3 { get; set; }
        public string city { get; set; }
        public string postcode { get; set; }
        public string country { get; set; }
        public DateTime? timestamp { get; set; }
        public string primary_contact_name { get; set; }
        public string primary_contact_email { get; set; }
        public string primary_contact_number { get; set; }
       
    }

The input data looks like this:
image

But the scheme_id field in this record is written as the Date field "01/05/2782" in excel.
My excel culture is GB English.

image

Expected behavior
The scheme_id field "002782-5" is left as a string containing the hyphen.

Screenshots
See above.

Versions (please complete the following information):

  • CSVHelper: 27.2.1
  • CSVHelper.Excel.Core: 27.2.1

Additional context
The file is written to the memory stream, converted to an array and downloaded in an ASP.NET core web application.

        [HttpGet]
        public IActionResult ExportCsv(int id, string type = "csv")
        {
            var vm = _apiProviderService.Get(id);
            var ext = type == "csv" ? "csv" : "xlsx";
            var fileName = $"{vm.ProviderSchemeId}_Operators_{DateTime.Now:yyyyMMddHHmm}.{ext}";
            var data = _operatorImportExportService.Export(id, type);
            return File(data, type == "csv"? "text/csv": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant