ClosedXML / ClosedXML

ClosedXML is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying OpenXML API.
MIT License
4.7k stars 822 forks source link

Support System.ComponentModel.DataAnnotations #2353

Closed vanillajonathan closed 1 week ago

vanillajonathan commented 3 months ago

Read and complete the full issue template

Do not randomly delete sections. They are here for a reason.

Do you want to request a feature or report a bug?

Did you test against the latest CI build?

If you answered No, please test with the latest development build first.

Version of ClosedXML

0.102.2

What is the current behavior?

Attributes used on a model fed to the InsertTable method have no effect.

What is the expected behavior or new feature?

ClosedXML should adjust output according to attributes from the System.ComponentModel.DataAnnotations namespace.

Is this a regression from the previous version?

No.

Reproducibility

Code to reproduce problem:

public void Main()
{
    var collection = new List<TestData>();
    collection.Add(new TestData
    {
        Date = DateTime.Now,
        Email = "example@example.com",
        Url = "https://www.example.com/",
    });

    using var workbook = new XLWorkbook();

    var worksheet = workbook.AddWorksheet();
    worksheet.Cell("A1").InsertTable(collection);

    workbook.SaveAs("test.xlsx");
}

public class TestData
{
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }

    [DataType(DataType.EmailAddress)]
    public DateTime Date { get; set; }

    [DataType(DataType.Url)]
    public string Url { get; set; }
}
jahav commented 1 week ago

Thank you for the suggestion, but this is not something I wish to have in core library. It's basically out of scope of the project.

Generally speaking, ClosedXML should provide ability to determine concrete formatting, without actually doing a concrete formatting convention. E.g. DataType.Date has many possible formats, e.g. ISO8601, long date, short date... Whatever choice is selected, it would be wrong for someone (plus fun with formats for different countries). Therefore ClosedXML shouldn't do it. Also, many enum values don't really make sense for formatting, e.g. DataType.Upload.

IXLCell.InsertTable should insert data, nothing more. Is user desires to fix formatting, he should set it himself.