MiniExcel icon indicating copy to clipboard operation
MiniExcel copied to clipboard

Reading from file and writing to template does not format dates correctly

Open tombolini opened this issue 2 years ago • 0 comments

Excel Type

  • [X] XLSX
  • [ ] XLSM
  • [ ] CSV
  • [ ] OTHER

Upload Excel File

SourceFile.xlsx (Note that the names included are not real and randomly generated)

TemplateFile.xlsx

MiniExcel Version

1.31.3

Description

See my example program below. I am reading from my source file and writing the same information to a new file using a template. This works fine, however, in the Date column, somehow the date is not immediately recognized by Excel as a date value, only when I click into the cell and leave the cell is it recognized as a date value.

As you can see in my sample class, it is read as a DateTime, and in the source file the date entries are correctly formatted as dates.

Source file: image

Generated file: image

 internal class Program
 {
     static void Main(string[] args)
     {
         var content = MiniExcel.Query<SampleClass>(@"SourceFile.xlsx").ToList();
         var templateValues = new Dictionary<string, object>();
         templateValues.Add(nameof(content), content);
         MiniExcel.SaveAsByTemplate("GeneratedTemplate.xlsx", "TemplateFile.xlsx", templateValues);
     }

     public class SampleClass
     {
         [ExcelColumnName("Date")]
         public DateTime Date { get; set; }
         [ExcelColumnName("Name")]
         public string Name { get; set; }
     }
 }

I've already tried adding [ExcelFormat("dd.MM.yyyy")] to my SampleClass, which changes the text in the cell itself, but doesn't change the behavior of Excel.

My problem is that I want to use the dates in a pivot table and I can't use grouping because the dates are not recognized as such.

Thanks in advance and thank you for this great and helpful library!

tombolini avatar Feb 29 '24 10:02 tombolini