C# - OpenOfficeXML 日期格式

C# - OpenOfficeXML Date Format

我在使用 OOX 将一些数据传递到 Excel 电子表格时遇到问题。事情是,一切都很好,除了日期,它以数值而不是日期时间格式显示。这是我的代码:

private object ProcessXLSFile(PayeesWorkingHoursFullDataDTO payeesWorkingHoursFullDataDTO)
    {
        DataTable dt = Functions.ObjectToDataTable(payeesWorkingHoursFullDataDTO.ListWorkingHours);

        ExcelPackage pck = new ExcelPackage();

        //Create the worksheet
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("WorkSheet1");

        //Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
        ws.Cells["A1"].LoadFromDataTable(dt, true);

        string _range = "G2:G" + (dt.Rows.Count + 1).ToString();
        using (ExcelRange col = ws.Cells[_range])
        {
            col.Style.Numberformat.Format = "#,##0.00";
            col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
        }

        _range = "J2:J" + (dt.Rows.Count + 1).ToString();
        using (ExcelRange col = ws.Cells[_range])
        {
            col.Style.Numberformat.Format = "dd/MM/yyyy";
            col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        }

        _range = "T2:T" + (dt.Rows.Count + 1).ToString();
        using (ExcelRange col = ws.Cells[_range])
        {
            col.Style.Numberformat.Format = "dd-MM-yyyy";
            col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        }

        //Write it back to the client
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment; filename=Payee-Grid.xlsx");
        Response.BinaryWrite(pck.GetAsByteArray());
        Response.End();

        throw new NotImplementedException();
    }

这些行是应将列转换为日期时间的行:

_range = "J2:J" + (dt.Rows.Count + 1).ToString();
        using (ExcelRange col = ws.Cells[_range])
        {
            col.Style.Numberformat.Format = "dd/MM/yyyy";
            col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        }

但这绝对不行。

这是我创建数据表的代码:

public static DataTable ObjectToDataTable<T>(this IList<T> data)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (T item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;
    }

调试列我已经能够看到,直到我将列格式化为 "dd-MM-yyyy" 的行,日期信息仍然是日期时间格式。我已经尝试了一切,但我似乎真的无法让它正常工作。你们知道如何让它发挥作用吗?

你试过了吗:

_range = "J2:J" + (dt.Rows.Count + 1).ToString();
        using (ExcelRange col = ws.Cells[_range])
        {
            col.Style.Numberformat.Format = "dd\/MM\/yyyy";
            col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
        }

有时,出于不明原因,您不得不逃避事情:-)

问题出在列的宽度上。出于某种原因,如果宽度太窄,单元格中的所有文本都会转换为 ####,这很奇怪。我想通了,因为它只是一个视觉故障,单元格中的值是正确的。在网格的所有单元格中使用 AutoFit() 解决了这个问题。谢谢大家!