Excel date format using EPPlus

后端 未结 9 530
长情又很酷
长情又很酷 2020-12-18 17:44

I\'m having trouble with format my cells to Date.

FileInfo info = new FileInfo(path);
using (ExcelPackage package = new ExcelPackage(info))
{
      ExcelWork         


        
相关标签:
9条回答
  • 2020-12-18 18:22
    var dateColumns = from DataColumn d in dt.Columns
                      where d.DataType == typeof(DateTime) || d.ColumnName.Contains("Date")
                      select d.Ordinal + 1;
    
    foreach (var dc in dateColumns)
    {
        worksheet.Cells[2, dc, rowCount + 2, dc].Style.Numberformat.Format = "mm/dd/yyyy hh:mm:ss AM/PM";
    }
    

    it will format all the columns with header Date to specific format given/ provided

    0 讨论(0)
  • 2020-12-18 18:22

    Following on from the very good Generic solution which takes IEnumerable.. answer we had to go a step further and display different date formatting for different properties. Fro example some columns needed to be displayed as dd/MM/yyyy and others as dd/MM/yyyy hh:mm.

    So we added a DisplayFormat annotation with a DataFormatString (representing a DateTime format) to our properties like this:

    using System.ComponentModel.DataAnnotations;
    ...
    [DisplayName("Download Date")]
    [DisplayFormat(DataFormatString = "dd/MM/yyyy hh:mm")]
    public string DownloadDate { get; set; }
    ...
    

    And then borrowing from Generic solution which takes IEnumerable.. we pulled out the date format string from the DisplayFormat annotation when iterating the properties of the data object:

    public void FormatDateColumns(ExcelWorksheet worksheet, IEnumerable<IResult> data)
    {
        // Dictionary 'key' contains the Index of the column that contains DateTime data
        // Dictionary 'value' contains the DateTime format for that column
        Dictionary<int, string> dateColumns = new Dictionary<int, string>();
        int dateColumnIndex = 1;
    
        // find all the DateTime/DateTime? columns in the data object 
        foreach (var PropertyInfo in data.FirstOrDefault().GetType().GetProperties())
        {
            if (PropertyInfo.PropertyType == typeof(DateTime) || PropertyInfo.PropertyType == typeof(DateTime?))
            {
                string dateTimeFormat = Constants.DefaultDateTimeFormat;
    
                // attempt to get a DataFormatString from a DisplayFormat annotation which may be decorating the Property
                // looking for an annotation something like [DisplayFormat(DataFormatString = "dd-MM-yyyy hh:mm")] 
                if (PropertyInfo.CustomAttributes != null)
                {
                    var dislayFormatAttribute = PropertyInfo.CustomAttributes.Where(x => x.AttributeType.Name == "DisplayFormatAttribute").FirstOrDefault();
                    if (dislayFormatAttribute != null && dislayFormatAttribute.NamedArguments != null && dislayFormatAttribute.NamedArguments.Count > 0)
                    {
                        var displayFormatArg = dislayFormatAttribute.NamedArguments.First();
                        if (displayFormatArg != null && displayFormatArg.TypedValue != null && displayFormatArg.TypedValue.Value != null)
                        {
                            // NOTE: there is probably an easier way to get at this value?
                            dateTimeFormat = displayFormatArg.TypedValue.Value.ToString();
                        }
                    }
                }
    
                dateColumns.Add(dateColumnIndex, dateTimeFormat);
            }
            dateColumnIndex++;
        }
    
        if (dateColumns.Count > 0)
        {
            // apply the formatting
            dateColumns.ToList().ForEach(item => worksheet.Column(item.Key).Style.Numberformat.Format = item.Value);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 18:25

    Some news:

    ws.Cells["A3"].Style.Numberformat.Format = "[$-en-US]yyyy-mmm-dd";
    ws.Cells["A3"].Formula = "=DATE(2014,10,5)";

    0 讨论(0)
提交回复
热议问题